This commit is contained in:
yumoqing 2025-10-15 16:47:47 +08:00
parent caa91cf9e4
commit 1a53694f75
3 changed files with 119 additions and 83 deletions

View File

@ -23,12 +23,77 @@ async def download(url):
clist = m3u.m3uParser(txt) clist = m3u.m3uParser(txt)
return clist return clist
async def check_if_exists(url):
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
db = DBPools()
async with db.sqlorContext(dbname) as sor:
sql = "select * from iptvchannles where url = ${url}$"
recs = await sor.sqlExe(sql, {'url': url})
if len(recs) > 0:
return True
return False
async def load_url_iptv(media_type,url, dbname): async def write_goodchannel(b):
debug(f'write badchannels({b.url})')
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
db = DBPools()
async with db.sqlorContext(dbname) as sor:
sql = """insert into iptvchannels
(
id,
tv_group,
tv_name,
logo_url,
url,
media_type,
download_date,
del_flg
)
values
(
${id}$,
${tv_group}$,
${tv_name}$,
${logo_url}$,
${url}$,
${media_type}$,
${download_date}$,
'0'
)"""
query = """select * from iptvchannels where url=${url}$"""
q = await sor.sqlExe(query,{'url', b['url']})
if len(q) == 0:
r = copy(b)
r['media_type'] = 'iptv'
r['id'] = getID()
if not r.get('tv_group'):
r['tv_group'] = r.get('group-title','')[:500]
if not r.get('tv_name'):
r['tv_name'] = r.get('name','')[:500]
if not r.get('logo_url'):
r['logo_url'] = r.get('tvg-logo',None)
if r['logo_url'] and len(r['logo_url']) > 1000:
r['logo_url'] = None
if len(r['url']) >= 1000:
return
dt = datetime.now()
r['download_date'] = '%d-%02d-%02d' % (dt.year,dt.month,dt.day)
await sor.sqlExe(sql,r)
async def load_url_iptv(url):
clist = await download(url) clist = await download(url)
if clist: debug(f'clist={clist}')
debug('%d channels' % len(goodchannels)) newchannels = []
good, bad = await test_channels(clist, if_ok=write_goodchannel) for c in clist:
b = await check_if_exists(c.url)
if not b:
newchannels.append(c)
if len(newchannels) > 0:
debug('%d new channels' % len(newchannels))
good, bad = await test_channels(newchannels, if_ok=write_goodchannel)
debug(f'{len(good)} new channels add {len(bad)} channels exists') debug(f'{len(good)} new channels add {len(bad)} channels exists')
else: else:
debug(f'{url} return None') debug(f'{url} return None')

View File

@ -1,8 +1,6 @@
import time import time
from appPublic.log import debug, exception from appPublic.log import debug, exception
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from sqlor.dbpools import DBPools
from ahserver.serverenv import ServerEnv
from aiohttp import ( from aiohttp import (
client, client,
ClientSession ClientSession
@ -14,35 +12,38 @@ from sqlor.dbpools import DBPools
async def test_channels(channels,if_failed=None, if_ok=None): async def test_channels(channels,if_failed=None, if_ok=None):
goodchannels = [] goodchannels = []
badchannels = [] badchannels = []
env = ServerEnv()
dbname = env.get_module_dbname('iptv')
db = DBPools()
async with db.sqlorContext(dbname) as sor:
for c in channels: for c in channels:
try: try:
t1 = time.time() t1 = time.time()
async with ClientSession() as sess: async with ClientSession() as sess:
async with sess.get(c.url) as resp: async with sess.get(c.url) as resp:
debug(f'{resp.status=}, {type(resp.status)=}')
if resp.status != 200: if resp.status != 200:
debug(f'{resp.status=}, {type(resp.status)=}')
badchannels.append(c) badchannels.append(c)
if if_ok:
t2 = time.time()
c['channel_delay'] = t2 - t1
await if_ok(sor, c)
else:
goodchannels.append(c)
if if_failed: if if_failed:
c['errorcode'] = resp.status c['errorcode'] = resp.status
await if_failed(sor, c) 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: except Exception as e:
debug(f'{c.url}, {e}') debug(f'{c.url}, {e}')
badchannels.append(c) badchannels.append(c)
if if_failed: if if_failed:
await if_failed(sor, c) c['errorcode'] = 600
await if_failed(c)
return goodchannels, badchannels return goodchannels, badchannels
async def write_badchannel(sor, b): 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', { await sor.C('badchannels', {
'id':getID(), 'id':getID(),
'channelid': b.id, 'channelid': b.id,
@ -50,49 +51,16 @@ async def write_badchannel(sor, b):
}) })
sql = "update iptvchannels set del_flg='1' where id=${id}$" sql = "update iptvchannels set del_flg='1' where id=${id}$"
await sor.sqlExe(sql, {'id': b.id}) await sor.sqlExe(sql, {'id': b.id})
debug(f'write badchannels finished')
async def write_goodchannel(sor, b): async def write_goodchannel(b):
sql = """insert into iptvchannels debug(f'write goodchannels({b.url})')
( env = ServerEnv()
id, dbname = env.get_module_dbname('iptv')
tv_group, db = DBPools()
tv_name, async with db.sqlorContext(dbname) as sor:
logo_url, sql = "update iptvchannels set channel_delay = ${channel_delay}$ where id=${id}$"
url, await sor.sqlExe(sql, {'id':b.id, 'channel_delay': b.channel_delay})
media_type,
download_date,
del_flg
)
values
(
${id}$,
${tv_group}$,
${tv_name}$,
${logo_url}$,
${url}$,
${media_type}$,
${download_date}$,
'0'
)"""
query = """select * from iptvchannels where url=${url}$"""
q = await sor.sqlExe(query,{'url', b['url']})
if len(q) == 0:
r = copy(b)
r['media_type'] = media_type
r['id'] = getID()
if not r.get('tv_group'):
r['tv_group'] = r.get('group-title','')[:500]
if not r.get('tv_name'):
r['tv_name'] = r.get('name','')[:500]
if not r.get('logo_url'):
r['logo_url'] = r.get('tvg-logo',None)
if r['logo_url'] and len(r['logo_url']) > 1000:
r['logo_url'] = None
if len(r['url']) >= 1000:
return
dt = datetime.now()
r['download_date'] = '%d-%02d-%02d' % (dt.year,dt.month,dt.day)
await sor.sqlExe(sql,r)
async def kickout_badchannels(): async def kickout_badchannels():
db = DBPools() db = DBPools()
@ -101,5 +69,7 @@ async def kickout_badchannels():
channels = [] channels = []
async with db.sqlorContext(dbname) as sor: async with db.sqlorContext(dbname) as sor:
channels = await sor.R('iptvchannels', {'del_flg':'0'}) channels = await sor.R('iptvchannels', {'del_flg':'0'})
good, bad2 = await test_channels(channels, if_failed=write_badchannel) good, bad2 = await test_channels(channels,
if_ok=write_goodchannel,
if_failed=write_badchannel)
debug(f'{len(good)=},{len(bad)}') debug(f'{len(good)=},{len(bad)}')

View File

@ -1,7 +1,8 @@
url = params_kw.url url = params_kw.url
debug(f'{params_kw=}, {url=}') debug(f'{params_kw=}, {url=}')
try: try:
x = await load_url_iptv('iptv', url, 'iptvdb') dbname = get_module_dbname('iptv')
x = background_reco(load_url_iptv, url)
return { return {
"widgettype":"Message", "widgettype":"Message",
"options":{ "options":{