logic.go 10.1 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
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 (
48
49
50
51
52
53
54
	logicVarLineCount                   = 30
	logicVarWindow                      = logicWindowNone
	logicVarVideos    []*videoContainer = []*videoContainer{}
	logicVarLazy                        = &listLazy{}
	logicVarUsers                       = []string{}
	logicVarUser                        = ""
	logicVarSearch                      = ""
Jonny Schäfer's avatar
Jonny Schäfer committed
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
)

// 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) {
96
	upd := func(state int) []*videoContainer {
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
97
		return videoListSearch(title, strconv.Itoa(state+1))
98
99
100
	}

	logicVarLazy = listNew(upd)
Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
101
	logicSearchPage("searching: "+title, 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
102
103
104
}

//logicSearchPage searches the title in the given page
105
106
func logicSearchPage(title string, page int) {
	cliStatus("page " + strconv.Itoa(page) + " | " + title)
Jonny Schäfer's avatar
Jonny Schäfer committed
107
108
	logicVarWindow = logicWindowSearch
	logicVarSearch = title
109

Jonny Schäfer's avatar
go fmt    
Jonny Schäfer committed
110
	list := logicVarLazy.listSlice(page*logicVarLineCount, (page+1)*logicVarLineCount)
111

Jonny Schäfer's avatar
Jonny Schäfer committed
112
113
114
115
116
117
118
	if list == nil {
		cliStatusError("network error: unable to load data")
		return
	}
	if len(list) == 0 {
		cliStatusError("no results for the given query")
	}
Jonny Schäfer's avatar
Jonny Schäfer committed
119
	logicList(-1, list)
Jonny Schäfer's avatar
Jonny Schäfer committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
}

// 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) {
Jonny Schäfer's avatar
Jonny Schäfer committed
140
			logicPlay(player, sel) // non blocking
Jonny Schäfer's avatar
Jonny Schäfer committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		} 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) {
155
156
157
	pnum, err := strconv.Atoi(page)
	errorCare(err)

Jonny Schäfer's avatar
Jonny Schäfer committed
158
159
	switch logicVarWindow {
	case logicWindowSearch:
160
		logicSearchPage(logicVarSearch, pnum)
Jonny Schäfer's avatar
Jonny Schäfer committed
161
	case logicWindowUser:
162
		logicUserPage(logicVarUser, pnum)
Jonny Schäfer's avatar
Jonny Schäfer committed
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
	}
}

// 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")
Jonny Schäfer's avatar
Jonny Schäfer committed
199
	cliTable(-1, id[0], us[0], id[1], us[1], id[2], us[2], id[3], us[3])
Jonny Schäfer's avatar
Jonny Schäfer committed
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
}

// 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) {
252
	logicUserPage(user, 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
253
254
255
}

// logicUserPage shows the page of the given user
256
func logicUserPage(user string, page int) {
Jonny Schäfer's avatar
Jonny Schäfer committed
257
	// TODO: support pages (priority: low)
258
	cliStatus("page " + strconv.Itoa(page) + " | user: " + user)
Jonny Schäfer's avatar
Jonny Schäfer committed
259
260
	logicVarWindow = logicWindowUser
	logicVarUser = user
Jonny Schäfer's avatar
Jonny Schäfer committed
261
	logicList(-1, videoListUser(user))
Jonny Schäfer's avatar
Jonny Schäfer committed
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
}

// 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})

299
300
301
302
	upd := func(state int) []*videoContainer {
		all := videos
		videos = nil
		return all
Jonny Schäfer's avatar
Jonny Schäfer committed
303
	}
304
	logicVarLazy = listNew(upd)
305
	logicSearchPage("new videos", 0)
Jonny Schäfer's avatar
Jonny Schäfer committed
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
}

// 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)
}

Jonny Schäfer's avatar
Jonny Schäfer committed
349
350
351
352
// logicList shows a video list and highlights the line with the given
// line number. If hightlight < 0 no line will be highlighted.
// The global video list logicVarVideos will be set by this function.
func logicList(highlight int, videos []*videoContainer) {
Jonny Schäfer's avatar
Jonny Schäfer committed
353
	logicVarVideos = videos
Jonny Schäfer's avatar
Jonny Schäfer committed
354
355

	cliClearDown()
Jonny Schäfer's avatar
Jonny Schäfer committed
356
357
358
359
360
361
362
363
364
365
	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)
	}
Jonny Schäfer's avatar
Jonny Schäfer committed
366
	cliTable(highlight, id, titles, durations, users, views, dates)
Jonny Schäfer's avatar
Jonny Schäfer committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
}

// 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()
}

Jonny Schäfer's avatar
Jonny Schäfer committed
384
385
386
// logicPlay plays the video with the given video id in the player
func logicPlay(player string, id int) {
	video := logicVarVideos[id]
Jonny Schäfer's avatar
Jonny Schäfer committed
387
388
389
390
391
	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 {
Jonny Schäfer's avatar
Jonny Schäfer committed
392
393
		cliStatus("[" + player + "] " + video.title)
		logicList(id, logicVarVideos)
Jonny Schäfer's avatar
Jonny Schäfer committed
394
395
396
	}
	go exe.Wait()
}