Created
August 20, 2019 18:46
-
-
Save isidentical/d0018714fd1f89c6777625190510457c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import annotations | |
import random | |
import webbrowser | |
from contextlib import suppress | |
from dataclasses import asdict, dataclass, replace | |
from enum import Enum | |
from html.parser import HTMLParser | |
from typing import Optional | |
Connection = Optional[int] | |
S_MAX_7 = 22 | |
S_MAX_8 = 20 | |
class Provider(Enum): | |
DP = "https://dizipub.net/stargate-sg-1-{season}-sezon-{episode}-bolum-izle/?player=embed" | |
def __str__(self): | |
return self.value | |
class EpisodeParser(HTMLParser): | |
def __init__(self, season, episode, *args, **kwargs): | |
self.season = season | |
self.episode = episode | |
self.episode_name = None | |
self._recent_tag = None | |
self._recent_attrs = None | |
self._current_season = None | |
super().__init__(*args, **kwargs) | |
def handle_starttag(self, tag, attrs): | |
attrs = dict(attrs) | |
self._recent_tag = tag | |
self._recent_attrs = attrs | |
if tag == "a" and (title := attrs.get("title")): | |
with suppress(ValueError): | |
if title.startswith("SG-1 Season"): | |
self._current_season = int(title[len("SG-1 Season ") :]) | |
def handle_data(self, data): | |
if self._recent_tag == "a" and (title := self._recent_attrs.get("title")): | |
with suppress(ValueError): | |
episode = int(data) | |
if self._current_season == self.season and episode == self.episode: | |
self.episode_name = title | |
@dataclass | |
class Episode: | |
season: int | |
episode: int | |
connection: Connection = None | |
def __post_init__(self): | |
if (conn := self.check_connection()) : | |
self.connection = conn | |
@property | |
def start(self) -> Episode: | |
if (conn := self.connection) : | |
need = 1 - conn | |
assert self.episode + need > 0 | |
return replace(self, episode=self.episode + need) | |
else: | |
return self | |
@classmethod | |
def random(cls) -> Episode: | |
season = random.randint(1, 10) | |
episode = random.randint(1, S_MAX_8 if season >= 8 else S_MAX_7) | |
return cls(season=season, episode=episode) | |
def check_connection(self) -> Connection: | |
with open("wiki.html") as wiki: | |
content = wiki.read() | |
parser = EpisodeParser(self.season, self.episode) | |
parser.feed(content) | |
if (name := parser.episode_name) : | |
name, *connection = name.split(", ") | |
if connection: | |
(connection,) = connection | |
assert "part" in connection.casefold() | |
_, connection = connection.split(" ") | |
return int(connection) | |
if __name__ == "__main__": | |
provider = random.choice(tuple(Provider.__members__.values())) | |
episode = Episode.random() | |
webbrowser.open_new_tab(str(provider).format_map(asdict(episode.start))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment