main: consolidate integrated changes after v0.1.54
This commit is contained in:
@@ -19,8 +19,8 @@ import hashlib
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeAlias
|
||||
from urllib.parse import urlencode, urljoin
|
||||
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
|
||||
from urllib.parse import urljoin
|
||||
|
||||
try: # pragma: no cover - optional dependency
|
||||
import requests
|
||||
@@ -51,13 +51,13 @@ if TYPE_CHECKING: # pragma: no cover
|
||||
from requests import Session as RequestsSession
|
||||
from bs4 import BeautifulSoup as BeautifulSoupT # type: ignore[import-not-found]
|
||||
else: # pragma: no cover
|
||||
RequestsSession: TypeAlias = Any
|
||||
BeautifulSoupT: TypeAlias = Any
|
||||
RequestsSession = Any
|
||||
BeautifulSoupT = Any
|
||||
|
||||
|
||||
ADDON_ID = "plugin.video.viewit"
|
||||
SETTING_BASE_URL = "topstream_base_url"
|
||||
DEFAULT_BASE_URL = "https://www.meineseite"
|
||||
DEFAULT_BASE_URL = "https://topstreamfilm.live"
|
||||
GLOBAL_SETTING_LOG_URLS = "debug_log_urls"
|
||||
GLOBAL_SETTING_DUMP_HTML = "debug_dump_html"
|
||||
GLOBAL_SETTING_SHOW_URL_INFO = "debug_show_url_info"
|
||||
@@ -78,6 +78,16 @@ HEADERS = {
|
||||
"Accept-Language": "de-DE,de;q=0.9,en;q=0.8",
|
||||
"Connection": "keep-alive",
|
||||
}
|
||||
ProgressCallback = Optional[Callable[[str, Optional[int]], Any]]
|
||||
|
||||
|
||||
def _emit_progress(callback: ProgressCallback, message: str, percent: Optional[int] = None) -> None:
|
||||
if not callable(callback):
|
||||
return
|
||||
try:
|
||||
callback(str(message or ""), None if percent is None else int(percent))
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -87,6 +97,7 @@ class SearchHit:
|
||||
title: str
|
||||
url: str
|
||||
description: str = ""
|
||||
poster: str = ""
|
||||
|
||||
|
||||
def _normalize_search_text(value: str) -> str:
|
||||
@@ -139,6 +150,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
self._season_to_episode_numbers: Dict[tuple[str, str], List[int]] = {}
|
||||
self._episode_title_by_number: Dict[tuple[str, int, int], str] = {}
|
||||
self._detail_html_cache: Dict[str, str] = {}
|
||||
self._title_meta: Dict[str, tuple[str, str]] = {}
|
||||
self._popular_cache: List[str] | None = None
|
||||
self._default_preferred_hosters: List[str] = list(DEFAULT_PREFERRED_HOSTERS)
|
||||
self._preferred_hosters: List[str] = list(self._default_preferred_hosters)
|
||||
@@ -419,6 +431,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
continue
|
||||
seen.add(hit.title)
|
||||
self._title_to_url[hit.title] = hit.url
|
||||
self._store_title_meta(hit.title, plot=hit.description, poster=hit.poster)
|
||||
titles.append(hit.title)
|
||||
if titles:
|
||||
self._save_title_url_cache()
|
||||
@@ -477,6 +490,69 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
def _pick_image_from_node(self, node: Any) -> str:
|
||||
if node is None:
|
||||
return ""
|
||||
image = node.select_one("img")
|
||||
if image is None:
|
||||
return ""
|
||||
for attr in ("data-src", "src"):
|
||||
value = (image.get(attr) or "").strip()
|
||||
if value and "lazy_placeholder" not in value.casefold():
|
||||
return self._absolute_external_url(value, base=self._get_base_url())
|
||||
srcset = (image.get("data-srcset") or image.get("srcset") or "").strip()
|
||||
if srcset:
|
||||
first = srcset.split(",")[0].strip().split(" ", 1)[0].strip()
|
||||
if first:
|
||||
return self._absolute_external_url(first, base=self._get_base_url())
|
||||
return ""
|
||||
|
||||
def _store_title_meta(self, title: str, *, plot: str = "", poster: str = "") -> None:
|
||||
title = (title or "").strip()
|
||||
if not title:
|
||||
return
|
||||
old_plot, old_poster = self._title_meta.get(title, ("", ""))
|
||||
merged_plot = (plot or old_plot or "").strip()
|
||||
merged_poster = (poster or old_poster or "").strip()
|
||||
self._title_meta[title] = (merged_plot, merged_poster)
|
||||
|
||||
def _extract_detail_metadata(self, soup: BeautifulSoupT) -> tuple[str, str]:
|
||||
if not soup:
|
||||
return "", ""
|
||||
plot = ""
|
||||
poster = ""
|
||||
for selector in ("meta[property='og:description']", "meta[name='description']"):
|
||||
node = soup.select_one(selector)
|
||||
if node is None:
|
||||
continue
|
||||
content = (node.get("content") or "").strip()
|
||||
if content:
|
||||
plot = content
|
||||
break
|
||||
if not plot:
|
||||
candidates: list[str] = []
|
||||
for paragraph in soup.select("article p, .TPost p, .Description p, .entry-content p"):
|
||||
text = (paragraph.get_text(" ", strip=True) or "").strip()
|
||||
if len(text) >= 60:
|
||||
candidates.append(text)
|
||||
if candidates:
|
||||
plot = max(candidates, key=len)
|
||||
|
||||
for selector in ("meta[property='og:image']", "meta[name='twitter:image']"):
|
||||
node = soup.select_one(selector)
|
||||
if node is None:
|
||||
continue
|
||||
content = (node.get("content") or "").strip()
|
||||
if content:
|
||||
poster = self._absolute_external_url(content, base=self._get_base_url())
|
||||
break
|
||||
if not poster:
|
||||
for selector in ("article", ".TPost", ".entry-content"):
|
||||
poster = self._pick_image_from_node(soup.select_one(selector))
|
||||
if poster:
|
||||
break
|
||||
return plot, poster
|
||||
|
||||
def _clear_stream_index_for_title(self, title: str) -> None:
|
||||
for key in list(self._season_to_episode_numbers.keys()):
|
||||
if key[0] == title:
|
||||
@@ -584,15 +660,25 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
session = self._get_session()
|
||||
self._log_url(url, kind="VISIT")
|
||||
self._notify_url(url)
|
||||
response = None
|
||||
try:
|
||||
response = session.get(url, timeout=DEFAULT_TIMEOUT)
|
||||
response.raise_for_status()
|
||||
except Exception as exc:
|
||||
self._log_error(f"GET {url} failed: {exc}")
|
||||
raise
|
||||
self._log_url(response.url, kind="OK")
|
||||
self._log_response_html(response.url, response.text)
|
||||
return BeautifulSoup(response.text, "html.parser")
|
||||
try:
|
||||
final_url = (response.url or url) if response is not None else url
|
||||
body = (response.text or "") if response is not None else ""
|
||||
self._log_url(final_url, kind="OK")
|
||||
self._log_response_html(final_url, body)
|
||||
return BeautifulSoup(body, "html.parser")
|
||||
finally:
|
||||
if response is not None:
|
||||
try:
|
||||
response.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _get_detail_soup(self, title: str) -> Optional[BeautifulSoupT]:
|
||||
title = (title or "").strip()
|
||||
@@ -701,7 +787,17 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
continue
|
||||
if is_movie_hint:
|
||||
self._movie_title_hint.add(title)
|
||||
hits.append(SearchHit(title=title, url=self._absolute_url(href), description=""))
|
||||
description_tag = item.select_one(".TPMvCn .Description, .Description, .entry-summary")
|
||||
description = (description_tag.get_text(" ", strip=True) or "").strip() if description_tag else ""
|
||||
poster = self._pick_image_from_node(item)
|
||||
hits.append(
|
||||
SearchHit(
|
||||
title=title,
|
||||
url=self._absolute_url(href),
|
||||
description=description,
|
||||
poster=poster,
|
||||
)
|
||||
)
|
||||
return hits
|
||||
|
||||
def is_movie(self, title: str) -> bool:
|
||||
@@ -774,6 +870,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
continue
|
||||
seen.add(hit.title)
|
||||
self._title_to_url[hit.title] = hit.url
|
||||
self._store_title_meta(hit.title, plot=hit.description, poster=hit.poster)
|
||||
titles.append(hit.title)
|
||||
if titles:
|
||||
self._save_title_url_cache()
|
||||
@@ -814,7 +911,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
# Sonst: Serie via Streams-Accordion parsen (falls vorhanden).
|
||||
self._parse_stream_accordion(soup, title=title)
|
||||
|
||||
async def search_titles(self, query: str) -> List[str]:
|
||||
async def search_titles(self, query: str, progress_callback: ProgressCallback = None) -> List[str]:
|
||||
"""Sucht Titel ueber eine HTML-Suche.
|
||||
|
||||
Erwartetes HTML (Snippet):
|
||||
@@ -827,6 +924,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
query = (query or "").strip()
|
||||
if not query:
|
||||
return []
|
||||
_emit_progress(progress_callback, "Topstreamfilm Suche", 15)
|
||||
|
||||
session = self._get_session()
|
||||
url = self._get_base_url() + "/"
|
||||
@@ -834,6 +932,7 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
request_url = f"{url}?{urlencode(params)}"
|
||||
self._log_url(request_url, kind="GET")
|
||||
self._notify_url(request_url)
|
||||
response = None
|
||||
try:
|
||||
response = session.get(
|
||||
url,
|
||||
@@ -844,15 +943,28 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
except Exception as exc:
|
||||
self._log_error(f"GET {request_url} failed: {exc}")
|
||||
raise
|
||||
self._log_url(response.url, kind="OK")
|
||||
self._log_response_html(response.url, response.text)
|
||||
try:
|
||||
final_url = (response.url or request_url) if response is not None else request_url
|
||||
body = (response.text or "") if response is not None else ""
|
||||
self._log_url(final_url, kind="OK")
|
||||
self._log_response_html(final_url, body)
|
||||
|
||||
if BeautifulSoup is None:
|
||||
return []
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
if BeautifulSoup is None:
|
||||
return []
|
||||
soup = BeautifulSoup(body, "html.parser")
|
||||
finally:
|
||||
if response is not None:
|
||||
try:
|
||||
response.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
hits: List[SearchHit] = []
|
||||
for item in soup.select("li.TPostMv"):
|
||||
items = soup.select("li.TPostMv")
|
||||
total_items = max(1, len(items))
|
||||
for idx, item in enumerate(items, start=1):
|
||||
if idx == 1 or idx % 20 == 0:
|
||||
_emit_progress(progress_callback, f"Treffer pruefen {idx}/{total_items}", 55)
|
||||
anchor = item.select_one("a[href]")
|
||||
if not anchor:
|
||||
continue
|
||||
@@ -870,7 +982,8 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
self._movie_title_hint.add(title)
|
||||
description_tag = item.select_one(".TPMvCn .Description")
|
||||
description = description_tag.get_text(" ", strip=True) if description_tag else ""
|
||||
hit = SearchHit(title=title, url=self._absolute_url(href), description=description)
|
||||
poster = self._pick_image_from_node(item)
|
||||
hit = SearchHit(title=title, url=self._absolute_url(href), description=description, poster=poster)
|
||||
if _matches_query(query, title=hit.title, description=hit.description):
|
||||
hits.append(hit)
|
||||
|
||||
@@ -883,10 +996,41 @@ class TopstreamfilmPlugin(BasisPlugin):
|
||||
continue
|
||||
seen.add(hit.title)
|
||||
self._title_to_url[hit.title] = hit.url
|
||||
self._store_title_meta(hit.title, plot=hit.description, poster=hit.poster)
|
||||
titles.append(hit.title)
|
||||
self._save_title_url_cache()
|
||||
_emit_progress(progress_callback, f"Fertig: {len(titles)} Treffer", 95)
|
||||
return titles
|
||||
|
||||
def metadata_for(self, title: str) -> tuple[dict[str, str], dict[str, str], list[object] | None]:
|
||||
title = (title or "").strip()
|
||||
if not title:
|
||||
return {}, {}, None
|
||||
|
||||
info: dict[str, str] = {"title": title}
|
||||
art: dict[str, str] = {}
|
||||
|
||||
cached_plot, cached_poster = self._title_meta.get(title, ("", ""))
|
||||
if cached_plot:
|
||||
info["plot"] = cached_plot
|
||||
if cached_poster:
|
||||
art = {"thumb": cached_poster, "poster": cached_poster}
|
||||
|
||||
if "plot" in info and art:
|
||||
return info, art, None
|
||||
|
||||
soup = self._get_detail_soup(title)
|
||||
if soup is None:
|
||||
return info, art, None
|
||||
|
||||
plot, poster = self._extract_detail_metadata(soup)
|
||||
if plot:
|
||||
info["plot"] = plot
|
||||
if poster:
|
||||
art = {"thumb": poster, "poster": poster}
|
||||
self._store_title_meta(title, plot=plot, poster=poster)
|
||||
return info, art, None
|
||||
|
||||
def genres(self) -> List[str]:
|
||||
if not REQUESTS_AVAILABLE or BeautifulSoup is None:
|
||||
return []
|
||||
|
||||
Reference in New Issue
Block a user