Created
June 21, 2019 20:11
-
-
Save pboehm/504fc07804a7dddeb39ce375c07c0b7d 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
#!/usr/bin/env python | |
import json | |
import os | |
import random | |
import re | |
import subprocess | |
import sys | |
from typing import List, Optional | |
import requests | |
from lxml import html | |
def fetch_hd_link_ids(slug: str, season: str, episode: str, lang: str) -> List[str]: | |
lang_code = 1 if lang == "de" else 2 | |
page = requests.get(f"https://s.to/serie/stream/{slug}/staffel-{season}/episode-{episode}") | |
tree = html.fromstring(page.content) | |
query = f"//li[@data-link-id and @data-lang-key='{lang_code}' and .//small[.='HD-Stream']]" | |
return [node.attrib["data-link-id"] for node in tree.xpath(query)] | |
def resolve_link_id(link_id: str) -> Optional[str]: | |
response = requests.post(f"http://localhost:8080/api/link/resolve/{link_id}") | |
if response.status_code == 200: | |
return response.json()["videoUrl"] | |
return None | |
def call_command_with_link(command: str, link_id: str, resolved_link: str) -> bool: | |
print(f"Run action with link {resolved_link}") | |
env = dict(os.environ) | |
env["SERIES_VIDEO_URL"] = resolved_link | |
env["SERIES_LINK_ID"] = link_id | |
env["SERIES_REDIRECT_URL"] = re.sub(r"stream/\d+", f"stream/{link_id}", env["SERIES_REDIRECT_URL"]) | |
res = subprocess.run([command], shell=True, stdout=sys.stdout, stderr=sys.stderr, bufsize=0, | |
env=env) | |
if res.returncode == 0: | |
return True | |
return False | |
def main(run_action: str): | |
env_vars = {var: val for (var, val) in os.environ.items() if var.startswith("SERIES_")} | |
series_slug = env_vars["SERIES_SERIES_SLUG"] | |
season = env_vars["SERIES_SEASON"] | |
episode = env_vars["SERIES_EPISODE"] | |
lang = env_vars["SERIES_LANGUAGE"] | |
config = json.load(open(env_vars["SERIES_CONFIG_FILE"], "rb")) | |
command = [a["Command"] for a in config["StreamsLinkActions"] if a["Id"] == run_action][0] | |
success = False | |
link_ids = fetch_hd_link_ids(series_slug, season, episode, lang) | |
random.shuffle(link_ids) | |
for link_id in link_ids: | |
resolved_link = resolve_link_id(link_id) | |
if not resolved_link: | |
continue | |
if call_command_with_link(command, link_id, resolved_link): | |
success = True | |
break | |
if not success: | |
print("was not successful") | |
sys.exit(1) | |
if __name__ == '__main__': | |
main(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment