-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.py
executable file
·96 lines (81 loc) · 2.78 KB
/
check.py
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
#!/usr/bin/env python3
from __future__ import unicode_literals
import yt_dlp
import json
import sys
def outputFormatter(versions):
toReturn = []
for x in versions:
toReturn.append({'ref': x})
return json.dumps(toReturn)
class PlaylistProcessor(object):
def __init__(self):
self.vidlist = []
def process_playlist_json(self, msg, format):
playlist = json.loads(msg)
ok = False
for x in playlist['entries']:
if not ok:
# Want to make sure first entry is accessible
if self.is_ok(x['id'], format):
ok = True
else:
# This entry will be skipped because it is not accessible (yet)
continue
self.vidlist.append(x['id'])
return
def count(self):
return len(self.vidlist)
def most_recent(self):
return self.vidlist[0]
def since_item(self, item):
indexOfItem = self.vidlist.index(item)
toReturn = self.vidlist[0:indexOfItem]
toReturn.reverse()
return toReturn
def is_ok(self, item, format):
if format == "":
format = "137+140"
ydl_opts = {
'skip_download': True,
'logtostderr': True,
'ignoreerrors': True,
'extract_flat': False,
'in_playlist': False,
'format': format
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl2:
ydl2.download([item])
video_exit_code = ydl2._download_retcode
return video_exit_code == 0
resource_config = json.load(sys.stdin)
print("Got Config: "+json.dumps(resource_config), file=sys.stderr)
ydl_output = PlaylistProcessor()
ydl_opts_playlist = {
'skip_download': True,
'logtostderr': True,
'ignoreerrors': True,
'extract_flat': True,
'in_playlist': True,
}
exit_code = 0
with yt_dlp.YoutubeDL(ydl_opts_playlist) as ydl:
info = ydl.extract_info(resource_config['source']['playlist'])
if ydl._download_retcode:
exit_code = ydl._download_retcode
ydl_output.process_playlist_json(json.dumps(ydl.sanitize_info(info)), resource_config['source']['format_id'])
try:
if ('version' in resource_config) and ('ref' in resource_config['version']):
#Getting newer
currentVer = resource_config['version']['ref']
print("Returning newer than: " +currentVer, file=sys.stderr)
newerItems = ydl_output.since_item(currentVer)
print(outputFormatter(newerItems))
else:
#Getting latest
print("Was not provided a version, returning latest", file=sys.stderr)
print(json.dumps([{'ref': ydl_output.most_recent()}]))
except TypeError:
#Get latest
print(outputFormatter([ydl_output.most_recent()]))
sys.exit(exit_code)