logic.go 9.53 KB
Newer Older
Jonny Schäfer's avatar
Jonny Schäfer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main

import (
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"sort"
	"strconv"
	"strings"
	"sync"
)

// maps user input to events
type logicMatchEntry struct {
	event func(string)
	match *regexp.Regexp
}

// compile regex patterns and map to actions
var (
	logicMatchTable = map[string]*logicMatchEntry{
		"logicSearch": &logicMatchEntry{logicSearch, regexp.MustCompile(`(?m)^[^\:]`)},
		"logicNumber": &logicMatchEntry{logicNumber, regexp.MustCompile(`(?m)^\:[0-9]+(\ |$)`)},
		"logicPage":   &logicMatchEntry{logicPage, regexp.MustCompile(`(?m)^\::[0-9]+$`)},
		"logicSub":    &logicMatchEntry{logicSub, regexp.MustCompile(`(?m)^\:sub$`)}, // avoid matching conflict with subadd!
		"logicSubAdd": &logicMatchEntry{logicSubAdd, regexp.MustCompile(`(?m)^\:subadd`)},
		"logicSubDel": &logicMatchEntry{logicSubDel, regexp.MustCompile(`(?m)^\:subdel`)},
		"logicUser":   &logicMatchEntry{logicUser, regexp.MustCompile(`(?m)^\:user`)},
		"logicNew":    &logicMatchEntry{logicNew, regexp.MustCompile(`(?m)^\:new$`)},
		"logicSet":    &logicMatchEntry{logicSet, regexp.MustCompile(`(?m)^\:set`)},
		"logicHelp":   &logicMatchEntry{logicHelp, regexp.MustCompile(`(?m)^\:help`)},
		"logicQuit":   &logicMatchEntry{logicQuit, regexp.MustCompile(`(?m)^\:q$`)},
	}
)

type logicWindowState int

// window states
const (
	logicWindowNone logicWindowState = iota
	logicWindowSearch
	logicWindowSub
	logicWindowUser
)

var (
	logicVarWindow                   = logicWindowNone
	logicVarVideos []*videoContainer = []*videoContainer{}
	logicVarUsers                    = []string{}
	logicVarUser                     = ""
	logicVarSearch                   = ""
)

// logicAction parses the input string and performs the given action
func logicAction(input string) {
	// match pattern
	logicMatch(input)(logicTrim(input))
}

// logicMatch returns the best fitting function for the given input
func logicMatch(input string) func(string) {
	for _, entry := range logicMatchTable {
		if entry.match.MatchString(input) {
			return entry.event
		}
	}
	return logicUnknown
}

// logicTrim separates and returns the commands arguments
func logicTrim(trim string) string {
	if logicMatchTable["logicNumber"].match.MatchString(trim) {
		return trim[1:]
	}

	if logicMatchTable["logicPage"].match.MatchString(trim) {
		return trim[2:]
	}

	if strings.Index(trim, ":") == 0 {
		out := strings.SplitN(trim, " ", 2)
		if len(out) > 1 {
			return out[1]
		}
		return ""
	}

	return trim
}

// logicSearch searches for the given video title
func logicSearch(title string) {
	logicSearchPage(title, "1")
}

//logicSearchPage searches the title in the given page
func logicSearchPage(title, page string) {
	cliStatus("page " + page + " | searching for: " + title)
	logicVarWindow = logicWindowSearch
	logicVarSearch = title
	list := videoListSearch(title, page)
	if list == nil {
		cliStatusError("network error: unable to load data")
		return
	}
	if len(list) == 0 {
		cliStatusError("no results for the given query")
	}
	logicList(list)
}

// logicNumber selects the given list number
func logicNumber(command string) {
	splits := strings.SplitN(command, " ", 2)
	player := logicGetPlayer()
	if len(splits) > 1 {
		player = splits[1]
	}

	sel, err := strconv.Atoi(splits[0])
	errorCare(err)

	switch logicVarWindow {
	case logicWindowNone:
		cliStatusError("error: video list is empty")
	case logicWindowUser:
		fallthrough
	case logicWindowSearch:
		if sel < len(logicVarVideos) {
			logicPlay(player, logicVarVideos[sel]) // non blocking
		} else {
			cliStatusError("error: video id not in list")
		}
	case logicWindowSub:
		if sel < len(logicVarUsers) {
			logicUser(logicVarUsers[sel])
		} else {
			cliStatusError("error: user id not in list")
		}
	}
}

// logicPage selects the given page
func logicPage(page string) {
	switch logicVarWindow {
	case logicWindowSearch:
		logicSearchPage(logicVarSearch, page)
	case logicWindowUser:
		logicUserPage(logicVarUser, page)
	}
}

// logicGetUsers retieves a list of all subscripted users
func logicGetUsers() []string {
	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)
	users := []string{}

	for _, user := range strings.Split(subs, "\n") {
		if len(user) > 2 && user[0] == '"' && user[len(user)-1] == '"' {
			users = append(users, user[1:len(user)-1])
		}
	}
	return users
}

// logicSub lists the subscriptions
func logicSub(none string) {
	columns := 4

	logicVarWindow = logicWindowSub
	id := make([][]string, columns)
	us := make([][]string, columns)

	users := logicGetUsers()
	sort.Strings(users)

	newcol := len(users)/columns + 1
	for i, u := range users {
		id[i/newcol] = append(id[i/newcol], strconv.Itoa(i))
		us[i/newcol] = append(us[i/newcol], u)
	}

	logicVarUsers = users
	cliStatus("list of subscriptions")
	cliTable(id[0], us[0], id[1], us[1], id[2], us[2], id[3], us[3])
}

// logicSubAdd adds the given user to the subscription list
func logicSubAdd(user string) {
	if user == "" && logicVarWindow == logicWindowUser {
		user = logicVarUser
	}

	if strings.Contains(user, "\"") {
		cliStatusError("error: illicit username")
		return
	}

	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)

	if strings.Contains(subs, "\""+user+"\"") {
		cliStatusError("error: user '" + user + "' already in list")
		return
	}

	errorCare(storeWriteFile(storeDefaultSubscriptions, subs+"\""+user+"\"\n"))
	cliStatus("subscription '" + user + "' added")
	cliClearDown()
}

// logicSubAdd adds the given user to the subscription list
func logicSubDel(user string) {
	if user == "" && logicVarWindow == logicWindowUser {
		user = logicVarUser
	}

	if strings.Contains(user, "\"") {
		cliStatusError("error: illicit username")
		return
	}

	subs, err := storeReadFile(storeDefaultSubscriptions)
	errorCare(err)

	if !strings.Contains(subs, "\""+user+"\"") {
		cliStatusError("error: user '" + user + "' not in list")
		return
	}

	errorCare(storeWriteFile(storeDefaultSubscriptions, strings.Replace(subs, "\""+user+"\"\n", "", 1))) // meh
	cliStatus("subscription '" + user + "' deleted")
	cliClearDown()
}

// logicUser shows the videos of the given user
func logicUser(user string) {
	logicUserPage(user, "1")
}

// logicUserPage shows the page of the given user
func logicUserPage(user, page string) {
	// TODO: support pages (priority: low)
	cliStatus("page " + page + " | user: " + user)
	logicVarWindow = logicWindowUser
	logicVarUser = user
	logicList(videoListUser(user))
}

// logicNew shows a list of new videos
func logicNew(none string) {
	logicVarWindow = logicWindowSearch
	logicVarSearch = ""
	videos := []*videoContainer{}
	users := logicGetUsers()
	lock := &sync.Mutex{}
	barrier := &sync.WaitGroup{}
	barrier.Add(len(users))

	for i, user := range users {

		// put into scope
		user := user
		i := i
		go func() {
			list := videoListUser(user)
			cliHome()
			fmt.Println()
			lock.Lock()

			cliStatus("updating users (" + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(users)) + ")")
			videos = append(videos, list...)

			lock.Unlock()
			barrier.Done()
		}()
	}
	barrier.Wait()

	cliHome()
	fmt.Println()
	cliStatus("sorting ...")
	sort.Sort(&videoSort{videos})

	if len(videos) > 30 {
		videos = videos[0:30] // TODO add multiple pages
	}

	logicVarVideos = videos

	cliHome()
	fmt.Println()
	cliStatus("newest videos")
	logicList(videos)
}

// logicSet sets the key to the given value. Format: "key value"
func logicSet(perform string) {
	logicSetPlayer(perform)
}

// logicHelp shows the help
func logicHelp(none string) {
	cliStatus("available commands and usage")
	cliClearDown()

	help := `
to search for a video simply input keywords

commands:

:[number] [player]   select list item (optional: video player)
::[number]           select list page
:user [user]         show videos of user
:sub                 show subscriptions
:new                 show new subscribed videos
:subadd [user]       add given or current user
:subdel [user]       delete given or current user
:set [player]        set default video player
:help                show this help
:q                   quit the program
`

	fmt.Println(help)
}

// logicUnknown shows a message if the command could not be parsed
func logicUnknown(command string) {
	cliStatusError("error: unknown command")
}

// logicQuit exits the program
func logicQuit(none string) {
	cliReset()
	os.Exit(0)
}

// logicList shows a video list
func logicList(videos []*videoContainer) {
	cliClearDown()
	logicVarVideos = videos
	id, titles, urls, durations, users, dates, views := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
	for i, v := range videos {
		id = append(id, strconv.Itoa(i))
		titles = append(titles, cliTrimLength(v.title, " >>", 55))
		urls = append(urls, v.url)
		durations = append(durations, v.duration)
		users = append(users, v.user)
		dates = append(dates, v.date)
		views = append(views, v.views)
	}
	cliTable(id, titles, durations, users, views, dates)
}

// logicGetPlayer gets the default video player
func logicGetPlayer() string {
	player, err := storeReadFile(storeDefaultPlayer)
	errorCare(err)

	return strings.Replace(player, "\n", "", -1)
}

// logicSetPlayer sets the default video player
func logicSetPlayer(player string) {
	errorCare(storeWriteFile(storeDefaultPlayer, player))
	cliStatus("default player set to '" + player + "'")
	cliClearDown()
}

// logicPlay plays the video in the given player
func logicPlay(player string, video *videoContainer) {
	cmd := append(strings.Split(player, " "), "https://www.youtube.com/watch?v="+video.url)
	exe := exec.Command(cmd[0], cmd[1:]...)
	if err := exe.Start(); err != nil {
		cliStatusError(fmt.Sprint("playback error: ", err))
	} else {
		cliStatus("[" + player + "] " + video.title)
	}
	go exe.Wait()
}