/* scorpiox-scorpioplayer-subtitle-fetch — find and download subtitle / lyric files. * * Pure C99. Bytes + metadata out; parsing is scorpiox-scorpioplayer-subtitle-parse's job. * v2.0.0: transport is the fleet scorpiox_http vtable (scorpiox-http.h: any method, request * headers, binary bodies with a length, Range). The v1 scorpiox_io face (spsubfetch_set_io) * still works. All 34 v1.0.0 exports are unchanged; 21 are added. See SPEC.md (D1-D50). * * Not thread-safe: one process-global engine, like every scorpiox-scorpioplayer-* sibling. */ #ifndef SPSUBFETCH_H #define SPSUBFETCH_H #include #include "scorpiox-io.h" #include "scorpiox-http.h" #if defined(SPSF_STATIC) # define SPSF_API #elif defined(_WIN32) # if defined(SPSF_BUILD) # define SPSF_API __declspec(dllexport) # else # define SPSF_API __declspec(dllimport) # endif #else # define SPSF_API __attribute__((visibility("default"))) #endif #ifdef __cplusplus extern "C" { #endif /* ---- return codes (D25) — numbers match spmovie, plus RESTRICTED ---- */ #define SPSF_OK 0 #define SPSF_ERR_TRANSPORT 1 #define SPSF_ERR_HTTP 2 #define SPSF_ERR_PARSE 3 #define SPSF_ERR_EMPTY 4 #define SPSF_ERR_NOT_FOUND 5 #define SPSF_ERR_INVALID_ARG 6 #define SPSF_ERR_NO_IO 7 #define SPSF_ERR_OOM 8 #define SPSF_ERR_RESTRICTED 9 #define SPSF_ERR_NOT_ARMED 10 /* 2.0.0: a cloud call while writes are not armed (D45) */ /* ---- providers: bitmask (D2) ---- */ #define SPSF_P_SHOOTER 1 #define SPSF_P_OSDB 2 #define SPSF_P_SUBHUB 4 #define SPSF_P_NETEASE 8 #define SPSF_P_SUBTITLES 7 /* SHOOTER|OSDB|SUBHUB — the v1.0.0 mask */ /* 2.0.0 providers */ #define SPSF_P_OSDB_REST 16 /* api.opensubtitles.com/api/v1 (Api-Key, optional JWT login) */ #define SPSF_P_STREMIO_OSDB 32 /* opensubtitles-v3.strem.io (zero key; IMDb + S/E + optional hash) */ #define SPSF_P_SUBDL 64 /* api.subdl.com/api/v1 (Api-Key) */ #define SPSF_P_CLOUD 128 /* host-only: gcp.scorpioplayer.com (/api/fs/cf, /api/KeyValue) */ /* providers == 0 means this mask, + OSDB_REST / SUBDL when their Api-Key is set (D34). * The dead anonymous XML-RPC (SPSF_P_OSDB) is no longer in any default. */ #define SPSF_P_DEFAULT (SPSF_P_SUBHUB | SPSF_P_STREMIO_OSDB | SPSF_P_SHOOTER) /* ---- match method of a result (Subhub.cs SearchMethod values) ---- */ #define SPSF_MATCH_UNKNOWN 0 #define SPSF_MATCH_HASH 1 #define SPSF_MATCH_FULLTEXT 2 #define SPSF_MATCH_IMDB 4 /* ---- per-provider status after the last search (D26) ---- */ #define SPSF_ST_NOT_QUERIED 0 #define SPSF_ST_WORKING 1 /* >= 1 result */ #define SPSF_ST_EMPTY 2 /* reachable, valid response, zero results */ #define SPSF_ST_PARSE 3 /* reachable, 2xx, response unusable: ROT */ #define SPSF_ST_DOWN 4 /* transport failure / 5xx / unexpected 4xx */ #define SPSF_ST_RESTRICTED 5 /* answered but refuses (VIP nag, 401, 403) */ /* ---- Subhub search methods (Subhub.cs SearchMethod) ---- */ #define SPSF_SUBHUB_HASH 1 #define SPSF_SUBHUB_FULLTEXT 2 #define SPSF_SUBHUB_IMDB 4 /* ---- lifecycle / transport ---- */ SPSF_API const char *spsubfetch_version(void); SPSF_API void spsubfetch_set_io(const scorpiox_io *io); /* copied; NULL = uninstall */ SPSF_API int spsubfetch_set_fake_io(const char *canned_dir); /* offline replay backend (D27) */ SPSF_API int spsubfetch_set_host(int provider, const char *base_url); /* NULL = default (D29) */ SPSF_API const char *spsubfetch_host(int provider); SPSF_API void spsubfetch_shutdown(void); /* frees results, token, io */ SPSF_API const char *spsubfetch_last_error(void); SPSF_API long spsubfetch_last_http_code(void); SPSF_API const char *spsubfetch_error_name(int code); SPSF_API const char *spsubfetch_status_name(int status); /* ---- hashes — delegated to vendored scorpiox-scorpioplayer-media-hash (D24) ---- */ SPSF_API const char *spsubfetch_hash_shooter(const char *path); /* NULL on failure */ SPSF_API const char *spsubfetch_hash_opensubtitles(const char *path); /* NULL on failure */ SPSF_API long long spsubfetch_file_size(const char *path); /* -1 on failure */ /* ---- search ---- */ /* Any argument may be NULL/0 if unknown. providers = SPSF_P_* mask (0 = SPSF_P_SUBTITLES). */ SPSF_API int spsubfetch_search(const char *hash_shooter, const char *hash_osdb, long long file_size, const char *filename, const char *lang, int providers); /* One Subhub query with an explicit method (SubhubDialog.xaml.cs:153-170). */ SPSF_API int spsubfetch_search_subhub(int method, const char *query, long long file_size, const char *lang); /* Audio-only path: lyrics by title + artist (CaptionService.cs:69-80, D20). */ SPSF_API int spsubfetch_search_by_title(const char *title, const char *artist, const char *lang); SPSF_API int spsubfetch_provider_status(int provider); /* single SPSF_P_* bit */ SPSF_API long spsubfetch_provider_http_code(int provider); /* ---- results (engine-owned, valid until next search / shutdown) ---- */ SPSF_API int spsubfetch_result_count(void); SPSF_API int spsubfetch_result_provider(int i); /* SPSF_P_* bit, 0 if out of range */ SPSF_API const char *spsubfetch_result_filename(int i); SPSF_API const char *spsubfetch_result_language(int i); SPSF_API const char *spsubfetch_result_ext(int i); /* ".srt"; "" until known */ SPSF_API const char *spsubfetch_result_id(int i); SPSF_API int spsubfetch_download(int i); SPSF_API const unsigned char *spsubfetch_data(int i, size_t *len); /* raw bytes; caller copies */ /* ---- OSDbConfig language table (OSDbConfig.cs:36-109, D11) ---- */ SPSF_API int spsubfetch_lang_count(void); SPSF_API const char *spsubfetch_lang_iso6392(int i); SPSF_API const char *spsubfetch_lang_iso6391(int i); SPSF_API const char *spsubfetch_lang_name(int i); SPSF_API const char *spsubfetch_lang_to_osdb(const char *langs); /* "en,fr" -> "eng,fre" */ /* ---- fake backend introspection (tests / .console harness prove the request shape) ---- */ SPSF_API const char *spsubfetch_fake_last_url(void); SPSF_API const char *spsubfetch_fake_last_body(void); SPSF_API int spsubfetch_fake_request_count(void); /* ======================= 2.0.0 additions (21) ======================= */ /* ---- transport & provider auth (6) ---- */ /* Install a fleet transport (copied; its ctx must outlive the engine's use of it). * NULL = the built-in default (libcurl on Linux, WinHTTP on Windows desktop); ERR_NO_IO when * this build has none (UWP, Android, Apple: the shell installs its own). */ SPSF_API int spsubfetch_set_http_backend(const scorpiox_http *http); /* provider = SPSF_P_OSDB_REST or SPSF_P_SUBDL. NULL/"" clears. Wiped on shutdown, never echoed * in last_error, redacted (***) in every fake_* introspection. */ SPSF_API int spsubfetch_set_provider_api_key(int provider, const char *api_key); /* provider = SPSF_P_OSDB_REST: optional login for the authenticated download quota. */ SPSF_API int spsubfetch_set_provider_credentials(int provider, const char *user, const char *pass); SPSF_API int spsubfetch_set_user_agent(const char *ua); /* NULL = default */ /* Offline replay (routes.txt, binary-safe, optional response-header file, `range` code). */ SPSF_API int spsubfetch_set_fake_http(const char *canned_dir); SPSF_API const char *spsubfetch_fake_last_headers_json(void); /* {"Name":"value",...} */ /* ---- remote / chunk hashing (3) ---- */ /* Same contract as spmhash_*_chunks (media-hash 1.1.0, vendored). Return SPSF_* codes: * bad arguments / small out_cap -> INVALID_ARG, shooter <= 8192 B without name -> EMPTY. */ SPSF_API int spsubfetch_hash_opensubtitles_chunks(const unsigned char *head, size_t head_len, const unsigned char *tail, size_t tail_len, long long total_size, char *out_hex, size_t out_cap); SPSF_API int spsubfetch_hash_shooter_chunks(const unsigned char *b0, size_t l0, const unsigned char *b1, size_t l1, const unsigned char *b2, size_t l2, const unsigned char *b3, size_t l3, long long total_size, const char *fallback_name, char *out_hex, size_t out_cap); /* Hash a remote file through HTTP Range: <= 128 KiB (+8 KiB for Shooter), 2-4 requests. * headers_json: NULL, {"Name":"value"} or ["Name: value"] (cookies, auth for the stream). * hash_mask: 1 = OpenSubtitles, 2 = Shooter, 0 = both. * out_json: {"osdb":"..","shooter":"..","file_size":N,"requests":n,"bytes":b}. * A server that ignores Range (HTTP 200) is aborted after its first chunk; 200/403/416 -> * SPSF_ERR_NOT_FOUND so the caller falls back to IMDb / title search. */ SPSF_API int spsubfetch_hash_http_url(const char *url, const char *headers_json, int hash_mask, char *out_json, size_t out_cap); /* ---- structured search & rich metadata (8) ---- */ /* request_json: {"imdb_id":"tt0111161","season":0,"episode":0,"query":"...","filename":"...", * "hash_osdb":"...","hash_shooter":"...","file_size":0,"lang":"en,zh-cn", * "providers":0} (every field optional; at least one identifier) * Results are de-duplicated by (provider, id) and ranked: Hash -> IMDb(+S/E) -> FullText, * then download_count desc, then non-hearing-impaired first (D38). */ SPSF_API int spsubfetch_search_ex(const char *request_json); SPSF_API const char *spsubfetch_results_json(void); /* ranked array; valid until next call */ SPSF_API int spsubfetch_result_download_count(int i); /* -1 unknown */ SPSF_API int spsubfetch_result_rating_permille(int i); /* 0..1000 of the provider scale, -1 unknown */ SPSF_API const char *spsubfetch_result_uploader(int i); /* "" unknown */ SPSF_API int spsubfetch_result_hearing_impaired(int i);/* 1 / 0 / -1 unknown */ SPSF_API int spsubfetch_result_match_method(int i); /* SPSF_MATCH_* */ SPSF_API const char *spsubfetch_lang_to_rest(const char *langs);/* "chi,zht,en" -> "zh-cn,zh-tw,en" */ /* ---- write-armed cloud persistence (4) — D45 ---- * Every cloud call returns SPSF_ERR_NOT_ARMED and sends NOTHING until spsubfetch_arm_writes(1). * shutdown disarms. Keys: SMF_ExternalSubtitle_{imdb}_{s}_{e}_{filename} (shared) and * SMF_ExternalSubtitleEnabled_{uid}_{pid}_{imdb}_{s}_{e} (per user/profile). */ SPSF_API void spsubfetch_arm_writes(int armed); /* Downloads result i if needed, uploads it to /api/fs/cf, registers the shared key, writes the * hosted URL (https://.../dl/subtitles/{stable(filename)}{ext}) to out_url. */ SPSF_API int spsubfetch_cloud_share(int i, const char *imdb_id, int season, int episode, char *out_url, size_t out_cap); SPSF_API int spsubfetch_cloud_set_enabled(const char *user_id, int profile_id, const char *imdb_id, int season, int episode, const char *track_ids_csv); SPSF_API int spsubfetch_cloud_get_enabled(const char *user_id, int profile_id, const char *imdb_id, int season, int episode, char *out_csv, size_t out_cap); #ifdef __cplusplus } #endif #endif