Created
May 30, 2026 00:49
-
-
Save me-suzy/41e67b5150276ccd902dc7ecfe864e5f to your computer and use it in GitHub Desktop.
g5hgghh.py
This file contains hidden or 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
| import os | |
| import secrets | |
| import subprocess | |
| import sys | |
| import time | |
| from datetime import datetime | |
| from pathlib import Path | |
| from flask import Flask, abort, jsonify, render_template, request | |
| from pyngrok import conf, ngrok | |
| SCAN_ROOT = Path(r"G:\\") | |
| BASE_DIR = Path(__file__).resolve().parent | |
| BAT_PATH = Path(r"D:\TEST\Run_PDF_Downloader.bat") | |
| SECRET_FILE = BASE_DIR / "secret.txt" | |
| TOKEN_FILE = BASE_DIR / "ngrok_token.txt" | |
| URL_FILE = BASE_DIR / "current_url.txt" | |
| def load_or_create_secret() -> str: | |
| if SECRET_FILE.exists(): | |
| return SECRET_FILE.read_text(encoding="utf-8").strip() | |
| value = secrets.token_urlsafe(24) | |
| SECRET_FILE.write_text(value, encoding="utf-8") | |
| return value | |
| def load_ngrok_token() -> str: | |
| token = os.environ.get("NGROK_AUTHTOKEN") | |
| if token: | |
| return token.strip() | |
| if TOKEN_FILE.exists(): | |
| return TOKEN_FILE.read_text(encoding="utf-8").strip() | |
| sys.exit("Lipseste tokenul ngrok: creeaza ngrok_token.txt sau seteaza NGROK_AUTHTOKEN.") | |
| SECRET = load_or_create_secret() | |
| app = Flask(__name__, static_folder="static", template_folder="templates") | |
| @app.route("/") | |
| def index(): | |
| if request.args.get("k") != SECRET: | |
| abort(404) | |
| return render_template("index.html", secret=SECRET) | |
| @app.route("/run", methods=["POST"]) | |
| def run_bat(): | |
| if request.args.get("k") != SECRET and request.headers.get("X-Key") != SECRET: | |
| abort(403) | |
| if not BAT_PATH.exists(): | |
| return jsonify(ok=False, error=f"Lipseste {BAT_PATH}"), 500 | |
| try: | |
| subprocess.Popen( | |
| ["cmd.exe", "/c", "start", "", str(BAT_PATH)], | |
| cwd=str(BAT_PATH.parent), | |
| shell=False, | |
| creationflags=subprocess.CREATE_NEW_CONSOLE, | |
| ) | |
| return jsonify(ok=True, message="Pornit") | |
| except Exception as e: # noqa: BLE001 | |
| return jsonify(ok=False, error=str(e)), 500 | |
| @app.route("/manifest.webmanifest") | |
| def manifest(): | |
| return app.send_static_file("manifest.webmanifest") | |
| def _human_size(n: int) -> str: | |
| for unit in ("B", "KB", "MB", "GB", "TB"): | |
| if n < 1024: | |
| return f"{n:.1f} {unit}" if unit != "B" else f"{n} B" | |
| n /= 1024 | |
| return f"{n:.1f} PB" | |
| def _fmt_date(ts: float) -> str: | |
| return datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M") | |
| def _latest_pdfs(root: Path, limit: int = 2): | |
| items = [] | |
| try: | |
| with os.scandir(root) as it: | |
| for entry in it: | |
| if not entry.is_file(follow_symlinks=False): | |
| continue | |
| if not entry.name.lower().endswith(".pdf"): | |
| continue | |
| try: | |
| st = entry.stat() | |
| except OSError: | |
| continue | |
| items.append((st.st_mtime, st.st_size, Path(entry.path))) | |
| except OSError: | |
| pass | |
| items.sort(key=lambda x: x[0], reverse=True) | |
| return [ | |
| { | |
| "name": p.name, | |
| "path": str(p), | |
| "size": _human_size(int(size)), | |
| "date": _fmt_date(mtime), | |
| } | |
| for mtime, size, p in items[:limit] | |
| ] | |
| def _latest_folders(root: Path, limit: int = 2): | |
| items = [] | |
| try: | |
| with os.scandir(root) as it: | |
| for entry in it: | |
| if not entry.is_dir(follow_symlinks=False): | |
| continue | |
| if entry.name.startswith("$") or entry.name.lower() == "system volume information": | |
| continue | |
| try: | |
| st = entry.stat() | |
| except OSError: | |
| continue | |
| items.append((st.st_mtime, Path(entry.path))) | |
| except OSError: | |
| pass | |
| items.sort(key=lambda x: x[0], reverse=True) | |
| return [ | |
| {"name": p.name, "path": str(p), "date": _fmt_date(mtime)} | |
| for mtime, p in items[:limit] | |
| ] | |
| @app.route("/files") | |
| def files_page(): | |
| if request.args.get("k") != SECRET: | |
| abort(404) | |
| return render_template( | |
| "files.html", | |
| secret=SECRET, | |
| root=str(SCAN_ROOT), | |
| pdfs=_latest_pdfs(SCAN_ROOT), | |
| folders=_latest_folders(SCAN_ROOT), | |
| scanned_at=datetime.now().strftime("%Y-%m-%d %H:%M:%S"), | |
| ) | |
| STATIC_DOMAIN = "seminivorous-overpiteously-nohemi.ngrok-free.dev" | |
| def start_tunnel() -> str: | |
| conf.get_default().auth_token = load_ngrok_token() | |
| tunnel = ngrok.connect(5000, "http", domain=STATIC_DOMAIN) | |
| url = tunnel.public_url.replace("http://", "https://") | |
| full = f"{url}/?k={SECRET}" | |
| URL_FILE.write_text(full, encoding="utf-8") | |
| print("=" * 60) | |
| print("URL PUBLIC (deschide pe telefon, apoi Add to Home Screen):") | |
| print(full) | |
| print("=" * 60, flush=True) | |
| return full | |
| if __name__ == "__main__": | |
| start_tunnel() | |
| app.run(host="127.0.0.1", port=5000, debug=False, use_reloader=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment