iptv/M3U8/fetch.py

73 lines
1.7 KiB
Python
Raw Normal View History

2025-08-17 10:05:09 -04:00
#!/usr/bin/env python3
2025-08-27 10:26:56 -04:00
import asyncio
2025-08-17 10:05:09 -04:00
from pathlib import Path
import httpx
2025-09-01 14:05:33 -04:00
from scrape import ace, logger, tvpass # , fstv
2025-08-30 16:45:19 -04:00
log = logger.get_logger(__name__)
2025-08-17 10:05:09 -04:00
2025-08-19 12:15:56 -04:00
base_url = "https://s.id/ePwXT"
2025-08-17 10:05:09 -04:00
2025-08-28 12:18:30 -04:00
m3u8_file = Path(__file__).parent / "TV.m3u8"
2025-08-27 10:26:56 -04:00
client = httpx.AsyncClient(
2025-08-17 17:01:52 -04:00
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"
},
)
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
async def vanilla_fetch() -> tuple[list[str], int]:
2025-08-30 16:45:19 -04:00
log.info("Fetching base M3U8")
2025-08-17 10:05:09 -04:00
try:
2025-08-27 10:26:56 -04:00
r = await client.get(base_url)
2025-08-17 10:05:09 -04:00
r.raise_for_status()
except Exception as e:
2025-08-30 16:45:19 -04:00
log.error(f'Failed to fetch "{base_url}"\n{e}')
raise SystemExit(e) from e
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
d = r.text.splitlines()[1:]
2025-08-17 10:05:09 -04:00
2025-08-31 08:29:01 -04:00
last_chnl_num = int(r.text.split("tvg-chno=")[-1].split('"')[1])
2025-08-17 10:05:09 -04:00
2025-08-31 08:29:01 -04:00
return d, last_chnl_num
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
async def main() -> None:
await tvpass.main(client)
2025-08-17 10:05:09 -04:00
2025-09-01 14:05:33 -04:00
await ace.main(client)
2025-08-31 08:29:01 -04:00
# await fstv.main(client)
2025-08-17 10:05:09 -04:00
2025-08-31 08:29:01 -04:00
base_m3u8, tvg_chno = await vanilla_fetch()
2025-08-17 10:05:09 -04:00
2025-09-01 14:05:33 -04:00
additions = tvpass.urls | ace.urls # | fstv.urls
2025-08-17 17:01:52 -04:00
2025-08-27 10:26:56 -04:00
lines = [
2025-08-31 08:29:01 -04:00
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(
2025-08-27 10:26:56 -04:00
sorted(additions.items()),
2025-08-31 08:29:01 -04:00
start=tvg_chno + 1,
2025-08-17 17:01:52 -04:00
)
2025-08-27 10:26:56 -04:00
]
2025-08-17 17:01:52 -04:00
2025-08-17 10:05:09 -04:00
m3u8_file.write_text(
'#EXTM3U url-tvg="https://raw.githubusercontent.com/doms9/iptv/refs/heads/default/EPG/TV.xml"\n'
+ "\n".join(base_m3u8)
2025-08-17 17:28:06 -04:00
+ "\n"
2025-08-27 10:26:56 -04:00
+ "\n".join(lines)
2025-08-17 10:05:09 -04:00
+ "\n",
encoding="utf-8",
)
2025-08-30 16:45:19 -04:00
log.info(f"M3U8 saved to {m3u8_file.name}")
2025-08-17 10:05:09 -04:00
if __name__ == "__main__":
2025-08-27 10:26:56 -04:00
asyncio.run(main())