From 654f95ac6f29dfdf7ac7d6b7b855cc2ea222d76d Mon Sep 17 00:00:00 2001 From: DeSqBlocki Date: Sat, 6 Dec 2025 13:20:46 +0100 Subject: [PATCH] add lib --- .gitignore | 1 - lib/mongo.js | 14 ++++ scripts/lib/proxer_utils.py | 128 ++++++++++++++++++++++++++++++++++++ scripts/lib/script_utils.py | 38 +++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 lib/mongo.js create mode 100644 scripts/lib/proxer_utils.py create mode 100644 scripts/lib/script_utils.py diff --git a/.gitignore b/.gitignore index 26085e3..84307af 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,6 @@ dist/ downloads/ eggs/ .eggs/ -lib/ lib64/ parts/ sdist/ diff --git a/lib/mongo.js b/lib/mongo.js new file mode 100644 index 0000000..d998bae --- /dev/null +++ b/lib/mongo.js @@ -0,0 +1,14 @@ +const { MongoClient } = require("mongodb"); +require("dotenv").config(); + +let client, db; + +exports.getDb = async function () { + if (db) return db; + + client = new MongoClient(process.env.MONGO_URI); + await client.connect(); + + db = client.db("downloads"); + return db; +}; diff --git a/scripts/lib/proxer_utils.py b/scripts/lib/proxer_utils.py new file mode 100644 index 0000000..3973325 --- /dev/null +++ b/scripts/lib/proxer_utils.py @@ -0,0 +1,128 @@ +""" + lib/proxer_utils.py + + A utility library for scraping and downloading videos from Proxer.me. +""" + +import logging +from enum import Enum +from requests import Response, get, RequestException +from clint.textui import progress + +# Set up logging +logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") + + +class Language(Enum): + """Enum for video languages""" + GERDUB = "gerdub" + GERSUB = "gersub" + ENGDUB = "engdub" + ENGSUB = "engsub" + + +class ProxerError(Exception): + """Base class for all Proxer-related errors""" + + +class NotFoundError(ProxerError): + """Raised when the target element could not be found""" + + +class NetworkError(ProxerError): + """Raised when there is a network-related error""" + + +def get_embed_url(episode_url: str, auth_cookie: tuple[str, str]) -> str: + """ + Extracts the embed URL from a Proxer episode page. + + :param episode_url: URL of the Proxer episode page + :param auth_cookie: Tuple containing (cookie_name, cookie_value) + :returns: Embed video URL + :raises NotFoundError: If embed URL is not found + :raises NetworkError: If a request fails + """ + SEARCH_TOKEN = '"code":"' + SEARCH_SERVER = "stream-service.proxer.me" + HEADERS = {'User-Agent': 'Mozilla/5.0'} + + try: + response = get(episode_url, cookies={auth_cookie[0]: auth_cookie[1], 'stream_choose': 'proxer-stream'}, headers=HEADERS) + response.raise_for_status() + content = response.text + except RequestException as e: + raise NetworkError(f"Failed to fetch episode page: {e}") + + # Extract the embed code + code_index = content.find(SEARCH_TOKEN) + len(SEARCH_TOKEN) + if code_index < len(SEARCH_TOKEN): + raise NotFoundError("Embed code not found in page content.") + + code : str = content[code_index:].partition('"')[0] + + # Detect the correct streaming service + host : str = "stream-service" if SEARCH_SERVER in content else "stream" + return f"https://{host}.proxer.me/embed-{code}.html" + + +def get_mp4_url(embed_url: str) -> str: + """ + Extracts the direct MP4 URL from an embed page. + + :param embed_url: URL of the Proxer embed player + :returns: Direct MP4 URL + :raises NotFoundError: If the MP4 URL is not found + :raises NetworkError: If a request fails + """ + SEARCH_TOKEN = 'video/mp4" src="' + HEADERS = {'User-Agent': 'Mozilla/5.0'} + + try: + response = get(embed_url, headers=HEADERS) + response.raise_for_status() + content = response.text + except RequestException as e: + raise NetworkError(f"Failed to fetch embed page: {e}") + + # Extract the MP4 URL + link_index = content.find(SEARCH_TOKEN) + len(SEARCH_TOKEN) + if link_index < len(SEARCH_TOKEN): + raise NotFoundError("MP4 URL not found in embed page.") + + return content[link_index:].partition('"')[0] + + +def download_mp4(mp4_url: str, save_path: str, label: str = "") -> None: + """ + Downloads an MP4 file from a given URL and displays a progress bar. + + :param mp4_url: URL of the MP4 file + :param save_path: Path to save the downloaded file + :param label: Optional progress bar label + :raises NetworkError: If a request fails + """ + CHUNK_SIZE = 255 + MAX_LABEL_LENGTH = 32 + + # Adjust label for progress bar + if label: + label = label[:MAX_LABEL_LENGTH - 3] + "..." if len(label) > MAX_LABEL_LENGTH else label.ljust(MAX_LABEL_LENGTH) + + try: + response = get(mp4_url, stream=True) + response.raise_for_status() + total_length = int(response.headers.get('content-length', 0)) + except RequestException as e: + raise NetworkError(f"Failed to start download: {e}") + + # Download file with progress bar + with open(save_path, "wb") as file: + if total_length: + for chunk in progress.bar(response.iter_content(chunk_size=CHUNK_SIZE), expected_size=total_length / CHUNK_SIZE, label=label): + if chunk: + file.write(chunk) + else: + file.write(response.content) + + logging.info(f"Download complete: {save_path}") diff --git a/scripts/lib/script_utils.py b/scripts/lib/script_utils.py new file mode 100644 index 0000000..00f1f0b --- /dev/null +++ b/scripts/lib/script_utils.py @@ -0,0 +1,38 @@ +""" + lib/script_utils.py + + lib for utility functions for cl script execution +""" + +from sys import exit + + +def script_exit(reason: str) -> None: + """Wrapper for script exit on error""" + print(f'exit with error: {reason}') + exit() + + +def check_validity_token(token: str) -> list: + """Processes token input and checks its validity, exits if invalid""" + try: + spl_token : list = token.split('=') + if len(spl_token[0]) <= 0 or len(spl_token[1]) <= 0: + raise Exception + else: + return spl_token + except Exception: + script_exit('auth token could not be parsed') + + +def check_validity_episodes(episodes: str) -> list: + """Processes episodes input and checks its validity, exits if invalid""" + try: + spl_ep : list = list(map(int, episodes.split('-'))) + if len(spl_ep) != 2: + raise Exception + if spl_ep[0] > spl_ep[1]: + script_exit('start larger than end episode') + return spl_ep + except Exception: + script_exit('episodes input could not be processed')