iptv/M3U8/fetch.py

130 lines
3.3 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-10-30 15:38:34 -04:00
import re
2025-08-17 10:05:09 -04:00
from pathlib import Path
2025-10-24 12:00:59 -04:00
from scrapers import (
2025-11-08 16:22:00 -05:00
fawa,
2025-11-03 04:13:00 -05:00
pixel,
2025-10-24 12:00:59 -04:00
ppv,
2025-11-06 12:02:46 -05:00
roxie,
2025-11-19 18:58:52 -05:00
shark,
2025-10-24 12:00:59 -04:00
streambtw,
streameast,
2025-11-13 12:43:55 -05:00
streamfree,
2025-10-24 12:00:59 -04:00
strmd,
tvpass,
2025-11-25 22:55:03 -05:00
vuen,
2025-10-24 12:00:59 -04:00
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-10-13 14:17:56 -04:00
EVENTS_FILE = Path(__file__).parent / "events.m3u8"
COMBINED_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-10-30 15:38:34 -04:00
pattern = re.compile(r'tvg-chno="(\d+)"')
last_chnl_num = max(map(int, pattern.findall(data)), default=0)
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-11-08 16:22:00 -05:00
asyncio.create_task(fawa.scrape(network.client)),
2025-11-16 12:59:29 -05:00
asyncio.create_task(pixel.scrape()),
2025-10-19 11:38:06 -04:00
asyncio.create_task(ppv.scrape(network.client)),
2025-11-09 10:01:14 -05:00
asyncio.create_task(roxie.scrape(network.client)),
2025-11-19 18:58:52 -05:00
asyncio.create_task(shark.scrape(network.client)),
2025-10-01 11:57:49 -04:00
asyncio.create_task(streambtw.scrape(network.client)),
2025-10-12 19:30:34 -04:00
asyncio.create_task(streameast.scrape(network.client)),
2025-11-13 12:43:55 -05:00
asyncio.create_task(streamfree.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-11-25 22:55:03 -05:00
asyncio.create_task(vuen.scrape(network.client)),
2025-11-21 10:09:30 -05: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 = (
2025-11-08 16:22:00 -05:00
fawa.urls
2025-11-03 04:13:00 -05:00
| pixel.urls
2025-10-24 12:00:59 -04:00
| ppv.urls
2025-11-06 12:02:46 -05:00
| roxie.urls
2025-11-19 18:58:52 -05:00
| shark.urls
2025-09-17 19:35:28 -04:00
| streambtw.urls
2025-10-12 19:30:34 -04:00
| streameast.urls
2025-10-10 17:14:32 -04:00
| strmd.urls
2025-11-13 12:43:55 -05:00
| streamfree.urls
2025-09-17 19:35:28 -04:00
| tvpass.urls
2025-11-25 22:55:03 -05:00
| vuen.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-10-13 14:17:56 -04:00
live_events: list[str] = []
combined_channels: list[str] = []
2025-09-13 04:42:55 -04:00
2025-10-13 14:17:56 -04:00
for i, (event, info) in enumerate(
2025-09-13 04:42:55 -04:00
sorted(additions.items()),
2025-10-13 14:17:56 -04:00
start=1,
2025-09-13 04:42:55 -04:00
):
2025-10-13 14:17:56 -04:00
extinf_all = (
f'#EXTINF:-1 tvg-chno="{tvg_chno + i}" tvg-id="{info["id"]}" '
f'tvg-name="{event}" tvg-logo="{info["logo"]}" group-title="Live Events",{event}'
)
extinf_live = (
f'#EXTINF:-1 tvg-chno="{i}" tvg-id="{info["id"]}" '
f'tvg-name="{event}" tvg-logo="{info["logo"]}" group-title="Live Events",{event}'
2025-08-17 17:01:52 -04:00
)
2025-10-13 14:17:56 -04:00
vlc_block = [
f'#EXTVLCOPT:http-referrer={info["base"]}',
f'#EXTVLCOPT:http-origin={info["base"]}',
f"#EXTVLCOPT:http-user-agent={network.UA}",
info["url"],
]
combined_channels.extend(["\n" + extinf_all, *vlc_block])
live_events.extend(["\n" + extinf_live, *vlc_block])
COMBINED_FILE.write_text(
"\n".join(base_m3u8 + combined_channels),
encoding="utf-8",
)
2025-10-15 10:53:54 -04:00
log.info(f"Base + Events saved to {COMBINED_FILE.resolve()}")
2025-10-13 14:17:56 -04:00
EVENTS_FILE.write_text(
'#EXTM3U url-tvg="https://raw.githubusercontent.com/doms9/iptv/refs/heads/default/EPG/TV.xml"\n'
+ "\n".join(live_events),
encoding="utf-8",
)
2025-09-13 04:42:55 -04:00
2025-10-15 10:53:54 -04:00
log.info(f"Events saved to {EVENTS_FILE.resolve()}")
2025-08-17 10:05:09 -04:00
if __name__ == "__main__":
2025-08-27 10:26:56 -04:00
asyncio.run(main())
2025-10-30 15:38:34 -04:00
try:
asyncio.run(network.client.aclose())
except Exception:
pass