forked from 239144498/Streaming-Media-Server-Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutile.py
298 lines (268 loc) · 10.4 KB
/
utile.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
#!/usr/bin python3
# -*- coding: utf-8 -*-
# @Author: Naihe
# @Email: [email protected]
# @Software: Streaming-Media-Server-Pro
import asyncio
import datetime
import re
import time
from urllib.parse import quote
from loguru import logger
from threading import Thread
from apscheduler.triggers.interval import IntervalTrigger
from apscheduler.schedulers.background import BlockingScheduler, BackgroundScheduler
from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor
from app.common.endecrypt import get4gtvurl
from app.common.gitrepo import agit
from app.common.generateEpg import postask, generateprog
from app.common.tools import genftlive, now_time, solvelive, generate_url, gdata
from app.modules.DBtools import redisState, cur, DB
from app.modules.request import request
from app.settings import repoaccess_token, xmlaccess_token, xmlowner, xmlrepo, PATH, localhost, repoState, HD, \
repoowner, idata, downurls, vbuffer, downchoose
class container:
def __init__(self):
self.repo = None
self.para = {}
self.filename = dict() # -1->redis | 0->downloading | 1->completed
self.owner = repoowner
if redisState:
# 读取redis数据到内存
keys = cur.keys()
_ = []
for k in keys: # 排除非电视节目
if "4gtv" in str(k) or "litv" in str(k):
_.append(k)
for key, value in zip(_, cur.mget(_)):
_ = eval(value)
if len(_) < 3:
continue
self.updatelocal(key, _)
logger.success("init final")
def updateonline(self, fid, hd):
url = get4gtvurl(fid, idata[fid]['nid'], hd)
try:
last = int(re.findall(r"expires.=(\d+)", url).pop())
start, seq, gap = genftlive(url)
except:
idata.pop(fid)
logger.warning(f"删除 {fid}")
return
if redisState:
cur.setex(fid, last - now_time(), str([url, last, start, seq, gap]))
self.para[fid] = {
"url": url,
"last": last,
"start": start,
"seq": seq,
"gap": gap
}
def updatelocal(self, fid, _):
self.para[fid] = {
"url": _[0],
"last": _[1],
"start": _[2],
"seq": _[3],
"gap": _[4]
}
def check(self, fid, hd):
"""
处理参数
:param fid:
:param hd:
:return:
"""
if not self.para.get(fid) or self.para.get(fid)['last'] - now_time() < 0: # 本地找
if redisState:
_temp = cur.get(fid)
if not _temp or eval(_temp)[1] - now_time() < 0: # redis找
self.updateonline(fid, hd)
else: # 找到放进内存
_ = eval(_temp)
self.updatelocal(fid, _)
else:
self.updateonline(fid, hd)
def generalfun(self, fid, hd):
"""
通用生成参数
:param fid:
:param hd:
:return:
"""
data = self.para.get(fid)
if "4gtv-4gtv" in fid or "litv-ftv10" in fid or "litv-longturn17" == fid or "litv-longturn18" == fid:
url = idata[fid][hd]
now = now_time()
seq = round((now - data['start']) / idata[fid]['x']) - 1
begin = (seq + data['seq']) * idata[fid]['x']
return data["gap"], (begin - idata[fid]['x1']) // idata[fid]['x'], url, begin
elif "4gtv-live" in fid:
token = re.findall(r"(token1=.*&expires1=\d+)&", data['url']).pop()
url = idata[fid]['url'] + "?" + token
now = now_time()
seq = solvelive(now, data['start'], data['seq'], idata[fid]['x']) - 1
return data["gap"], seq, url, 0
else:
url = idata[fid][hd]
now = now_time()
seq = solvelive(now, data['start'], data['seq'], idata[fid]['x']) - 1
return data["gap"], seq, url, 0
def generatem3u8(self, host, fid, hd):
self.check(fid, hd)
gap, seq, url, begin = self.generalfun(fid, hd)
yield f"""#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:{gap}
#EXT-X-ALLOW-CACHE:YES
#EXT-X-MEDIA-SEQUENCE:{seq}
#EXT-X-INDEPENDENT-SEGMENTS"""
for num1 in range(5):
yield f"\n#EXTINF:{idata[fid]['gap']}" \
+ "\n" + generate_url(fid, host, hd, begin + (num1 * idata[fid]['x']), seq + num1, url)
def new_generatem3u8(self, host, fid, hd, background_tasks):
self.check(fid, hd)
gap, seq, url, begin = self.generalfun(fid, hd)
if downchoose == "online":
background_tasks.add_task(backtaskonline, url, fid, seq, hd, begin, host)
elif downchoose == "local":
background_tasks.add_task(backtasklocal, url, fid, seq, hd, begin, host)
yield f"""#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:{gap}
#EXT-X-MEDIA-SEQUENCE:{seq}
#EXT-X-INDEPENDENT-SEGMENTS"""
tsname = fid + str(seq) + ".ts"
if tsname in self.filename and self.filename.get(tsname) == 1:
for num1 in range(vbuffer):
url = "\n" + localhost + f"/call.ts?fid={fid}&seq={str(seq + num1)}&hd={hd}"
yield f"\n#EXTINF:{idata[fid]['gap']}" + url
else:
for num1 in range(1):
url = "\n" + localhost + f"/call.ts?fid={fid}&seq={str(seq + num1)}&hd={hd}"
yield f"\n#EXTINF:{idata[fid]['gap']}" + url
def geturl(self, fid, hd):
self.check(fid, hd)
if "4gtv-live" not in fid:
return re.sub(r"(\w+\.m3u8)", "channel3.m3u8", self.para[fid]['url'])
else:
return re.sub(r"(\w+\.m3u8)", HD[hd], self.para[fid]['url'])
get = container()
def call_get(url, tsname):
with request.get(url) as res:
get.filename.update({tsname: 1})
def backtaskonline(url, fid, seq, hd, begin, host):
threads = []
# 分布式下载,改成你的链接,看不懂就去看我发布的教程
# urlset = ["https://www.example1.com/url3?url=", "https://www.example2.com/url3?url=",
# "https://www.example3.com/url3?url=", "https://www.example4.com/url3?url=",
# "https://www.example5.com/url3?url="]
urlset = downurls
for i in range(0, vbuffer):
tsname = fid + str(seq + i) + ".ts"
# .ts已下载或正在下载
if tsname in get.filename:
continue
get.filename.update({tsname: 0})
herf = generate_url(fid, host, hd, begin + (i * idata[fid]['x']), seq + i, url)
x = urlset.pop() + quote(herf) + f"&filepath={tsname}"
t = Thread(target=call_get, args=(x, tsname))
threads.append(t)
for i in range(len(threads)):
threads[i].start()
time.sleep(1 + i * 0.1)
# time.sleep(1)
for t in threads:
t.join()
def backtasklocal(url, fid, seq, hd, begin, host):
threads = []
# 本地多线程下载
for i in range(0, vbuffer):
tsname = fid + str(seq + i) + ".ts"
# .ts已下载或正在下载
if tsname in get.filename:
continue
get.filename.update({tsname: 0})
herf = generate_url(fid, host, hd, begin + (i * idata[fid]['x']), seq + i, url)
t = Thread(target=downvideo, args=(herf, tsname))
threads.append(t)
for i in range(len(threads)):
threads[i].start()
time.sleep(1.2 + i * 0.1)
for t in threads:
t.join()
def downvideo(url: str, filepath: str):
"""
本地下载存放到数据库
:param url:
:param filepath:
:return:
"""
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0",
"Accept-Encoding": "gzip, deflate, br",
}
a = time.time()
repo = str(datetime.date.today())
with request.get(url=url, headers=header) as res:
status = res.status_code
content = res.content
b = time.time()
sql = "insert into video(vname, vcontent, vsize) values(%s, %s, %s)"
a1 = DB(0).execute(sql, (filepath, content, len(content))) # 执行sql语句
get.filename.update({filepath: 1})
c = time.time()
return {
"总时长": round(c - a, 2),
"请求": round(b - a, 2),
"请求状态": status,
"上传": round(c - b, 2),
"上传状态": a1,
"字长": len(content),
"repo": repo
}
def gotask():
get.filename.clear()
if repoState:
import platform
if "Windows" in platform.platform():
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(postask())
else:
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
loop = asyncio.get_event_loop()
task = asyncio.ensure_future(postask())
loop.run_until_complete(asyncio.wait([task]))
content = generateprog(gdata)
filepath = "4gtvchannel.xml"
agit(xmlaccess_token).update_repo_file(xmlowner, xmlrepo, filepath, content)
with open(PATH / "assets/EPG.xml", "wb") as f:
f.write(content)
logger.success("今日任务完成")
def sqltask():
# 保留最近100条缓存,避免长时间运行内存溢出
keys = list(get.filename)
keys.reverse()
_ = {}
if len(keys) > 100:
for i in range(len(keys)):
if i < 100:
_.update({keys[i]: get.filename.get(keys[i])})
get.filename = _
logger.success("删除完成")
def everyday(t=2):
executors = {
'default': ThreadPoolExecutor(5), # 名称为“default ”的ThreadPoolExecutor,最大线程20个
'processpool': ProcessPoolExecutor(2) # 名称“processpool”的ProcessPoolExecutor,最大进程5个
}
job_defaults = {
'coalesce': False, # 默认为新任务关闭合并模式()
'max_instances': 3 # 设置新任务的默认最大实例数为3
}
scheduler = BlockingScheduler(timezone='Asia/Shanghai', executors=executors, job_defaults=job_defaults)
scheduler.add_job(gotask, 'cron', max_instances=10, day_of_week='0-6', hour=t, minute=00, second=00,
misfire_grace_time=120)
scheduler.add_job(func=sqltask, max_instances=10, trigger=IntervalTrigger(minutes=59), misfire_grace_time=120)
logger.info(scheduler.get_jobs())
scheduler.start()