-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.py
284 lines (236 loc) · 11.6 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
import json
import time
import os
import threading
import urllib
import utils
import getm3u8
import getjson
import const
import text
from addons import telegram
from addons import discord
from addons import pushalert
from addons import fcm
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 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()
try:
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:
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
status = utils.get_video_status(video_id)
if status is utils.PlayabilityStatus.OK:
continue
if status is utils.PlayabilityStatus.ON_LIVE:
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)
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
save()
utils.log(f" {message}")
try:
is_live = utils.is_live(channel_id)
except urllib.error.HTTPError as e:
utils.warn(f" got HTTPError when checking live status for channel \"{channel_id}\". {e}")
is_live = False
if is_live:
utils.log(f"[{channel_name}] On live!")
video_url = is_live
video_id = getjson.get_youtube_id(video_url)
m3u8_url = getm3u8.get_m3u8(video_url)
m3u8_id = getm3u8.get_m3u8_id(m3u8_url)
if channel_name not in fetched:
fetched[channel_name] = {
video_id: {
"fregments": {},
"skipPrivateCheck": False,
"skipOnliveNotify": False,
"multiManifestNotify": 1
}
}
if video_id not in fetched[channel_name]:
fetched[channel_name][video_id] = {
"fregments": {},
"skipPrivateCheck": False,
"skipOnliveNotify": False,
"multiManifestNotify": 1
}
filepath = os.path.join(const.BASE_JSON_DIR, f"{m3u8_id}.json")
video_data = getjson.get_json(video_url, filepath)
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")
chats[video_id] = getchat.ChatArchiver(video_url, chat_file, start_timestamp=start_timestamp)
fetched[channel_name][video_id]["chat"] = chat_file
if not fetched[channel_name][video_id]["skipOnliveNotify"]:
onlive_message = text.get_onlive_message(video_id=video_id).format(video_id=video_id, channel_name=channel_name, channel_id=channel_id)
if const.ENABLED_MODULES_ONLIVE["discord"]:
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}")
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.")
finally:
expiry_task.stop()
if const.CHAT_DIR:
chat_expiry_task.stop()
for video_id in chats:
chats[video_id].stop()