Skip to content

Commit

Permalink
Merge branch 'development' into localsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
elpiel committed Jul 12, 2023
2 parents ad7c383 + e2d7913 commit 88444c2
Show file tree
Hide file tree
Showing 50 changed files with 789 additions and 159 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ magnet-url = "2.0"
hex = "0.4"

# error handling
anyhow = "1.0.*"
anyhow = "1.0"
thiserror = "1"

regex = "1.8"

# local search and autocomplete functionallity
localsearch = { version = "0.1.0", git = "https://github.com/Stremio/local-search", branch = "main" }

Expand Down
55 changes: 49 additions & 6 deletions src/addon_transport/http_transport/legacy/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::addon_transport::AddonTransport;
use crate::constants::BASE64;
use crate::constants::{BASE64, VIDEO_HASH_EXTRA_PROP, VIDEO_SIZE_EXTRA_PROP};
use crate::runtime::{ConditionalSend, Env, EnvError, EnvFutureExt, TryEnvFuture};
use crate::types::addon::{Manifest, ResourcePath, ResourceResponse};
use crate::types::resource::{MetaItem, MetaItemPreview, Stream, Subtitles};
Expand All @@ -8,6 +8,7 @@ use futures::{future, TryFutureExt};
use http::Request;
use serde::Deserialize;
use serde_json::json;
use std::collections::HashMap;
use std::marker::PhantomData;
use url::Url;

Expand Down Expand Up @@ -199,10 +200,27 @@ fn build_legacy_req(transport_url: &Url, path: &ResourcePath) -> Result<Request<
query.insert("type".into(), serde_json::Value::String(r#type.to_owned()));
build_jsonrpc("stream.find", json!({ "query": query }))
}
"subtitles" => build_jsonrpc(
"subtitles.find",
json!({ "query": json!({ "itemHash": id.replace(':', " ") }) }),
),
"subtitles" => {
let mut query = HashMap::new();
query.insert("itemHash", serde_json::Value::String(id.replace(':', " ")));
let video_hash = path.get_extra_first_value(VIDEO_HASH_EXTRA_PROP.name.as_str());
if let Some(video_hash) = video_hash {
query.insert(
VIDEO_HASH_EXTRA_PROP.name.as_str(),
serde_json::Value::String(video_hash.to_owned()),
);
}
let video_size = path
.get_extra_first_value(VIDEO_SIZE_EXTRA_PROP.name.as_str())
.and_then(|video_size| video_size.parse().ok());
if let Some(video_size) = video_size {
query.insert(
VIDEO_SIZE_EXTRA_PROP.name.as_str(),
serde_json::Value::Number(video_size),
);
}
build_jsonrpc("subtitles.find", json!({ "query": query }))
}
_ => return Err(LegacyErr::UnsupportedRequest.into()),
};
// NOTE: this is not using a URL safe base64 standard, which means that technically this is
Expand Down Expand Up @@ -263,7 +281,7 @@ fn query_from_id(id: &str) -> serde_json::Value {
#[cfg(test)]
mod test {
use super::*;
use crate::types::addon::ResourcePath;
use crate::types::addon::{ExtraExt, ResourcePath};

// Those are a bit sensitive for now, but that's a good thing, since it will force us
// to pay attention to minor details that might matter with the legacy system
Expand All @@ -290,6 +308,31 @@ mod test {
);
}

#[test]
fn subtitles_only_id() {
let transport_url =
Url::parse("https://legacywatchhub.strem.io/stremio/v1").expect("url parse failed");
let path = ResourcePath::without_extra("subtitles", "series", "tt0386676:5:1");
assert_eq!(
&build_legacy_req(&transport_url, &path).unwrap().uri().to_string(),
"https://legacywatchhub.strem.io/stremio/v1/q.json?b=eyJpZCI6MSwianNvbnJwYyI6IjIuMCIsIm1ldGhvZCI6InN1YnRpdGxlcy5maW5kIiwicGFyYW1zIjpbbnVsbCx7InF1ZXJ5Ijp7Iml0ZW1IYXNoIjoidHQwMzg2Njc2IDUgMSJ9fV19"
);
}

#[test]
fn subtitles_with_hash() {
let transport_url =
Url::parse("https://legacywatchhub.strem.io/stremio/v1").expect("url parse failed");
let extra = &vec![]
.extend_one(&VIDEO_HASH_EXTRA_PROP, Some("ffffffffff".to_string()))
.extend_one(&VIDEO_SIZE_EXTRA_PROP, Some("1000000000".to_string()));
let path = ResourcePath::with_extra("subtitles", "series", "tt0386676:5:1", extra);
assert_eq!(
&build_legacy_req(&transport_url, &path).unwrap().uri().to_string(),
"https://legacywatchhub.strem.io/stremio/v1/q.json?b=eyJpZCI6MSwianNvbnJwYyI6IjIuMCIsIm1ldGhvZCI6InN1YnRpdGxlcy5maW5kIiwicGFyYW1zIjpbbnVsbCx7InF1ZXJ5Ijp7Iml0ZW1IYXNoIjoidHQwMzg2Njc2IDUgMSIsInZpZGVvSGFzaCI6ImZmZmZmZmZmZmYiLCJ2aWRlb1NpemUiOjEwMDAwMDAwMDB9fV19"
);
}

#[test]
fn query_meta() {
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const SCHEMA_VERSION_STORAGE_KEY: &str = "schema_version";
pub const PROFILE_STORAGE_KEY: &str = "profile";
pub const LIBRARY_STORAGE_KEY: &str = "library";
pub const LIBRARY_RECENT_STORAGE_KEY: &str = "library_recent";
pub const STREAMS_STORAGE_KEY: &str = "streams";
pub const LIBRARY_COLLECTION_NAME: &str = "libraryItem";
pub const SEARCH_EXTRA_NAME: &str = "search";
pub const META_RESOURCE_NAME: &str = "meta";
Expand Down
105 changes: 92 additions & 13 deletions src/deep_links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,95 @@ use crate::models::installed_addons_with_filters::InstalledAddonsRequest;
use crate::models::library_with_filters::LibraryRequest;
use crate::types::addon::{ExtraValue, ResourcePath, ResourceRequest};
use crate::types::library::LibraryItem;
use crate::types::profile::Settings;
use crate::types::query_params_encode;
use crate::types::resource::{MetaItem, MetaItemPreview, Stream, StreamSource, Video};
use percent_encoding::utf8_percent_encode;
use regex::Regex;
use serde::Serialize;
use url::Url;

#[derive(Default, Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct OpenPlayerLink {
pub ios: Option<String>,
pub android: Option<String>,
pub windows: Option<String>,
pub macos: Option<String>,
pub linux: Option<String>,
pub tizen: Option<String>,
pub webos: Option<String>,
pub chromeos: Option<String>,
pub roku: Option<String>,
}

#[derive(Default, Serialize, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ExternalPlayerLink {
pub href: Option<String>,
pub download: Option<String>,
pub streaming: Option<String>,
pub open_player: Option<OpenPlayerLink>,
pub android_tv: Option<String>,
pub tizen: Option<String>,
pub webos: Option<String>,
pub file_name: Option<String>,
}

impl From<(&Stream, &Option<Url>)> for ExternalPlayerLink {
fn from((stream, streaming_server_url): (&Stream, &Option<Url>)) -> Self {
impl From<(&Stream, &Option<Url>, &Settings)> for ExternalPlayerLink {
fn from((stream, streaming_server_url, settings): (&Stream, &Option<Url>, &Settings)) -> Self {
let http_regex = Regex::new(r"https?://").unwrap();
let download = stream.download_url();
let streaming = stream.streaming_url(streaming_server_url.as_ref());
let m3u_uri = stream.m3u_data_uri(streaming_server_url.as_ref());
let file_name = m3u_uri.as_ref().map(|_| "playlist.m3u".to_owned());
let href = m3u_uri.or_else(|| download.to_owned());
let open_player = match &streaming {
Some(url) => match settings.player_type.as_ref() {
Some(player_type) => match player_type.as_str() {
"choose" => Some(OpenPlayerLink {
android: Some(format!(
"{}#Intent;type=video/any;scheme=https;end",
http_regex.replace(url, "intent://"),
)),
..Default::default()
}),
"vlc" => Some(OpenPlayerLink {
ios: Some(format!("vlc-x-callback://x-callback-url/stream?url={url}")),
android: Some(format!(
"{}#Intent;package=org.videolan.vlc;type=video;scheme=https;end",
http_regex.replace(url, "intent://"),
)),
..Default::default()
}),
"mxplayer" => Some(OpenPlayerLink {
android: Some(format!(
"{}#Intent;package=com.mxtech.videoplayer.ad;type=video;scheme=https;end",
http_regex.replace(url, "intent://"),
)),
..Default::default()
}),
"justplayer" => Some(OpenPlayerLink {
android: Some(format!(
"{}#Intent;package=com.brouken.player;type=video;scheme=https;end",
http_regex.replace(url, "intent://"),
)),
..Default::default()
}),
"outplayer" => Some(OpenPlayerLink {
ios: Some(format!("{}", http_regex.replace(url, "outplayer://"))),
..Default::default()
}),
"infuse" => Some(OpenPlayerLink {
ios: Some(format!("infuse://x-callback-url/play?url={url}")),
..Default::default()
}),
_ => None,
},
None => None,
},
None => None,
};
let (android_tv, tizen, webos) = match &stream.source {
StreamSource::External {
android_tv_url,
Expand All @@ -48,6 +112,7 @@ impl From<(&Stream, &Option<Url>)> for ExternalPlayerLink {
href,
download,
streaming,
open_player,
android_tv,
tizen,
webos,
Expand Down Expand Up @@ -183,9 +248,14 @@ pub struct VideoDeepLinks {
pub external_player: Option<ExternalPlayerLink>,
}

impl From<(&Video, &ResourceRequest, &Option<Url>)> for VideoDeepLinks {
impl From<(&Video, &ResourceRequest, &Option<Url>, &Settings)> for VideoDeepLinks {
fn from(
(video, request, streaming_server_url): (&Video, &ResourceRequest, &Option<Url>),
(video, request, streaming_server_url, settings): (
&Video,
&ResourceRequest,
&Option<Url>,
&Settings,
),
) -> Self {
let stream = video.stream();
VideoDeepLinks {
Expand All @@ -210,9 +280,9 @@ impl From<(&Video, &ResourceRequest, &Option<Url>)> for VideoDeepLinks {
})
.transpose()
.unwrap_or_else(|error| Some(ErrorLink::from(error).into())),
external_player: stream
.as_ref()
.map(|stream| ExternalPlayerLink::from((stream.as_ref(), streaming_server_url))),
external_player: stream.as_ref().map(|stream| {
ExternalPlayerLink::from((stream.as_ref(), streaming_server_url, settings))
}),
}
}
}
Expand All @@ -224,8 +294,8 @@ pub struct StreamDeepLinks {
pub external_player: ExternalPlayerLink,
}

impl From<(&Stream, &Option<Url>)> for StreamDeepLinks {
fn from((stream, streaming_server_url): (&Stream, &Option<Url>)) -> Self {
impl From<(&Stream, &Option<Url>, &Settings)> for StreamDeepLinks {
fn from((stream, streaming_server_url, settings): (&Stream, &Option<Url>, &Settings)) -> Self {
StreamDeepLinks {
player: stream
.encode()
Expand All @@ -236,18 +306,27 @@ impl From<(&Stream, &Option<Url>)> for StreamDeepLinks {
)
})
.unwrap_or_else(|error| ErrorLink::from(error).into()),
external_player: ExternalPlayerLink::from((stream, streaming_server_url)),
external_player: ExternalPlayerLink::from((stream, streaming_server_url, settings)),
}
}
}

impl From<(&Stream, &ResourceRequest, &ResourceRequest, &Option<Url>)> for StreamDeepLinks {
impl
From<(
&Stream,
&ResourceRequest,
&ResourceRequest,
&Option<Url>,
&Settings,
)> for StreamDeepLinks
{
fn from(
(stream, stream_request, meta_request, streaming_server_url): (
(stream, stream_request, meta_request, streaming_server_url, settings): (
&Stream,
&ResourceRequest,
&ResourceRequest,
&Option<Url>,
&Settings,
),
) -> Self {
StreamDeepLinks {
Expand All @@ -265,7 +344,7 @@ impl From<(&Stream, &ResourceRequest, &ResourceRequest, &Option<Url>)> for Strea
)
})
.unwrap_or_else(|error| ErrorLink::from(error).into()),
external_player: ExternalPlayerLink::from((stream, streaming_server_url)),
external_player: ExternalPlayerLink::from((stream, streaming_server_url, settings)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/addon_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Selected {
pub transport_url: Url,
}

#[derive(Default, Serialize)]
#[derive(Default, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AddonDetails {
pub selected: Option<Selected>,
Expand Down
2 changes: 1 addition & 1 deletion src/models/catalogs_with_extra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub type CatalogPage<T> = ResourceLoadable<Vec<T>>;

pub type Catalog<T> = Vec<CatalogPage<T>>;

#[derive(Default, Serialize, Debug)]
#[derive(Default, Clone, Serialize, Debug)]
pub struct CatalogsWithExtra {
pub selected: Option<Selected>,
pub catalogs: Vec<Catalog<MetaItemPreview>>,
Expand Down
2 changes: 1 addition & 1 deletion src/models/continue_watching_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::types::library::{LibraryBucket, LibraryItem};
use lazysort::SortedBy;
use serde::Serialize;

#[derive(Default, Serialize, Debug)]
#[derive(Default, Clone, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
/// The continue watching section in the app
pub struct ContinueWatchingPreview {
Expand Down
Loading

0 comments on commit 88444c2

Please sign in to comment.