|
| 1 | +"""palladiumbio.se — Filmhuset Palladium, Arvika.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import re |
| 5 | +from collections.abc import Iterator |
| 6 | +from datetime import date, time |
| 7 | + |
| 8 | +import requests |
| 9 | +from bs4 import BeautifulSoup |
| 10 | + |
| 11 | +from parse._util import infer_year |
| 12 | +from parse.parsers._tmdb_cache import lookup as _tmdb |
| 13 | +from store import Screening, Venue |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | +_URL = "https://palladiumbio.se/" |
| 18 | +_CINEMA = "Filmhuset Palladium" |
| 19 | +_CITY = "Arvika" |
| 20 | +_ADDRESS = "Hamngatan 11" |
| 21 | + |
| 22 | +_MONTHS = { |
| 23 | + "januari": 1, |
| 24 | + "februari": 2, |
| 25 | + "mars": 3, |
| 26 | + "april": 4, |
| 27 | + "maj": 5, |
| 28 | + "juni": 6, |
| 29 | + "juli": 7, |
| 30 | + "augusti": 8, |
| 31 | + "september": 9, |
| 32 | + "oktober": 10, |
| 33 | + "november": 11, |
| 34 | + "december": 12, |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +def _parse_title(raw: str) -> tuple[str, str, str]: |
| 39 | + """Extract (film_title, language, subtitles) from a raw title string. |
| 40 | +
|
| 41 | + Examples: |
| 42 | + "Super Mario Galaxy Filmen (Tal: Eng) (Text: Sv)" |
| 43 | + "Köln 75 (Tal: Tyska) (Text: Svenska)" |
| 44 | + "Super Mario Galaxy Filmen (Tal: Svenska (dubbat))" |
| 45 | + "BIODLAREN (Tal:Sv) (Tex:Sv)" |
| 46 | + """ |
| 47 | + language = "" |
| 48 | + subtitles = "" |
| 49 | + |
| 50 | + # Extract language — handles both "Tal: Eng" and "Tal:Sv" and "Tal: Svenska (dubbat)" |
| 51 | + m = re.search(r"\(Tal:\s*((?:[^()]*|\([^)]*\))*)\)", raw) |
| 52 | + if m: |
| 53 | + language = m.group(1).strip() |
| 54 | + |
| 55 | + # Extract subtitles — handles "Text: Sv", "Tex:Sv", "Text: Svenska" |
| 56 | + m = re.search(r"\(Te(?:x|xt):\s*([^)]+)\)", raw) |
| 57 | + if m: |
| 58 | + subtitles = m.group(1).strip() |
| 59 | + |
| 60 | + # Strip everything from first parenthesis for the title |
| 61 | + title = re.sub(r"\s*\(.*", "", raw).strip() |
| 62 | + |
| 63 | + return title, language, subtitles |
| 64 | + |
| 65 | + |
| 66 | +def _parse_screen(venue_text: str) -> str: |
| 67 | + """Extract screen name from venue text like 'Filmhuset Palladium Arvika, Salong 2'.""" |
| 68 | + m = re.search(r",\s*(Salong\s+\S+)", venue_text) |
| 69 | + return m.group(1) if m else "" |
| 70 | + |
| 71 | + |
| 72 | +def parse() -> Iterator[Screening | Venue]: |
| 73 | + yield Venue(name=_CINEMA, city=_CITY, address=_ADDRESS) |
| 74 | + |
| 75 | + resp = requests.get(_URL, timeout=15, headers={"User-Agent": "Mozilla/5.0"}) |
| 76 | + resp.raise_for_status() |
| 77 | + soup = BeautifulSoup(resp.text, "html.parser") |
| 78 | + |
| 79 | + current_date: date | None = None |
| 80 | + count = 0 |
| 81 | + |
| 82 | + for tr in soup.select("table.tableBioprogram tr"): |
| 83 | + # Date header row |
| 84 | + th = tr.select_one("th.date") |
| 85 | + if th: |
| 86 | + text = th.get_text(strip=True) |
| 87 | + # "Onsdag 1 april" |
| 88 | + m = re.match(r"\w+\s+(\d{1,2})\s+(\w+)", text) |
| 89 | + if m: |
| 90 | + day = int(m.group(1)) |
| 91 | + month = _MONTHS.get(m.group(2).lower()) |
| 92 | + if month: |
| 93 | + current_date = date(infer_year(month), month, day) |
| 94 | + continue |
| 95 | + |
| 96 | + if current_date is None: |
| 97 | + continue |
| 98 | + |
| 99 | + time_el = tr.select_one("td.time span") |
| 100 | + title_el = tr.select_one("td.title a") |
| 101 | + venue_el = tr.select_one("td.venue") |
| 102 | + buy_el = tr.select_one("td.buy a.btn-primary") |
| 103 | + |
| 104 | + if not time_el or not title_el or not buy_el: |
| 105 | + continue |
| 106 | + |
| 107 | + raw_time = time_el.get_text(strip=True) |
| 108 | + m = re.match(r"(\d{1,2}):(\d{2})", raw_time) |
| 109 | + if not m: |
| 110 | + continue |
| 111 | + t = time(int(m.group(1)), int(m.group(2))) |
| 112 | + |
| 113 | + raw_title = title_el.get_text(strip=True) |
| 114 | + film_title, language, subtitles = _parse_title(raw_title) |
| 115 | + if not film_title: |
| 116 | + continue |
| 117 | + |
| 118 | + ticket_url = buy_el.get("href", "") |
| 119 | + if not ticket_url: |
| 120 | + continue |
| 121 | + |
| 122 | + screen = _parse_screen(venue_el.get_text(strip=True)) if venue_el else "" |
| 123 | + |
| 124 | + tmdb_id = _tmdb(film_title) |
| 125 | + if tmdb_id is None: |
| 126 | + continue |
| 127 | + |
| 128 | + yield Screening( |
| 129 | + tmdb_id=tmdb_id, |
| 130 | + date=current_date, |
| 131 | + time=t, |
| 132 | + ticket_url=ticket_url, |
| 133 | + cinema_name=_CINEMA, |
| 134 | + city=_CITY, |
| 135 | + screen=screen, |
| 136 | + language=language, |
| 137 | + subtitles=subtitles, |
| 138 | + ) |
| 139 | + count += 1 |
| 140 | + |
| 141 | + log.info("palladiumbio.se: %d screenings", count) |
0 commit comments