iptv/M3U8/fetch.py
2025-09-08 11:49:52 -04:00

75 lines
2 KiB
Python

#!/usr/bin/env python3
import asyncio
from pathlib import Path
import httpx
from scrapers import livetvsx, ppv, streambtw, tvpass
from scrapers.utils import get_logger
log = get_logger(__name__)
BASE_URL = "https://s.id/ePwXT"
M3U8_FILE = Path(__file__).parent / "TV.m3u8"
CLIENT = httpx.AsyncClient(
timeout=5,
follow_redirects=True,
headers={
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0"
},
)
async def vanilla_fetch() -> tuple[list[str], int]:
log.info("Fetching base M3U8")
try:
r = await CLIENT.get(BASE_URL)
r.raise_for_status()
except Exception as e:
log.error(f'Failed to fetch "{BASE_URL}"\n{e}')
raise SystemExit(e) from e
last_chnl_num = int(r.text.split("tvg-chno=")[-1].split('"')[1])
return r.text.splitlines()[1:], last_chnl_num
async def main() -> None:
tasks = [
asyncio.create_task(livetvsx.main(CLIENT)),
asyncio.create_task(ppv.main(CLIENT)),
asyncio.create_task(streambtw.main(CLIENT)),
asyncio.create_task(tvpass.main(CLIENT)),
vanilla_fetch(),
]
results = await asyncio.gather(*tasks)
base_m3u8, tvg_chno = results[-1]
additions = livetvsx.urls | ppv.urls | streambtw.urls | tvpass.urls
lines = [
f'#EXTINF:-1 tvg-chno="{chnl_num}" tvg-id="(N/A)" tvg-name="{event}" tvg-logo="{info["logo"]}" group-title="Live Events",{event}\n{info["url"]}'
for chnl_num, (event, info) in enumerate(
sorted(additions.items()),
start=tvg_chno + 1,
)
]
M3U8_FILE.write_text(
'#EXTM3U url-tvg="https://raw.githubusercontent.com/doms9/iptv/refs/heads/default/EPG/TV.xml"\n'
+ "\n".join(base_m3u8)
+ "\n"
+ "\n".join(lines)
+ "\n",
encoding="utf-8",
)
log.info(f"M3U8 saved to {M3U8_FILE.name}")
if __name__ == "__main__":
asyncio.run(main())