iptv/iptv/m3u8test.py
2025-10-15 16:47:47 +08:00

76 lines
2.1 KiB
Python

import time
from appPublic.log import debug, exception
from appPublic.uniqueID import getID
from aiohttp import (
client,
ClientSession
)
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import DBPools
async def test_channels(channels,if_failed=None, if_ok=None):
goodchannels = []
badchannels = []
for c in channels:
try:
t1 = time.time()
async with ClientSession() as sess:
async with sess.get(c.url) as resp:
if resp.status != 200:
debug(f'{resp.status=}, {type(resp.status)=}')
badchannels.append(c)
if if_failed:
c['errorcode'] = resp.status
await if_failed(c)
else:
goodchannels.append(c)
if if_ok:
debug(f'write good channels')
t2 = time.time()
c['channel_delay'] = t2 - t1
await if_ok(c)
except Exception as e:
debug(f'{c.url}, {e}')
badchannels.append(c)
if if_failed:
c['errorcode'] = 600
await if_failed(c)
return goodchannels, badchannels
async def write_badchannel(b):
debug(f'write badchannels({b.url})')
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
db = DBPools()
async with db.sqlorContext(dbname) as sor:
await sor.C('badchannels', {
'id':getID(),
'channelid': b.id,
'errorcode': b.errorcode
})
sql = "update iptvchannels set del_flg='1' where id=${id}$"
await sor.sqlExe(sql, {'id': b.id})
debug(f'write badchannels finished')
async def write_goodchannel(b):
debug(f'write goodchannels({b.url})')
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
db = DBPools()
async with db.sqlorContext(dbname) as sor:
sql = "update iptvchannels set channel_delay = ${channel_delay}$ where id=${id}$"
await sor.sqlExe(sql, {'id':b.id, 'channel_delay': b.channel_delay})
async def kickout_badchannels():
db = DBPools()
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
channels = []
async with db.sqlorContext(dbname) as sor:
channels = await sor.R('iptvchannels', {'del_flg':'0'})
good, bad2 = await test_channels(channels,
if_ok=write_goodchannel,
if_failed=write_badchannel)
debug(f'{len(good)=},{len(bad)}')