iptv/M3U8/fetch.py

91 lines
2.2 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-13 04:42:55 -04:00
from scrapers import fstv, livetvsx, ppv, streambtw, streameast, tvpass
2025-09-11 14:55:53 -04:00
from scrapers.utils import UA, get_logger
2025-08-30 16:45:19 -04:00
2025-09-03 15:00:17 -04:00
log = get_logger(__name__)
2025-08-17 10:05:09 -04:00
2025-09-02 18:06:35 -04:00
BASE_URL = "https://s.id/ePwXT"
2025-08-17 10:05:09 -04:00
2025-09-02 18:06:35 -04:00
M3U8_FILE = Path(__file__).parent / "TV.m3u8"
2025-08-28 12:18:30 -04:00
2025-09-02 18:06:35 -04:00
CLIENT = httpx.AsyncClient(
2025-08-17 17:01:52 -04:00
timeout=5,
follow_redirects=True,
2025-09-11 14:55:53 -04:00
headers={"User-Agent": UA},
2025-08-17 17:01:52 -04:00
)
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-09-02 18:06:35 -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-09-02 18:06:35 -04:00
log.error(f'Failed to fetch "{BASE_URL}"\n{e}')
2025-08-30 16:45:19 -04:00
raise SystemExit(e) from e
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-09-08 11:49:52 -04:00
return r.text.splitlines()[1:], last_chnl_num
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
async def main() -> None:
2025-09-01 19:12:49 -04:00
tasks = [
2025-09-13 04:42:55 -04:00
asyncio.create_task(fstv.main(CLIENT)),
2025-09-03 00:00:22 -04:00
asyncio.create_task(livetvsx.main(CLIENT)),
2025-09-03 15:00:17 -04:00
asyncio.create_task(ppv.main(CLIENT)),
2025-09-13 04:42:55 -04:00
asyncio.create_task(streambtw.main(CLIENT)),
2025-09-11 14:55:53 -04:00
asyncio.create_task(streameast.main(CLIENT)),
2025-09-03 00:00:22 -04:00
asyncio.create_task(tvpass.main(CLIENT)),
vanilla_fetch(),
2025-09-01 19:12:49 -04:00
]
2025-09-01 14:05:33 -04:00
2025-09-03 00:00:22 -04:00
results = await asyncio.gather(*tasks)
2025-08-17 10:05:09 -04:00
2025-09-03 00:00:22 -04:00
base_m3u8, tvg_chno = results[-1]
2025-08-17 10:05:09 -04:00
2025-09-13 04:42:55 -04:00
additions = (
fstv.urls
| livetvsx.urls
| ppv.urls
| streambtw.urls
| streameast.urls
| tvpass.urls
)
2025-08-17 17:01:52 -04:00
2025-09-13 04:42:55 -04:00
live_events = []
for chnl_num, (event, info) in enumerate(
sorted(additions.items()),
start=tvg_chno + 1,
):
live_events.extend(
(
f'#EXTINF:-1 tvg-chno="{chnl_num}" tvg-id="(N/A)" tvg-name="{event}" tvg-logo="{info["logo"]}" group-title="Live Events",{event}',
f'#EXTVLCOPT:http-referrer={info["base"]}',
f'#EXTVLCOPT:http-origin={info["base"]}',
info["url"],
)
2025-08-17 17:01:52 -04:00
)
2025-09-13 04:42:55 -04:00
m3u8_content = "\n".join(
[
'#EXTM3U url-tvg="https://raw.githubusercontent.com/doms9/iptv/refs/heads/default/EPG/TV.xml"'
]
+ base_m3u8
+ live_events
2025-08-17 10:05:09 -04:00
)
2025-09-13 04:42:55 -04:00
M3U8_FILE.write_text(m3u8_content, encoding="utf-8")
2025-09-02 18:06:35 -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())