mirror of
https://github.com/doms9/iptv.git
synced 2026-06-06 02:43:05 +02:00
e
- edit scraping for livetvsx.py - edit caching for streamsgate.py - edit caching for streamhub.py - misc edits.
This commit is contained in:
parent
fce1e8f6a9
commit
00000d9fe2
6 changed files with 152 additions and 249 deletions
|
|
@ -13,9 +13,7 @@ urls: dict[str, dict[str, str | float]] = {}
|
|||
|
||||
TAG = "STRMHUB"
|
||||
|
||||
CACHE_FILE = Cache(TAG, exp=10_800)
|
||||
|
||||
HTML_FILE = Cache(f"{TAG}-html", exp=19_800)
|
||||
CACHE_FILE = Cache(TAG, exp=28_800)
|
||||
|
||||
BASE_URL = "https://livesports4u.net"
|
||||
|
||||
|
|
@ -116,116 +114,78 @@ async def process_event(
|
|||
page.remove_listener("request", handler)
|
||||
|
||||
|
||||
async def refresh_html_cache(
|
||||
date: str,
|
||||
sport_id: str,
|
||||
ts: float,
|
||||
) -> dict[str, dict[str, str | float]]:
|
||||
async def get_events() -> list[dict[str, str]]:
|
||||
now = Time.clean(Time.now())
|
||||
|
||||
events = {}
|
||||
|
||||
if not (
|
||||
html_data := await network.request(
|
||||
tasks = [
|
||||
network.request(
|
||||
urljoin(BASE_URL, f"events/{date}"),
|
||||
params={"sport_id": sport_id},
|
||||
log=log,
|
||||
)
|
||||
):
|
||||
for date in [now.date(), now.delta(days=1).date()]
|
||||
for sport_id in SPORT_ENDPOINTS
|
||||
]
|
||||
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
events = []
|
||||
|
||||
if not (soups := [HTMLParser(html.content) for html in results if html]):
|
||||
return events
|
||||
|
||||
soup = HTMLParser(html_data.content)
|
||||
|
||||
for section in soup.css(".events-section"):
|
||||
if not (sport_node := section.css_first(".section-titlte")):
|
||||
continue
|
||||
|
||||
sport = sport_node.text(strip=True)
|
||||
|
||||
for event in section.css(".section-event"):
|
||||
event_name = "Live Event"
|
||||
|
||||
if teams := event.css_first(".event-competitors"):
|
||||
home, away = teams.text(strip=True).split("vs.")
|
||||
|
||||
event_name = f"{away} vs {home}"
|
||||
|
||||
if not (event_button := event.css_first(".event-button a")) or not (
|
||||
href := event_button.attributes.get("href")
|
||||
):
|
||||
for soup in soups:
|
||||
for section in soup.css(".events-section"):
|
||||
if not (sport_node := section.css_first(".section-titlte")):
|
||||
continue
|
||||
|
||||
event_date = event.css_first(".event-countdown").attributes.get(
|
||||
"data-start"
|
||||
)
|
||||
sport = sport_node.text(strip=True)
|
||||
|
||||
event_dt = Time.from_str(event_date, timezone="UTC")
|
||||
for event in section.css(".section-event"):
|
||||
event_name = "Live Event"
|
||||
|
||||
key = f"[{sport}] {event_name} ({TAG})"
|
||||
if teams := event.css_first(".event-competitors"):
|
||||
home, away = teams.text(strip=True).split("vs.")
|
||||
|
||||
events[key] = {
|
||||
"sport": sport,
|
||||
"event": event_name,
|
||||
"link": href,
|
||||
"event_ts": event_dt.timestamp(),
|
||||
"timestamp": ts,
|
||||
}
|
||||
event_name = f"{away} vs {home}"
|
||||
|
||||
if not (event_button := event.css_first(".event-button a")) or not (
|
||||
href := event_button.attributes.get("href")
|
||||
):
|
||||
continue
|
||||
|
||||
event_date = event.css_first(".event-countdown").attributes.get(
|
||||
"data-start"
|
||||
)
|
||||
|
||||
event_dt = Time.from_str(event_date, timezone="UTC")
|
||||
|
||||
if event_dt.date() != now.date():
|
||||
continue
|
||||
|
||||
events.append(
|
||||
{
|
||||
"sport": sport,
|
||||
"event": event_name,
|
||||
"link": href,
|
||||
"timestamp": now.timestamp(),
|
||||
}
|
||||
)
|
||||
|
||||
return events
|
||||
|
||||
|
||||
async def get_events(cached_keys: list[str]) -> list[dict[str, str]]:
|
||||
now = Time.clean(Time.now())
|
||||
|
||||
if not (events := HTML_FILE.load()):
|
||||
log.info("Refreshing HTML cache")
|
||||
|
||||
tasks = [
|
||||
refresh_html_cache(
|
||||
date,
|
||||
sport_id,
|
||||
now.timestamp(),
|
||||
)
|
||||
for date in [now.date(), now.delta(days=1).date()]
|
||||
for sport_id in SPORT_ENDPOINTS
|
||||
]
|
||||
|
||||
results = await asyncio.gather(*tasks)
|
||||
|
||||
events = {k: v for data in results for k, v in data.items()}
|
||||
|
||||
HTML_FILE.write(events)
|
||||
|
||||
live = []
|
||||
|
||||
start_ts = now.delta(minutes=-30).timestamp()
|
||||
end_ts = now.delta(minutes=30).timestamp()
|
||||
|
||||
for k, v in events.items():
|
||||
if k in cached_keys:
|
||||
continue
|
||||
|
||||
if not start_ts <= v["event_ts"] <= end_ts:
|
||||
continue
|
||||
|
||||
live.append(v)
|
||||
|
||||
return live
|
||||
|
||||
|
||||
async def scrape(browser: Browser) -> None:
|
||||
cached_urls = CACHE_FILE.load()
|
||||
if cached_urls := CACHE_FILE.load():
|
||||
urls.update({k: v for k, v in cached_urls.items() if v["url"]})
|
||||
|
||||
valid_urls = {k: v for k, v in cached_urls.items() if v["url"]}
|
||||
log.info(f"Loaded {len(urls)} event(s) from cache")
|
||||
|
||||
valid_count = cached_count = len(valid_urls)
|
||||
|
||||
urls.update(valid_urls)
|
||||
|
||||
log.info(f"Loaded {cached_count} event(s) from cache")
|
||||
return
|
||||
|
||||
log.info(f'Scraping from "{BASE_URL}"')
|
||||
|
||||
if events := await get_events(cached_urls.keys()):
|
||||
if events := await get_events():
|
||||
log.info(f"Processing {len(events)} new URL(s)")
|
||||
|
||||
async with network.event_context(browser) as context:
|
||||
|
|
@ -249,7 +209,7 @@ async def scrape(browser: Browser) -> None:
|
|||
sport, event, ts = (
|
||||
ev["sport"],
|
||||
ev["event"],
|
||||
ev["event_ts"],
|
||||
ev["timestamp"],
|
||||
)
|
||||
|
||||
key = f"[{sport}] {event} ({TAG})"
|
||||
|
|
@ -268,13 +228,11 @@ async def scrape(browser: Browser) -> None:
|
|||
cached_urls[key] = entry
|
||||
|
||||
if url:
|
||||
valid_count += 1
|
||||
|
||||
entry["url"] = url.split("?st")[0]
|
||||
|
||||
urls[key] = entry
|
||||
|
||||
log.info(f"Collected and cached {valid_count - cached_count} new event(s)")
|
||||
log.info(f"Collected and cached {len(urls)} new event(s)")
|
||||
|
||||
else:
|
||||
log.info("No new events found")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue