e
This commit is contained in:
parent
7103b0f1c4
commit
00000d9937
17 changed files with 597 additions and 524 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import asyncio
|
||||
from datetime import datetime, timedelta
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from urllib.parse import urljoin
|
||||
|
|
@ -7,26 +6,15 @@ from urllib.parse import urljoin
|
|||
import httpx
|
||||
from playwright.async_api import async_playwright
|
||||
|
||||
from .utils import (
|
||||
TZ,
|
||||
capture_req,
|
||||
get_base,
|
||||
get_logger,
|
||||
leagues,
|
||||
load_cache,
|
||||
new_browser,
|
||||
now,
|
||||
safe_process_event,
|
||||
write_cache,
|
||||
)
|
||||
from .utils import Cache, Time, get_logger, leagues, network
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
urls: dict[str, dict[str, str | float]] = {}
|
||||
|
||||
API_FILE = Path(__file__).parent / "caches" / "ppv_api.json"
|
||||
API_FILE = Cache(Path(__file__).parent / "caches" / "ppv_api.json", exp=86_400)
|
||||
|
||||
CACHE_FILE = Path(__file__).parent / "caches" / "ppv.json"
|
||||
CACHE_FILE = Cache(Path(__file__).parent / "caches" / "ppv.json", exp=10_800)
|
||||
|
||||
MIRRORS = [
|
||||
"https://ppvs.su",
|
||||
|
|
@ -37,7 +25,7 @@ MIRRORS = [
|
|||
]
|
||||
|
||||
|
||||
def get_tvg(sport: str, event: str) -> str | None:
|
||||
def get_tvg(sport: str, event: str) -> str:
|
||||
match sport:
|
||||
case "American Football":
|
||||
if leagues.is_valid(event, "NFL"):
|
||||
|
|
@ -79,7 +67,7 @@ async def refresh_api_cache(
|
|||
|
||||
async def process_event(url: str, url_num: int) -> str | None:
|
||||
async with async_playwright() as p:
|
||||
browser, context = await new_browser(p)
|
||||
browser, context = await network.browser(p)
|
||||
|
||||
page = await context.new_page()
|
||||
|
||||
|
|
@ -87,12 +75,16 @@ async def process_event(url: str, url_num: int) -> str | None:
|
|||
|
||||
got_one = asyncio.Event()
|
||||
|
||||
handler = partial(capture_req, captured=captured, got_one=got_one)
|
||||
handler = partial(network.capture_req, captured=captured, got_one=got_one)
|
||||
|
||||
page.on("request", handler)
|
||||
|
||||
try:
|
||||
await page.goto(url, wait_until="domcontentloaded", timeout=15_000)
|
||||
await page.goto(
|
||||
url,
|
||||
wait_until="domcontentloaded",
|
||||
timeout=15_000,
|
||||
)
|
||||
|
||||
wait_task = asyncio.create_task(got_one.wait())
|
||||
|
||||
|
|
@ -137,25 +129,18 @@ async def get_events(
|
|||
|
||||
events: list[dict[str, str]] = []
|
||||
|
||||
if not (
|
||||
api_data := load_cache(
|
||||
API_FILE,
|
||||
exp=86_400,
|
||||
nearest_hr=True,
|
||||
per_entry=False,
|
||||
)
|
||||
):
|
||||
if not (api_data := API_FILE.load(nearest_hr=True, per_entry=False)):
|
||||
api_data = await refresh_api_cache(client, urljoin(base_url, "api/streams"))
|
||||
|
||||
write_cache(API_FILE, api_data)
|
||||
API_FILE.write(api_data)
|
||||
|
||||
for stream_group in api_data["streams"]:
|
||||
sport = stream_group["category"]
|
||||
for stream_group in api_data.get("streams", []):
|
||||
sport = stream_group("category", [])
|
||||
|
||||
if sport == "24/7 Streams":
|
||||
continue
|
||||
|
||||
for event in stream_group["streams"]:
|
||||
for event in stream_group.get("streams", []):
|
||||
name, start_ts, end_ts, logo, uri_name = (
|
||||
event["name"],
|
||||
event["starts_at"],
|
||||
|
|
@ -169,11 +154,11 @@ async def get_events(
|
|||
if cached_keys & {key}:
|
||||
continue
|
||||
|
||||
start_dt = datetime.fromtimestamp(start_ts, tz=TZ) - timedelta(minutes=30)
|
||||
start_dt = Time.from_ts(start_ts).delta(minutes=-30)
|
||||
|
||||
end_dt = datetime.fromtimestamp(end_ts, tz=TZ) + timedelta(minutes=30)
|
||||
end_dt = Time.from_ts(end_ts).delta(minutes=30)
|
||||
|
||||
if not start_dt <= now < end_dt:
|
||||
if not start_dt <= Time.now() < end_dt:
|
||||
continue
|
||||
|
||||
events.append(
|
||||
|
|
@ -182,6 +167,7 @@ async def get_events(
|
|||
"event": name,
|
||||
"link": urljoin(base_url, f"live/{uri_name}"),
|
||||
"logo": logo,
|
||||
"timestamp": start_dt.timestamp(),
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -189,15 +175,15 @@ async def get_events(
|
|||
|
||||
|
||||
async def scrape(client: httpx.AsyncClient) -> None:
|
||||
cached_urls = load_cache(CACHE_FILE, exp=10_800)
|
||||
cached_urls = CACHE_FILE.load()
|
||||
cached_count = len(cached_urls)
|
||||
urls.update(cached_urls)
|
||||
|
||||
log.info(f"Collected {cached_count} event(s) from cache")
|
||||
log.info(f"Loaded {cached_count} event(s) from cache")
|
||||
|
||||
if not (base_url := await get_base(client, MIRRORS)):
|
||||
if not (base_url := await network.get_base(MIRRORS)):
|
||||
log.warning("No working PPV mirrors")
|
||||
write_cache(CACHE_FILE, cached_urls)
|
||||
CACHE_FILE.write(cached_urls)
|
||||
return
|
||||
|
||||
log.info(f'Scraping from "{base_url}"')
|
||||
|
|
@ -211,14 +197,19 @@ async def scrape(client: httpx.AsyncClient) -> None:
|
|||
log.info(f"Processing {len(events)} new URL(s)")
|
||||
|
||||
for i, ev in enumerate(events, start=1):
|
||||
url = await safe_process_event(
|
||||
url = await network.safe_process(
|
||||
lambda: process_event(ev["link"], url_num=i),
|
||||
url_num=i,
|
||||
log=log,
|
||||
)
|
||||
|
||||
if url:
|
||||
sport, event, logo = ev["sport"], ev["event"], ev["logo"]
|
||||
sport, event, logo, ts = (
|
||||
ev["sport"],
|
||||
ev["event"],
|
||||
ev["logo"],
|
||||
ev["timestamp"],
|
||||
)
|
||||
|
||||
key = f"[{sport}] {event} (PPV)"
|
||||
|
||||
|
|
@ -226,7 +217,7 @@ async def scrape(client: httpx.AsyncClient) -> None:
|
|||
"url": url,
|
||||
"logo": logo,
|
||||
"base": base_url,
|
||||
"timestamp": now.timestamp(),
|
||||
"timestamp": ts,
|
||||
"id": get_tvg(sport, event) or "Live.Event.us",
|
||||
}
|
||||
|
||||
|
|
@ -237,4 +228,4 @@ async def scrape(client: httpx.AsyncClient) -> None:
|
|||
else:
|
||||
log.info("No new events found")
|
||||
|
||||
write_cache(CACHE_FILE, cached_urls)
|
||||
CACHE_FILE.write(cached_urls)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue