/
youtubeassist
executable file
·674 lines (568 loc) · 16.7 KB
/
youtubeassist
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/bin/bash
##
## command line tools for working with YouTube.
## See: https://developers.google.com/youtube/v3/docs/
##
config=$HOME/.config/youtubeassist/config
if [ -f "$config" ] ; then
. $config
else
echo "Missing config file: $config"
exit
fi
API_BASE=https://www.googleapis.com/youtube/v3
function usage() {
cmd="Usage: $(basename $0)"
echo "$cmd: -a init"
echo "$cmd: -a subscriptions"
echo "$cmd: -a subscription-delete -i id"
echo "$cmd: -a playlists"
echo "$cmd: -a playlist {-i id} {-n name}"
echo "$cmd: -a playlist-delete {-i id} {-n name}"
echo "$cmd: -a playlist-rename -i id -n new-name"
echo "$cmd: -a playlist-create -n name"
echo "$cmd: -a playlist-update {-n name} {-i id} [-p public|private|unlisted] [-t title]"
echo "$cmd: -a playlist-age -n playlist-name -t ttl-in-days -p preview|process [ -d directory-to-save-to ] [ -o playlist-to-output-to ] "
echo "$cmd: -a playlist-item-add -n playlist-name -i video-id"
echo "video_id ... | $cmd: -a playlist-items-add -n playlist-name"
echo "$cmd: -a playlist-item-delete -n playlist-name -i video-id"
echo "$cmd: -a playlists-rotate"
echo "$cmd: -a video -i id"
echo "$cmd: -a my-channel -i id"
echo "$cmd: -a playlists-snapshot -d directory"
echo "$cmd: -a search -q query [-o output] [-F] [-s date|rating|relevance|viewCount]"
exit
}
date_cmd() {
case $(uname) in
Darwin) echo gdate ;;
*) echo date ;;
esac
}
age() {
date=$(date_cmd)
before=$($date +%s -d "$1")
now=$($date +%s)
echo $(((now - before) / (60 * 60 * 24)))
}
playlist_id_by_name() {
n=$1
if [ -n "$n" ] ; then
is_id=$(echo $n | grep ^PL..........................)
if [ -n "$is_id" ] ; then
echo $n
else
found=$(youtubeassist -a playlists | grep "[|]${n}$" | cut -d'|' -f1 | head -1)
if [ -n "$found" ] ; then
echo $found
else
echo ""
fi
fi
else
echo ""
fi
}
resolve_dir() {
d="$1"
if [ -d "$d" -a -w "$d" ];then
echo $d
else
echo ""
fi
}
search_history_dir() {
q="$1"
s="$2"
search_name=$(echo "${q}-${s}" | sed 's/[^A-Za-z0-9-]//g')
search_id=$(echo -n "${q}-${s}" | cksum | cut -d' ' -f1)
echo "$SEARCH_HISTORY_DIR/${search_name}.${search_id}"
}
last_modified() {
file="$1"
case $(uname) in
Darwin) stat -f '%m' $file ;;
*) stat --format '%Y' $file ;;
esac
}
search_cache_age() {
q="$1"
s="$2"
dir=$(search_history_dir "$q" "$s")
a_year_ago=$((60*60*24*365))
if [ -d "$dir" ] ; then
last_file=$(cd $dir ; ls -t | head -1)
if [ -z "$last_file" ] ; then
echo $a_year_ago
else
now=$($(date_cmd) +%s)
t=$(echo $last_file | sed 's/[.].*//')
echo $((now - t))
fi
else
echo $a_year_ago
fi
}
search_execute() {
q="$1"
s="$2"
history_dir=$(search_history_dir "$q" "$s")
mkdir -p $history_dir
history_file=$history_dir/$($(date_cmd) +%s.%N).history
touch $history_file
if [ ! -w "$history_file" ] ; then
echo "Can't write to $history_file"
exit
fi
MAX_PAGES=4
invoke -G $API_BASE/search \
-d part=snippet \
-d maxResults=50 \
-d order=$sort_by \
--data-urlencode q="$query" \
-d safeSearch=none \
-d videoDefinition=high \
-d type=video > $HOME/.yt.serp
cat $HOME/.yt.serp | jq -r '.items[] | .id.videoId' > $HOME/.yt.serp.ids
cat $HOME/.yt.serp.ids | while read id ; do
found=$(grep ^${id}$ $history_dir/*.history)
if [ -z "$found" ]; then
echo $id >> $history_file
cat $HOME/.yt.serp | jq "[.items[] | select(.id.videoId == \"$id\")][0]" > $SEARCH_CACHE_DIR/$id.hit
fi
done
if [ ! -s "$history_file" ] ; then
rm $history_file
fi
}
search_data() {
q="$1"
s="$2"
f="$3"
history_dir=$(search_history_dir "$q" "$s")
if [ "$f" = "yes" ] ; then
limit_cmd="cat"
else
limit_cmd="head -1"
fi
echo '{ "items": [ '
sep=""
ls -t $history_dir | $limit_cmd | while read file_name ; do
ids_file=$history_dir/$file_name
cat $ids_file | while read id ; do
echo $sep
cat $SEARCH_CACHE_DIR/$id.hit
sep=","
done
sep=","
done
echo '] }'
}
filter() {
if [ -z "$VERBOSE" ] ; then
jq "$@"
else
cat
fi
}
while getopts ":a:i:n:vd:l:o:t:p:q:s:F" opt ; do
case $opt in
a) ACTION=$OPTARG ;;
v) VERBOSE=yes ;;
i) id=$OPTARG ;;
n) name=$OPTARG ;;
d) dir=$OPTARG ;;
t) ttl=$OPTARG ;;
o) output=$OPTARG ;;
p) privacy=$OPTARG ;;
s) sort_by=$OPTARG ;;
q) query=$OPTARG ;;
F) full_results=yes ;;
\?) usage ;;
esac
done
MAX_PAGES=10000
function invoke {
curl -s -H "Authorization: Bearer $AUTH_TOKEN" "$@" > /tmp/yt.buffer.$$
next_page=`jq -r '.nextPageToken' < /tmp/yt.buffer.$$`
num_pages=1
if [ "$next_page" = "null" -o -z "$next_page" ] ; then
cat /tmp/yt.buffer.$$
rm -f /tmp/yt.buffer.$$
else
jq '.items' < /tmp/yt.buffer.$$ | sed 's/^.//'> /tmp/yt.master.$$
while [ "$next_page" != "null" -a $num_pages -lt $MAX_PAGES ] ; do
curl -s -H "Authorization: Bearer $AUTH_TOKEN" "$@" -d pageToken=$next_page |
tee /tmp/yt.buffer.$$ |
( echo "," ; jq '.items' | sed 's/^.//' ) >> /tmp/yt.master.$$
next_page=`jq -r '.nextPageToken' < /tmp/yt.buffer.$$`
num_pages=$((num_pages + 1))
done
rm -f /tmp/yt.buffer.$$
echo '{ "items" : [ '
cat /tmp/yt.master.$$
echo ' ] }'
rm -f /tmp/yt.master.$$
fi
}
case $ACTION in
init)
gapi_auth -i $CLIENT_ID -p $CLIENT_SECRET -r $AUTH_REDIRECT_URI -s "$API_SCOPE" init
exit
;;
my-channel)
invoke -G $API_BASE/channels \
-d mine=true \
-d part=snippet,contentDetails,statistics \
-d maxResults=50 |
filter -r '.'
;;
subscriptions)
invoke -G $API_BASE/subscriptions \
-d mine=true \
-d part=snippet \
-d maxResults=50 |
filter -r ' .items[] | .id + "|" + .snippet.title'
;;
subscription-delete)
if [ -z "$id" ] ; then
echo "Deleting a subscription requires an id (-i) value"
usage
fi
invoke -X DELETE "$API_BASE/subscriptions?id=$id"
;;
playlists)
invoke -G $API_BASE/playlists \
-d mine=true \
-d part=snippet,status \
-d maxResults=50 |
filter -r ' .items[] | .id + "|" + .status.privacyStatus + "|" + .snippet.title'
;;
playlist)
if [ -n "$name" ] ; then
found=$(playlist_id_by_name "$name")
if [ -n "$found" ] ; then
id=$found
fi
fi
if [ -z "$id" ] ; then
echo "Listing a playlist requires a playlist id"
usage
fi
invoke -G $API_BASE/playlistItems \
-d playlistId=$id \
-d part=snippet,status,contentDetails \
-d maxResults=50 |
filter -r '.items[] | .id + "|" + .contentDetails.videoId + "|" + .snippet.publishedAt + "|" + .snippet.title' > $HOME/.yt.buffer
if [ -n "$VERBOSE" ] ; then
cat $HOME/.yt.buffer
else
cat $HOME/.yt.buffer | while read line ; do
id=$(echo $line | cut -d'|' -f1)
video_id=$(echo $line | cut -d'|' -f2)
added=$(echo $line | cut -d'|' -f3)
age=$(age $added)
title=$(echo $line | cut -d'|' -f4)
echo "$id|$video_id|$age|$title"
done
fi
;;
playlist-update)
if [ -n "$name" ] ; then
id=$(playlist_id_by_name "$name")
if [ -z "$id" ] ; then
echo "$name is not a valid playlist."
exit
fi
fi
if [ -z "$id" ] ; then
echo "Updating a playlist requires an ID (-i) value"
usage
fi
if [ -n "$privacy" ] ; then
case "$privacy" in
private|public|unlisted) ;;
*) echo "Invalid -p privacy. Must be: public, private or unlisted" ; exit ;;
esac
else
privacy=$(youtubeassist -a playlists | grep "^${id}|" | cut -d '|' -f2)
fi
if [ -n "$ttl" ] ; then
title="$ttl"
else
title=$(youtubeassist -a playlists | grep "^${id}|" | cut -d'|' -f3)
fi
invoke -X PUT "$API_BASE/playlists?part=status,snippet" \
-H 'Content-Type: application/json' \
-d "{ 'id': '$id', 'snippet': { 'title': '$title' }, 'status': { 'privacyStatus': '$privacy'} } " |
filter -r '.id + "|" + .status.privacyStatus + "|" + .snippet.title'
;;
playlist-item-add)
if [ -z "$name" ] ; then
echo "Missing -n playlist-name"
exit
fi
playlist_id=$(playlist_id_by_name "$name")
if [ -z "$playlist_id" ] ; then
echo "No playlist found with the name $name"
fi
if [ -z "$id" ] ; then
echo "Missing -i video-id"
exit
fi
exists=$(youtubeassist -a playlist -i $playlist_id | grep "|$id|")
if [ -n "$exists" ] ; then
echo $exists
exit
fi
invoke -X POST "$API_BASE/playlistItems?part=snippet" \
-H 'Content-Type: application/json' \
-d "{ 'snippet': { 'playlistId': '$playlist_id', 'position': 0, 'resourceId': { 'kind': 'youtube#video', 'videoId': '$id' } } } " |
filter -r '.id + "|" + .snippet.title'
;;
playlist-items-add)
if [ -z "$name" ] ; then
echo "Missing -n playlist-name"
exit
fi
playlist_id=$(playlist_id_by_name "$name")
if [ -z "$playlist_id" ] ; then
echo "No playlist found with the name $name"
fi
youtubeassist -a playlist -i $playlist_id > $HOME/.yt.pl
while read id ; do
exists=$(grep "|$id|" $HOME/.yt.pl)
if [ -z "$exists" ] ; then
invoke -X POST "$API_BASE/playlistItems?part=snippet" \
-H 'Content-Type: application/json' \
-d "{ 'snippet': { 'playlistId': '$playlist_id', 'position': 0, 'resourceId': { 'kind': 'youtube#video', 'videoId': '$id' } } } " |
filter -r '.id + "|" + .snippet.title'
fi
done
;;
playlist-item-delete)
if [ -z "$name" ] ; then
echo "Missing -n playlist-name"
exit
fi
playlist_id=$(playlist_id_by_name "$name")
if [ -z "$playlist_id" ] ; then
echo "No playlist found with the name $name"
fi
if [ -z "$id" ] ; then
echo "Missing -i video-id"
exit
fi
youtubeassist -a playlist -i $playlist_id | while read line ; do
item_id=$(echo $line | cut -d'|' -f1)
video_id=$(echo $line | cut -d'|' -f2)
if [ "$video_id" = "$id" ] ; then
invoke -X DELETE "$API_BASE/playlistItems?id=$item_id"
fi
done
;;
playlist-delete)
if [ -n "$name" ] ; then
id=$(playlist_id_by_name "$name")
if [ -z "$id" ] ; then
echo "$name is not a valid playlist."
exit
fi
fi
if [ -z "$id" ] ; then
echo "Deleting a playlist requires an ID (-i) value"
usage
fi
invoke -X DELETE "$API_BASE/playlists?id=$id"
;;
playlist-rename)
if [ -z "$id" -o -z "$name" ] ; then
echo "Renaming a playlist requires you set the ID (-i) and name (-n)"
usage
fi
invoke -X PUT "$API_BASE/playlists?part=snippet" \
-H 'Content-Type: application/json' \
-d "{'id': '$ID', 'snippet': {'title' : '$name'} }" |
filter -r '.id + "|" + .snippet.title'
;;
playlist-create)
if [ -z "$name" ] ; then
echo "Missing -n name"
exit
fi
invoke -X POST "$API_BASE/playlists?part=snippet,status" \
-H 'Content-Type: application/json' \
-d "{'snippet': {'title' : '$name'}, 'status' : {'privacyStatus' : 'private'} }" |
filter -r '.id + "|" + .status.privacyStatus + "|" + .snippet.title'
;;
video)
if [ -z "$id" ] ; then
echo "Video info requires an ID (-i)"
usage
fi
invoke -G $API_BASE/videos \
-d id=$id \
-d part=snippet,id,topicDetails,contentDetails,statistics,status,player \
-d maxResults=50 |
filter -r '.items[] | .id + ":" + .snippet.title'
;;
playlists-snapshot)
if [ -z "$dir" ] ; then
echo "-d directory is missing"
exit
fi
path=$(realpath -s $dir/$($(date_cmd) +%Y%m%d.%H%M))
if [ -d "$path" -a -w "$path" ] ; then
echo "Archive directory exists: $path"
exit
fi
mkdir -p "$path"
if [ ! -d "$path" ] ; then
echo "Failed to make directory: $path"
exit
fi
youtubeassist -a playlists | while read line ; do
id=$(echo $line | cut -d'|' -f1)
name=$(echo $line | cut -d'|' -f3)
file=$(echo $name | sed -e 's/[^A-Za-z0-9]/_/g' \
-e 's/^_*//' \
-e 's/_*$//' \
-e 's/__*/_/g').json
youtubeassist -a playlist -v -i $id > $path/$file
done
;;
playlists-rotate)
archive_dir=$ROTATE_DIR
if [ ! -d "$archive_dir" ] ; then
echo "Need to setup archive directory: $archive_dir"
exit
fi
for rule in $ROTATE_RULES ; do
name=$(echo $rule | cut -d: -f1 | tr '_' ' ')
ttl=$(echo $rule | cut -d: -f2)
output=$(echo $rule | cut -d: -f3)
directory=$(echo $rule | cut -d: -f4)
if [ -n "$directory" ] ; then
mkdir -p "$archive_dir/$directory"
if [ -n "$VERBOSE" ] ; then
echo "name=$name, ttl=$ttl, dir=$archive_dir/$directory"
fi
youtubeassist -a playlist-age -n "$name" -t $ttl -d "$archive_dir/$directory" -p process
fi
if [ -n "$output" ] ; then
if [ -n "$VERBOSE" ] ; then
echo "name=$name, ttl=$ttl, output=$output"
fi
youtubeassist -a playlist-age -n "$name" -t $ttl -o "$output" -p process
fi
done
;;
playlist-age)
id=$(playlist_id_by_name "$name")
if [ -z "$id" ] ;then
echo "-n playlist not found"
exit
fi
if [ -z "$privacy" ] ; then
echo "Missing directive -p preview|process"
exit
fi
case "$privacy" in
preview|process) goal=$privacy ;;
*) echo "Invalid '$privacy', must be preview or process." ; exit ;;
esac
output_dir=$(resolve_dir $dir)
output_id=$(playlist_id_by_name $output)
if [ -z "$dir" -a -z "$output" ] ; then
echo "Must set -d dir or -o output"
exit
fi
if [ -n "$dir" -a -z "$output_dir" ] ; then
echo "-d $dir doesn't exist"
exit
fi
if [ -n "$output" -a -z "$output_id" ] ; then
echo "-o $output doesn't exist"
exit
fi
if [ -z "$ttl" ] ; then
echo "-t ttl was not set. Must be a number of days."
exit
fi
youtubeassist -a playlist -i $id | while read line ; do
id=$(echo $line | cut -d'|' -f1)
video_id=$(echo $line | cut -d'|' -f2)
age=$(echo $line | cut -d'|' -f3)
title=$(echo $line | cut -d'|' -f4)
if [ $age -gt $ttl ] ; then
case $goal in
preview)
echo "Aging '$title', $age > $ttl"
;;
process)
if [ -n "$output_id" ] ; then
youtubeassist -a playlist-item-add -n "$output" -i "$video_id" > /dev/null
fi
if [ -n "$output_dir" ] ; then
youtubeassist -a video -i "$video_id" -v > $output_dir/$video_id.json
fi
youtubeassist -a playlist-item-delete -n "$name" -i $video_id > /dev/null
esac
fi
done
;;
search)
if [ -z "$query" ]; then
echo "Missing -q query"
exit
fi
if [ -z "$SEARCH_HISTORY_DIR" ] ; then
echo "You must set a SEARCH_HISTORY_DIR"
exit
fi
if [ -z "$SEARCH_CACHE_DIR" ] ; then
echo "You must set SEARCH_CACHE_DIR"
exit
fi
if [ ! -d "$SEARCH_HISTORY_DIR" ] ; then
echo "Search $SEARCH_HISTORY_DIR doesn't exist."
exit
fi
if [ ! -d "$SEARCH_CACHE_DIR" ] ; then
echo "Search $SEARCH_CACHE_DIR doesn't exist."
exit
fi
if [ -z "$sort_by" ]; then
sort_by=date
fi
if [ -n "$sort_by" ] ; then
case $sort_by in
date|rating|relevance|title|viewCount) ;;
*) echo "Invalid -s $sort_by" ; exit ;;
esac
fi
if [ -n "$output" ] ; then
output_id=$(playlist_id_by_name "$output")
if [ -z "$output_id" ] ; then
echo "-o $output does not exist"
exit
fi
fi
age=$(search_cache_age "$query" "$sort_by")
one_hour_ago=$((60 * 60))
if [ $age -gt $one_hour_ago ] ; then
search_execute "$query" "$sort_by"
fi
search_data "$query" "$sort_by" "$full_results" > $HOME/.yt.data
if [ -n "$output_id" ] ; then
cat $HOME/.yt.data | jq -r '.items[] | .id.videoId' | while read video_id ; do
youtubeassist -a playlist-item-add -n "$output_id" -i "$video_id" > /dev/null
done
fi
cat $HOME/.yt.data |
filter -r '.items[] | .id.videoId + "|" + .snippet.publishTime + "|" + .snippet.channelTitle + "|" + .snippet.title'
;;
*)
usage
;;
esac