mirror of
https://github.com/doms9/iptv.git
synced 2026-03-07 11:18:25 +01:00
update M3U8
This commit is contained in:
parent
db32314839
commit
f7325f1847
6 changed files with 1156 additions and 1184 deletions
|
|
@ -1,151 +0,0 @@
|
|||
from functools import partial
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from playwright.async_api import Browser
|
||||
|
||||
from .utils import Cache, Time, get_logger, leagues, network
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
urls: dict[str, dict[str, str | float]] = {}
|
||||
|
||||
TAG = "CDNTV"
|
||||
|
||||
CACHE_FILE = Cache(TAG, exp=10_800)
|
||||
|
||||
API_FILE = Cache(f"{TAG}-api", exp=19_800)
|
||||
|
||||
API_URL = "https://api.cdn-live.tv"
|
||||
|
||||
|
||||
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
|
||||
now = Time.clean(Time.now())
|
||||
|
||||
events = []
|
||||
|
||||
if not (api_data := API_FILE.load(per_entry=False)):
|
||||
log.info("Refreshing API cache")
|
||||
|
||||
if r := await network.request(
|
||||
urljoin(API_URL, "api/v1/events/sports"),
|
||||
log=log,
|
||||
params={"user": "cdnlivetv", "plan": "free"},
|
||||
):
|
||||
api_data = r.json().get("cdn-live-tv", {"timestamp": now.timestamp()})
|
||||
|
||||
API_FILE.write(api_data)
|
||||
|
||||
start_dt = now.delta(minutes=-30)
|
||||
end_dt = now.delta(minutes=30)
|
||||
|
||||
sports = [key for key in api_data.keys() if not key.islower()]
|
||||
|
||||
for sport in sports:
|
||||
event_info = api_data[sport]
|
||||
|
||||
for event in event_info:
|
||||
t1, t2 = event["awayTeam"], event["homeTeam"]
|
||||
|
||||
name = f"{t1} vs {t2}"
|
||||
|
||||
league = event["tournament"]
|
||||
|
||||
if f"[{league}] {name} ({TAG})" in cached_keys:
|
||||
continue
|
||||
|
||||
event_dt = Time.from_str(event["start"], timezone="UTC")
|
||||
|
||||
if not start_dt <= event_dt <= end_dt:
|
||||
continue
|
||||
|
||||
if not (channels := event.get("channels")):
|
||||
continue
|
||||
|
||||
event_links: list[str] = [channel["url"] for channel in channels]
|
||||
|
||||
# if not (
|
||||
# link := (
|
||||
# event_links[0]
|
||||
# if len(event_links) == 1
|
||||
# else await network.get_base(event_links)
|
||||
# )
|
||||
# ):
|
||||
# continue
|
||||
|
||||
link = event_links[0]
|
||||
|
||||
events.append(
|
||||
{
|
||||
"sport": league,
|
||||
"event": name,
|
||||
"link": link,
|
||||
"timestamp": event_dt.timestamp(),
|
||||
}
|
||||
)
|
||||
|
||||
return events
|
||||
|
||||
|
||||
async def scrape(browser: Browser) -> None:
|
||||
cached_urls = CACHE_FILE.load()
|
||||
|
||||
cached_count = len(cached_urls)
|
||||
|
||||
urls.update(cached_urls)
|
||||
|
||||
log.info(f"Loaded {cached_count} event(s) from cache")
|
||||
|
||||
log.info(f'Scraping from "{API_URL}"')
|
||||
|
||||
events = await get_events(cached_urls.keys())
|
||||
|
||||
if events:
|
||||
log.info(f"Processing {len(events)} new URL(s)")
|
||||
|
||||
async with network.event_context(browser) as context:
|
||||
for i, ev in enumerate(events, start=1):
|
||||
async with network.event_page(context) as page:
|
||||
handler = partial(
|
||||
network.process_event,
|
||||
url=(link := ev["link"]),
|
||||
url_num=i,
|
||||
page=page,
|
||||
log=log,
|
||||
)
|
||||
|
||||
url = await network.safe_process(
|
||||
handler,
|
||||
url_num=i,
|
||||
semaphore=network.PW_S,
|
||||
log=log,
|
||||
)
|
||||
|
||||
if url:
|
||||
sport, event, ts = (
|
||||
ev["sport"],
|
||||
ev["event"],
|
||||
ev["timestamp"],
|
||||
)
|
||||
|
||||
key = f"[{sport}] {event} ({TAG})"
|
||||
|
||||
tvg_id, logo = leagues.get_tvg_info(sport, event)
|
||||
|
||||
entry = {
|
||||
"url": url,
|
||||
"logo": logo,
|
||||
"base": link,
|
||||
"timestamp": ts,
|
||||
"id": tvg_id or "Live.Event.us",
|
||||
"link": link,
|
||||
}
|
||||
|
||||
urls[key] = cached_urls[key] = entry
|
||||
|
||||
if new_count := len(cached_urls) - cached_count:
|
||||
log.info(f"Collected and cached {new_count} new event(s)")
|
||||
|
||||
else:
|
||||
log.info("No new events found")
|
||||
|
||||
CACHE_FILE.write(cached_urls)
|
||||
|
|
@ -1,147 +0,0 @@
|
|||
import base64
|
||||
import re
|
||||
from functools import partial
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from .utils import Cache, Time, get_logger, leagues, network
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
urls: dict[str, dict[str, str | float]] = {}
|
||||
|
||||
TAG = "STRMBTW"
|
||||
|
||||
CACHE_FILE = Cache(TAG, exp=3_600)
|
||||
|
||||
API_FILE = Cache(f"{TAG}-api", exp=19_800)
|
||||
|
||||
BASE_URL = "https://streambtw.com"
|
||||
|
||||
|
||||
def fix_league(s: str) -> str:
|
||||
pattern = re.compile(r"^\w*-\w*", re.I)
|
||||
|
||||
return " ".join(s.split("-")) if pattern.search(s) else s
|
||||
|
||||
|
||||
async def process_event(url: str, url_num: int) -> str | None:
|
||||
if not (html_data := await network.request(url, log=log)):
|
||||
return
|
||||
|
||||
valid_m3u8 = re.compile(r'var\s+(\w+)\s*=\s*"([^"]*)"', re.I)
|
||||
|
||||
if not (match := valid_m3u8.search(html_data.text)):
|
||||
log.info(f"URL {url_num}) No M3U8 found")
|
||||
return
|
||||
|
||||
stream_link: str = match[2]
|
||||
|
||||
if not stream_link.startswith("http"):
|
||||
stream_link = base64.b64decode(stream_link).decode("utf-8")
|
||||
|
||||
log.info(f"URL {url_num}) Captured M3U8")
|
||||
|
||||
return stream_link
|
||||
|
||||
|
||||
async def get_events() -> list[dict[str, str]]:
|
||||
now = Time.clean(Time.now())
|
||||
|
||||
if not (api_data := API_FILE.load(per_entry=False)):
|
||||
log.info("Refreshing API cache")
|
||||
|
||||
api_data = {"timestamp": now.timestamp()}
|
||||
|
||||
if r := await network.request(
|
||||
urljoin(BASE_URL, "public/api.php"),
|
||||
log=log,
|
||||
params={"action": "get"},
|
||||
):
|
||||
api_data: dict = r.json()
|
||||
|
||||
api_data["timestamp"] = now.timestamp()
|
||||
|
||||
API_FILE.write(api_data)
|
||||
|
||||
events = []
|
||||
|
||||
if last_update := api_data.get("updated_at"):
|
||||
last_update_dt = Time.from_str(last_update, timezone="UTC")
|
||||
|
||||
if last_update_dt.date() != now.date():
|
||||
return events
|
||||
|
||||
for info in api_data.get("groups", []):
|
||||
if not (sport := info["title"]):
|
||||
sport = "Live Event"
|
||||
|
||||
if items := info.get("items"):
|
||||
for event in items:
|
||||
event_name: str = event["title"]
|
||||
|
||||
if not (link := event.get("url")):
|
||||
continue
|
||||
|
||||
events.append(
|
||||
{
|
||||
"sport": fix_league(sport),
|
||||
"event": event_name,
|
||||
"link": link,
|
||||
}
|
||||
)
|
||||
|
||||
return events
|
||||
|
||||
|
||||
async def scrape() -> None:
|
||||
if cached := CACHE_FILE.load():
|
||||
urls.update(cached)
|
||||
|
||||
log.info(f"Loaded {len(urls)} event(s) from cache")
|
||||
|
||||
return
|
||||
|
||||
log.info(f'Scraping from "{BASE_URL}"')
|
||||
|
||||
events = await get_events()
|
||||
|
||||
if events:
|
||||
log.info(f"Processing {len(events)} new URL(s)")
|
||||
|
||||
now = Time.clean(Time.now())
|
||||
|
||||
for i, ev in enumerate(events, start=1):
|
||||
handler = partial(
|
||||
process_event,
|
||||
url=(link := ev["link"]),
|
||||
url_num=i,
|
||||
)
|
||||
|
||||
url = await network.safe_process(
|
||||
handler,
|
||||
url_num=i,
|
||||
semaphore=network.HTTP_S,
|
||||
log=log,
|
||||
)
|
||||
|
||||
if url:
|
||||
sport, event = ev["sport"], ev["event"]
|
||||
|
||||
key = f"[{sport}] {event} ({TAG})"
|
||||
|
||||
tvg_id, logo = leagues.get_tvg_info(sport, event)
|
||||
|
||||
entry = {
|
||||
"url": url,
|
||||
"logo": logo,
|
||||
"base": link,
|
||||
"timestamp": now.timestamp(),
|
||||
"id": tvg_id or "Live.Event.us",
|
||||
"link": link,
|
||||
}
|
||||
|
||||
urls[key] = entry
|
||||
|
||||
log.info(f"Collected {len(urls)} event(s)")
|
||||
|
||||
CACHE_FILE.write(urls)
|
||||
|
|
@ -22,7 +22,7 @@ BASE_URL = "http://volokit.xyz"
|
|||
SPORT_ENDPOINTS = {
|
||||
"mlb": "MLB",
|
||||
# "nfl": "NFL",
|
||||
# "nhl": "NHL",
|
||||
"nhl": "NHL",
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue