iptv/M3U8/fetch.py

73 lines
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
2025-10-11 18:43:57 -04:00
from scrapers import fstv, livetvsx, streambtw, streamed, strmd, tvpass, watchfooty
2025-10-01 11:57:49 -04:00
from scrapers.utils import get_logger, network
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-18 17:31:45 -04:00
BASE_FILE = Path(__file__).parent / "base.m3u8"
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-08-17 17:01:52 -04:00
2025-10-01 11:57:49 -04:00
def load_base() -> 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
2025-09-17 23:52:37 -04:00
data = BASE_FILE.read_text(encoding="utf-8")
2025-08-17 10:05:09 -04:00
2025-09-17 23:52:37 -04:00
last_chnl_num = int(data.split("tvg-chno=")[-1].split('"')[1])
2025-08-17 10:05:09 -04:00
2025-09-17 23:52:37 -04:00
return data.splitlines(), last_chnl_num
2025-08-17 10:05:09 -04:00
2025-08-27 10:26:56 -04:00
async def main() -> None:
2025-10-01 11:57:49 -04:00
base_m3u8, tvg_chno = load_base()
2025-09-17 23:52:37 -04:00
2025-09-01 19:12:49 -04:00
tasks = [
2025-10-10 23:00:46 -04:00
asyncio.create_task(fstv.scrape(network.client)),
2025-10-11 11:23:28 -04:00
# asyncio.create_task(livetvsx.scrape(network.client)),
2025-10-01 11:57:49 -04:00
asyncio.create_task(streambtw.scrape(network.client)),
2025-10-11 12:01:23 -04:00
asyncio.create_task(streamed.scrape(network.client)),
2025-10-10 17:14:32 -04:00
asyncio.create_task(strmd.scrape(network.client)),
2025-10-01 11:57:49 -04:00
asyncio.create_task(tvpass.scrape(network.client)),
2025-10-11 18:43:57 -04:00
asyncio.create_task(watchfooty.scrape(network.client)),
2025-09-01 19:12:49 -04:00
]
2025-09-01 14:05:33 -04:00
2025-10-06 19:53:10 -04:00
await asyncio.gather(*tasks)
2025-08-17 10:05:09 -04:00
2025-09-17 19:35:28 -04:00
additions = (
fstv.urls
| livetvsx.urls
| streambtw.urls
2025-09-30 17:27:42 -04:00
| streamed.urls
2025-10-10 17:14:32 -04:00
| strmd.urls
2025-09-17 19:35:28 -04:00
| tvpass.urls
2025-10-11 18:43:57 -04:00
| watchfooty.urls
2025-09-17 19:35:28 -04:00
)
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(
(
2025-10-01 12:37:19 -04:00
f'\n#EXTINF:-1 tvg-chno="{chnl_num}" tvg-id="{info["id"]}" tvg-name="{event}" tvg-logo="{info["logo"]}" group-title="Live Events",{event}',
2025-09-13 04:42:55 -04:00
f'#EXTVLCOPT:http-referrer={info["base"]}',
f'#EXTVLCOPT:http-origin={info["base"]}',
2025-10-01 11:57:49 -04:00
f"#EXTVLCOPT:http-user-agent={network.UA}",
2025-09-13 04:42:55 -04:00
info["url"],
)
2025-08-17 17:01:52 -04:00
)
2025-09-17 23:52:37 -04:00
M3U8_FILE.write_text("\n".join(base_m3u8 + live_events), encoding="utf-8")
2025-09-13 04:42:55 -04:00
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())