forked from Spicadox/auto-ytarchive-raw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
378 lines (321 loc) · 17 KB
/
index.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
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
import json
import time
import os
import threading
import utils
import getm3u8
import getjson
import const
import text
import private_download
import live_download
from urllib.error import HTTPError
from addons import telegram
from addons import discord
from addons import pushalert
from addons import fcm
import traceback
if const.CALLBACK_AFTER_EXPIRY:
from callback import callback as expiry_callback
if const.CHAT_CALLBACK_AFTER_EXPIRY:
from callback import chat as chat_callback
os.chdir(os.path.dirname(os.path.realpath(__file__)))
if const.CHAT_DIR:
import chat_downloader
import getchat
global chats
chats = {}
if not os.path.exists(const.CHAT_DIR):
os.makedirs(const.CHAT_DIR)
if not os.path.exists(const.BASE_JSON_DIR):
os.makedirs(const.BASE_JSON_DIR)
if not os.path.exists(const.LOGS_DIR):
os.makedirs(const.LOGS_DIR)
with open(const.CHANNELS_JSON, encoding="utf8") as f:
CHANNELS = json.load(f)
# fetched
# {
# channel_name: {
# video_id: {
# "fregments": {
# m3u8_id: {
# file,
# create_time
# }
# }
# "skipPrivateCheck": false
# }
# }
# }
global save_lock
save_lock = threading.Lock()
def save():
global save_lock
save_lock.acquire()
with open(const.FETCHED_JSON, "w", encoding="utf8") as f:
json.dump(fetched, f, indent=4, ensure_ascii=False)
save_lock.release()
if os.path.isfile(const.FETCHED_JSON):
with open(const.FETCHED_JSON, encoding="utf8") as f:
try:
fetched = json.load(f)
except ValueError as j:
print(j)
fetched = {}
save()
else:
fetched = {}
save()
def clear_expiry():
utils.log(f" Running clear task.")
clear_queue = []
for channel_name in fetched:
for video_id in fetched[channel_name]:
for m3u8_id in fetched[channel_name][video_id]["fregments"]:
if time.time() - fetched[channel_name][video_id]["fregments"][m3u8_id]["create_time"] > const.EXPIRY_TIME:
utils.log(f"[{channel_name}] {m3u8_id} has expired. Clearing...")
clear_queue.append({
"channel_name": channel_name,
"video_id": video_id,
"m3u8_id": m3u8_id
})
for x in clear_queue:
try:
os.remove(fetched[x["channel_name"]][x["video_id"]]["fregments"][x["m3u8_id"]]["file"])
except:
utils.warn(f"[{x['channel_name']}] Error occurs when deleting {x['m3u8_id']}. Ignoring...")
fetched[x["channel_name"]][x["video_id"]]["fregments"].pop(x["m3u8_id"])
clear_queue = []
for channel_name in fetched:
for video_id in fetched[channel_name]:
if not fetched[channel_name][video_id]["fregments"]:
clear_queue.append({
"channel_name": channel_name,
"video_id": video_id
})
for x in clear_queue:
utils.log(f"[{x['channel_name']}] {x['video_id']} has all gone. Clearing...")
if const.CALLBACK_AFTER_EXPIRY:
expiry_callback.callback(fetched[x['channel_name']][x['video_id']], channel_name=x['channel_name'], video_id=x['video_id'])
if "chat" in fetched[x['channel_name']][x['video_id']]:
try:
os.remove(fetched[x['channel_name']][x['video_id']]["chat"])
except:
utils.warn(f"[{x['channel_name']}] Error occurs when deleting chat file for {x['video_id']}. Ignoring...")
fetched[x['channel_name']].pop(x['video_id'])
save()
clear_expiry()
expiry_task = utils.RepeatedTimer(const.TIME_BETWEEN_CLEAR, clear_expiry)
if const.CHAT_DIR:
def get_channel_name_by_video_id(video_id):
for channel_name in fetched:
if video_id in fetched[channel_name]:
return channel_name
return None
def clear_chat():
utils.log(f" Running chat instance clearing task.")
global chats
to_del = []
for video_id in chats:
if chats[video_id].is_finished():
if const.CHAT_CALLBACK_AFTER_EXPIRY:
channel_name = get_channel_name_by_video_id(video_id)
chat_callback.callback(chats[video_id], channel_name=channel_name, video_id=video_id)
to_del.append(video_id)
utils.log(f" Chat instance {video_id} has been queued to be cleared.")
for video_id in to_del:
chats.pop(video_id)
utils.log(f" Chat instance {video_id} has been cleared.")
chat_expiry_task = utils.RepeatedTimer(const.CHAT_TASK_CLEAR_INTERVAL, clear_chat)
while True:
try:
for channel_name, channel_id in CHANNELS.items():
# Check for privated videos
if const.ENABLE_PRIVATE_CHECK:
if channel_name in fetched:
for video_id in fetched[channel_name]:
# Might not needed, but I do dumb things.
if "skipPrivateCheck" not in fetched[channel_name][video_id]:
fetched[channel_name][video_id]["skipPrivateCheck"] = False
save()
elif fetched[channel_name][video_id]["skipPrivateCheck"]:
continue
# Can help catch HTTP 429 Too Many Requests response status code
try:
status = utils.get_video_status(video_id)
except (HTTPError, TimeoutError) as err:
print(f'[ERROR] {err}')
continue
if status is utils.PlayabilityStatus.OK:
continue
if status is utils.PlayabilityStatus.ON_LIVE:
continue
if status is utils.PlayabilityStatus.PREMIERE:
continue
if status is utils.PlayabilityStatus.LOGIN_REQUIRED and fetched[channel_name][video_id]["status"] == "LOGIN_REQUIRED":
continue
if status is utils.PlayabilityStatus.MEMBERS_ONLY and fetched[channel_name][video_id]["status"] == "MEMBERS_ONLY":
continue
if status is utils.PlayabilityStatus.OFFLINE:
continue
files = [fetched[channel_name][video_id]["fregments"][m3u8_id]["file"] for m3u8_id in fetched[channel_name][video_id]["fregments"]]
log_file_path = os.path.join(const.LOGS_DIR, f"{video_id}.html")
if os.path.isfile(log_file_path):
files.append(log_file_path)
# Don't send chat file as it can cause socket.timeout: The write operation timed out/thread exception when sending chat
# if "chat" in fetched[channel_name][video_id]:
# if os.path.isfile(fetched[channel_name][video_id]["chat"]):
# files.append(fetched[channel_name][video_id]["chat"])
# else:
# utils.warn(f" Chat file for {video_id} not found. This shouldn't happen, maybe someone stealed it...?")
message = text.get_private_check_text(status, video_id).format(video_id=video_id,
channel_name=channel_name,
channel_id=channel_id)
utils.notify(message, files)
fetched[channel_name][video_id]["skipPrivateCheck"] = True
fetched[channel_name][video_id]["status"] = status.name
save()
utils.log(f" {message}")
if const.PRIVATED_DOWNLOAD:
private_download.download(files)
live_list = []
try:
is_live_tuple = utils.is_live(channel_id)
if is_live_tuple[0]:
live_list.append(is_live_tuple)
except Exception as e:
print(e)
print("[ERROR] Unexpected Error while checking if channel is live")
continue
if const.PREMIERE_DOWNLOAD:
try:
is_premiere_tuple = utils.is_premiere(channel_id)
if is_premiere_tuple[0]:
live_list.append(is_premiere_tuple)
except Exception as e:
print(e)
print("[ERROR] Unexpected Error while checking if channel is a premiere")
continue
if len(live_list) > 0:
for is_live, live_status in live_list:
utils.log(f"[{channel_name}] On live!")
video_url = is_live
video_id = getjson.get_youtube_id(video_url)
try:
m3u8_url, require_cookie = getm3u8.get_m3u8(video_url)
except Exception as e:
print(e)
print("\n[ERROR] Unexpected error, might not be live, Skip saving json")
continue
m3u8_id = getm3u8.get_m3u8_id(m3u8_url)
if live_status == utils.PlayabilityStatus.ON_LIVE and require_cookie:
# if live stream is ON_LIVE but cookie is required then stream requires login and assumes
# member's only stream will never have the status of LOGIN_REQUIRED
live_status = utils.PlayabilityStatus.LOGIN_REQUIRED
if channel_name not in fetched:
fetched[channel_name] = {
video_id: {
"fregments": {},
"skipPrivateCheck": False,
"skipOnliveNotify": False,
"multiManifestNotify": 1,
"downloaded": False,
"status": live_status.name
}
}
if video_id not in fetched[channel_name]:
fetched[channel_name][video_id] = {
"fregments": {},
"skipPrivateCheck": False,
"skipOnliveNotify": False,
"multiManifestNotify": 1,
"downloaded": False,
"status": live_status.name
}
filepath = os.path.join(const.BASE_JSON_DIR, f"{m3u8_id}.json")
video_data = getjson.get_json(video_url, channel_id, channel_name, filepath, require_cookie)
if const.CHAT_DIR:
if video_id not in chats:
utils.log(f"[{channel_name}] Downloading chat...")
start_timestamp = video_data["metadata"]["startTimestamp"] if "startTimestamp" in video_data["metadata"] else None
chat_file = os.path.join(const.CHAT_DIR, f"{video_id}.chat")
try:
chats[video_id] = getchat.ChatArchiver(video_url, chat_file, require_cookie,
start_timestamp=start_timestamp)
fetched[channel_name][video_id]["chat"] = chat_file
except chat_downloader.errors.NoChatReplay:
print("[ERROR] Error getting chat. Maybe live already ended...?")
except chat_downloader.errors.VideoUnavailable as unavailableError:
print(f"[ERROR] {unavailableError}")
except Exception as e:
print(f"[ERROR] {e}")
if not fetched[channel_name][video_id]["skipOnliveNotify"]:
onlive_message = text.get_onlive_message(video_id=video_id, live_status=live_status).format(video_id=video_id,
channel_name=channel_name,
channel_id=channel_id)
if const.ENABLED_MODULES_ONLIVE["discord"]:
if live_status == utils.PlayabilityStatus.MEMBERS_ONLY and const.DISCORD_WEBHOOK_URL_MEMBERS:
discord.send(const.DISCORD_WEBHOOK_URL_MEMBERS, onlive_message, version=const.VERSION)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
elif live_status == utils.PlayabilityStatus.PREMIERE and const.DISCORD_WEBHOOK_URL_PREMIERE:
discord.send(const.DISCORD_WEBHOOK_URL_PREMIERE, onlive_message, version=const.VERSION)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
else:
discord.send(const.DISCORD_WEBHOOK_URL_ONLIVE, onlive_message, version=const.VERSION)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
if const.ENABLED_MODULES_ONLIVE["telegram"]:
telegram.send(const.TELEGRAM_BOT_TOKEN_ONLIVE, const.TELEGRAM_CHAT_ID_ONLIVE, onlive_message)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
if const.ENABLED_MODULES_ONLIVE["pushalert"]:
pushalert.onlive(video_data)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
if const.ENABLED_MODULES_ONLIVE["fcm"]:
fcm.onlive(video_data)
fetched[channel_name][video_id]["skipOnliveNotify"] = True
utils.log(f"[{channel_name}] Saving {m3u8_id}...")
fetched[channel_name][video_id]["fregments"][m3u8_id] = {
"file": filepath,
"create_time": time.time()
}
if len(fetched[channel_name][video_id]["fregments"]) > 1:
if "multiManifestNotify" not in fetched[channel_name][video_id]:
fetched[channel_name][video_id]["multiManifestNotify"] = 1
if len(fetched[channel_name][video_id]["fregments"]) > fetched[channel_name][video_id]["multiManifestNotify"] and text.MULTI_MANIFEST_MESSAGE:
fetched[channel_name][video_id]["multiManifestNotify"] = len(fetched[channel_name][video_id]["fregments"])
files = [fetched[channel_name][video_id]["fregments"][m3u8_id]["file"] for m3u8_id in fetched[channel_name][video_id]["fregments"]]
message = text.MULTI_MANIFEST_MESSAGE.format(video_id=video_id, channel_name=channel_name, channel_id=channel_id)
utils.notify(message, files)
utils.log(f" {message}")
# If os.system call of python index.py has an extra argument(in this case -d) to trigger download
if const.DOWNLOAD and not fetched[channel_name][video_id]["downloaded"]:
# Start the download
try:
setDownloaded = live_download.download(video_id, live_status)
fetched[channel_name][video_id]["downloaded"] = setDownloaded
except Exception as e:
print(e)
print("[ERROR] Error Live Downloading")
fetched[channel_name][video_id]["downloaded"] = False
pass
save()
else:
utils.log(f"[{channel_name}] Not on live.")
utils.log(f" Sleeping for {const.TIME_BETWEEN_CHECK} secs...")
time.sleep(const.TIME_BETWEEN_CHECK)
except KeyboardInterrupt:
utils.log(" Forced stop.")
except Exception as k:
print(f"[traceback] {traceback.format_exc()}")
utils.log(" Forced stop.")
# finally:
# try:
# utils.log(" Stopping expiry tasks")
# expiry_task.stop()
#
# if const.CHAT_DIR:
# chat_expiry_task.stop()
# for video_id in chats:
# chats[video_id].stop()
# except Exception as expiry_exception:
# print(f"[traceback] {traceback.format_exc()}")
# continue