diff --git a/crates/arr-subs/src/opensubtitles.rs b/crates/arr-subs/src/opensubtitles.rs index d4b601b..48b322e 100644 --- a/crates/arr-subs/src/opensubtitles.rs +++ b/crates/arr-subs/src/opensubtitles.rs @@ -251,7 +251,12 @@ impl OpenSubtitles { .header("Api-Key", &self.config.api_key) .header("User-Agent", USER_AGENT); if let Some(query) = query { - request = request.query(query); + // The API answers a 301 when the parameters are not in + // alphabetical order, so sort before sending rather than trust a + // redirect to carry the request intact (#270). + let mut query = query.to_vec(); + query.sort_by(|(left, _), (right, _)| left.cmp(right)); + request = request.query(&query); } if let Some(json) = json { request = request.json(json); @@ -343,25 +348,7 @@ impl OpenSubtitles { source, })?; - let mut params: Vec<(String, String)> = - vec![("languages".to_owned(), languages_param(&request.languages))]; - match request.file.media { - MediaRef::Movie { tmdb_id } => { - params.push(("tmdb_movie_id".to_owned(), tmdb_id.to_string())); - } - MediaRef::Episode { - tmdb_id, - season, - episode, - } => { - params.push(("tmdb_series_id".to_owned(), tmdb_id.to_string())); - params.push(("season_number".to_owned(), season.to_string())); - params.push(("episode_number".to_owned(), episode.to_string())); - } - } - if let Some(hash) = hash.as_deref() { - params.push(("moviehash".to_owned(), hash.to_owned())); - } + let params = search_params(request, hash.as_deref()); let response = self .send(Method::GET, "subtitles", Some(¶ms), None, None) @@ -579,6 +566,39 @@ struct SubtitleFileRef { file_id: u64, } +/// The query a search sends, in the parameter names the API documents. +/// +/// `tmdb_movie_id` and `tmdb_series_id` — what this sent until #270 — are not +/// parameters of this API. Unknown ones are ignored rather than refused, so +/// the id lane contributed nothing and every search came back as whatever the +/// `moviehash` alone matched: one result for a popular release, none at all +/// for anything the hash database has never seen. +fn search_params(request: &SearchRequest, hash: Option<&str>) -> Vec<(String, String)> { + let mut params: Vec<(String, String)> = + vec![("languages".to_owned(), languages_param(&request.languages))]; + match request.file.media { + MediaRef::Movie { tmdb_id } => { + params.push(("tmdb_id".to_owned(), tmdb_id.to_string())); + } + MediaRef::Episode { + tmdb_id, + season, + episode, + } => { + params.push(("parent_tmdb_id".to_owned(), tmdb_id.to_string())); + params.push(("season_number".to_owned(), season.to_string())); + params.push(("episode_number".to_owned(), episode.to_string())); + } + } + // Sent alongside the id rather than instead of it: the API answers the id + // search and flags the entries the hash also matched, which is what + // ranking reads. + if let Some(hash) = hash { + params.push(("moviehash".to_owned(), hash.to_owned())); + } + params +} + /// The API spells languages `pt-PT`, `pt-BR`, `en`; accept `_` too, since /// tooling around this API uses it. fn language_of(code: &str) -> Option { @@ -594,16 +614,21 @@ fn language_of(code: &str) -> Option { /// The `languages` query parameter: comma-separated codes, pt-PT and pt-BR /// kept apart — keeping them apart is why this is the primary provider. fn languages_param(languages: &[Language]) -> String { - languages + // Lowercase and sorted, both of which the API requires to answer rather + // than redirect (#270). Case is not what keeps pt-PT and pt-BR apart -- + // the region subtag is -- so lowercasing costs nothing. + let mut codes: Vec = languages .iter() .map(|language| match language { - Language::PortuguesePortugal => "pt-PT", - Language::PortugueseBrazil => "pt-BR", - Language::PortugueseUnverified => "pt", - Language::Other(tag) => tag.as_str(), + Language::PortuguesePortugal => "pt-pt".to_owned(), + Language::PortugueseBrazil => "pt-br".to_owned(), + Language::PortugueseUnverified => "pt".to_owned(), + Language::Other(tag) => tag.to_ascii_lowercase(), }) - .collect::>() - .join(",") + .collect(); + codes.sort(); + codes.dedup(); + codes.join(",") } /// The release group of the release a subtitle was timed against, read off @@ -658,7 +683,8 @@ fn rank_candidates( #[cfg(test)] mod tests { - use super::{format_of, language_of, moviehash, rank_candidates, PROVIDER_NAME}; + use super::{format_of, language_of, moviehash, rank_candidates, search_params, PROVIDER_NAME}; + use crate::SearchRequest; use crate::{Candidate, CandidateId, MediaFile, MediaRef, ProviderId, SubtitleFormat}; use arr_core::Language; @@ -718,7 +744,68 @@ mod tests { Language::PortugueseUnverified, Language::Other("en".to_owned()), ]), - "pt-PT,pt-BR,pt,en" + "en,pt,pt-br,pt-pt" + ); + } + + fn request(media: MediaRef, languages: Vec) -> SearchRequest { + SearchRequest { + file: MediaFile { + path: std::path::PathBuf::from("/media/x.mkv"), + size: 1, + release_name: None, + media, + }, + languages, + } + } + + /// #270: the id lane sent parameter names this API does not have. It + /// ignores unknown ones, so every search silently degraded to a + /// `moviehash` lookup and answered nothing for a release no one has + /// hashed. + #[test] + fn a_movie_search_asks_by_tmdb_id() { + let params = search_params( + &request( + MediaRef::Movie { tmdb_id: 272 }, + vec![Language::Other("en".to_owned())], + ), + Some("abc123"), + ); + + assert_eq!( + params, + vec![ + ("languages".to_owned(), "en".to_owned()), + ("tmdb_id".to_owned(), "272".to_owned()), + ("moviehash".to_owned(), "abc123".to_owned()), + ] + ); + } + + #[test] + fn an_episode_search_asks_by_the_series_tmdb_id_and_the_numbers() { + let params = search_params( + &request( + MediaRef::Episode { + tmdb_id: 60625, + season: 2, + episode: 4, + }, + vec![Language::PortuguesePortugal], + ), + None, + ); + + assert_eq!( + params, + vec![ + ("languages".to_owned(), "pt-pt".to_owned()), + ("parent_tmdb_id".to_owned(), "60625".to_owned()), + ("season_number".to_owned(), "2".to_owned()), + ("episode_number".to_owned(), "4".to_owned()), + ] ); } diff --git a/crates/arr-subs/tests/opensubtitles.rs b/crates/arr-subs/tests/opensubtitles.rs index 4964a0c..1a5f97f 100644 --- a/crates/arr-subs/tests/opensubtitles.rs +++ b/crates/arr-subs/tests/opensubtitles.rs @@ -92,9 +92,9 @@ async fn movie_search_sends_both_lanes_and_ranks_hash_match_first() { Mock::given(method("GET")) .and(path("/api/v1/subtitles")) .and(header("Api-Key", "test-api-key")) - .and(query_param("tmdb_movie_id", "693134")) + .and(query_param("tmdb_id", "693134")) .and(query_param("moviehash", "0000000000040000")) - .and(query_param("languages", "pt-PT,pt-BR,en")) + .and(query_param("languages", "en,pt-br,pt-pt")) .respond_with(ResponseTemplate::new(200).set_body_string(SEARCH_MOVIE)) .mount(&server) .await; @@ -139,7 +139,7 @@ async fn episode_search_addresses_the_series_not_the_movie_lane() { Mock::given(method("GET")) .and(path("/api/v1/subtitles")) - .and(query_param("tmdb_series_id", "94605")) + .and(query_param("parent_tmdb_id", "94605")) .and(query_param("season_number", "3")) .and(query_param("episode_number", "7")) .respond_with(ResponseTemplate::new(200).set_body_string(SEARCH_EPISODE)) @@ -182,7 +182,7 @@ async fn a_file_too_small_to_hash_searches_without_the_hash_lane() { Mock::given(method("GET")) .and(path("/api/v1/subtitles")) - .and(query_param("tmdb_movie_id", "42")) + .and(query_param("tmdb_id", "42")) .and(QueryParamMissing("moviehash")) .respond_with(ResponseTemplate::new(200).set_body_string(SEARCH_EPISODE)) .mount(&server)