#!/usr/bin/env python3 """Fetch subtitles from OpenSubtitles REST API (needs OPENSUBTITLES_API_KEY; downloads also need OPENSUBTITLES_USERNAME / OPENSUBTITLES_PASSWORD — free account gives ~20 downloads/day). usage: opensubs.py Tries moviehash match first (exact sync for this cut), then imdb/title query; picks most downloaded. exit 2 = nothing found / no key (caller falls back to Whisper + translation) """ import json, os, struct, sys, urllib.parse, urllib.request API = "https://api.opensubtitles.com/api/v1" KEY = os.environ.get("OPENSUBTITLES_API_KEY") if not KEY: print("no OPENSUBTITLES_API_KEY", file=sys.stderr); sys.exit(2) def req(path, data=None, token=None): h = {"Api-Key": KEY, "User-Agent": "wjs-fetching-movie v1", "Content-Type": "application/json", "Accept": "application/json"} if token: h["Authorization"] = f"Bearer {token}" r = urllib.request.Request(API + path, data=json.dumps(data).encode() if data else None, headers=h) with urllib.request.urlopen(r, timeout=30) as resp: return json.load(resp) def moviehash(path): size = os.path.getsize(path); h = size; chunk = 65536 with open(path, "rb") as f: for off in (0, max(0, size - chunk)): f.seek(off); buf = f.read(chunk) for (v,) in struct.iter_unpack("