-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt-playlist-dl.py
executable file
·151 lines (124 loc) · 4.23 KB
/
yt-playlist-dl.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
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
#!/usr/bin/env python
from __future__ import unicode_literals
import youtube_dl
import json
import sys
import os
import logging
LOGNAME = "yt-playlists.log"
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(logging.DEBUG)
fileHandler = logging.FileHandler(LOGNAME)
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
LOCK = "yt-playlists.lock"
CONFIG = "yt-playlists.json"
config = None
dl_success = None
SAMPLE_CONFIG = """
{
"options": {
"format": "best"
},
"playlist": {
"gu": {
"last": 55,
"match": "EP0?%d",
"url": "https://www.youtube.com/playlist?list=PLpOa-OrneXm1-d7KBR7qff7ETu3qnRp_6"
},
"san": {
"last": 16,
"match": "EP0?%d",
"url": "https://www.youtube.com/playlist?list=PLpOa-OrneXm34lESV2HJzCIFHA3tBrI5s"
}
}
}
"""
def lock():
with open(LOCK, 'w') as f:
# f is unused
logging.info("yt-playlists locked")
def clean_exit():
if os.path.isfile(LOCK):
os.remove(LOCK)
logging.info("yt-playlists unlocked")
sys.exit()
def load_config():
global config
try:
with open(CONFIG, 'r') as f:
config = json.load(f)
except:
logging.error("Unable to load config file (%s). Exiting...", CONFIG)
logging.debug("Sample config file:\n%s", SAMPLE_CONFIG)
clean_exit()
def save_config():
try:
os.rename(CONFIG, CONFIG+".bak") # in case next line fails and we end up with an empty file
with open(CONFIG, 'w') as f:
f.write(json.dumps(config, indent=4, sort_keys=True))
os.remove(CONFIG+".bak")
except:
logging.error("Unable to write to config file (%s):\n", CONFIG)
logging.debug(config)
os.rename(CONFIG+".bak", CONFIG)
logging.debug("%s has been restored.", CONFIG)
clean_exit()
def download_eps(name=None, url=None, ep=None, match=None):
""" Search and download given episode, and later ones if any.
Return last downloaded episode number, ep-1 if ep is not found.
name: str, video name prefix
url: str, YouTube playlist URL
ep: int, start episode number
match: str, regex pattern to match
"""
global dl_success
dl_success = False
def finish_hook(d):
global dl_success
if d["status"] == "finished":
dl_success = True
options = config["options"].copy() # don't want to save the runtime options below in config
options["matchtitle"] = match % ep
options["outtmpl"] = "%s_%s%d.mp4" % (name, '0' if ep < 10 else '', ep)
options["progress_hooks"] = [finish_hook]
with youtube_dl.YoutubeDL(options) as ytdl:
logging.info("Started downloading %s", options["outtmpl"])
try:
ytdl.download([url])
except:
logging.info("Crashed!")
return ep-1
if dl_success: # ytdl successfully downloaded specified video
logging.info("Finished downloading %s", options["outtmpl"])
return download_eps(name=name, url=url, ep=ep+1, match=match) # try next episode
else:
logging.info("Unable to download %s", options["outtmpl"])
return ep-1 # which is last downloaded episode number
if __name__=='__main__':
if len(sys.argv) == 2:
# pass any argument for sample json config
print CONFIG
print SAMPLE_CONFIG
sys.exit()
if os.path.isfile(LOCK):
logging.error("Another process is running in this directory. Exiting...")
sys.exit()
else:
lock()
load_config()
playlists = config["playlist"]
for pl in playlists:
last = playlists[pl]['last']
latest = download_eps(name=pl, url=playlists[pl]['url'], ep=last+1, match=playlists[pl]['match'])
if latest > last:
logging.info("%s is now up to EP%d", pl, latest)
playlists[pl]['last'] = latest
save_config()
else:
logging.info("%s has no new video", pl)
clean_exit()