Skip to content

Commit

Permalink
refactor: simplify logic for ExternalPlayerLink
Browse files Browse the repository at this point in the history
  • Loading branch information
tymmesyde committed Nov 20, 2023
1 parent 1fd2cdd commit be8ebd6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 93 deletions.
8 changes: 3 additions & 5 deletions src/deep_links/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,11 @@ impl From<(&Stream, Option<&Url>, &Settings)> for ExternalPlayerLink {
///
/// [`StreamingServer::base_url`]: crate::models::streaming_server::StreamingServer::base_url
fn from((stream, streaming_server_url, settings): (&Stream, Option<&Url>, &Settings)) -> Self {
let streaming_server_available = streaming_server_url.is_some();
let streaming_server_url = streaming_server_url.map(|_| &settings.streaming_server_url);
let http_regex = Regex::new(r"https?://").unwrap();
let download = stream.download_url();
let streaming =
stream.streaming_url(streaming_server_available, &settings.streaming_server_url);
let m3u_uri =
stream.m3u_data_uri(streaming_server_available, &settings.streaming_server_url);
let streaming = stream.streaming_url(streaming_server_url);
let m3u_uri = stream.m3u_data_uri(streaming_server_url);
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 {
Expand Down
163 changes: 75 additions & 88 deletions src/types/resource/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,98 +126,85 @@ impl Stream {
StreamSource::PlayerFrame { player_frame_url } => Some(player_frame_url.to_string()),
}
}
pub fn m3u_data_uri(
&self,
streaming_server_available: bool,
streaming_server_url: &Url,
) -> Option<String> {
self.streaming_url(streaming_server_available, streaming_server_url)
.map(|url| {
format!(
"data:application/octet-stream;charset=utf-8;base64,{}",
BASE64.encode(format!("#EXTM3U\n#EXTINF:0\n{url}"))
)
})
pub fn m3u_data_uri(&self, streaming_server_url: Option<&Url>) -> Option<String> {
self.streaming_url(streaming_server_url).map(|url| {
format!(
"data:application/octet-stream;charset=utf-8;base64,{}",
BASE64.encode(format!("#EXTM3U\n#EXTINF:0\n{url}"))
)
})
}
pub fn streaming_url(
&self,
streaming_server_available: bool,
streaming_server_url: &Url,
) -> Option<String> {
if streaming_server_available {
match (&self.source, streaming_server_url) {
(StreamSource::Url { url }, streaming_server_url) if url.scheme() != "magnet" => {
match &self.behavior_hints.proxy_headers {
Some(StreamProxyHeaders { request, response }) => {
let mut streaming_url = streaming_server_url.to_owned();
let mut proxy_query = form_urlencoded::Serializer::new(String::new());
let origin = format!("{}://{}", url.scheme(), url.authority());
proxy_query.append_pair("d", origin.as_str());
proxy_query.extend_pairs(
request
.iter()
.map(|header| ("h", format!("{}:{}", header.0, header.1))),
);
proxy_query.extend_pairs(
response
.iter()
.map(|header| ("r", format!("{}:{}", header.0, header.1))),
);
streaming_url
.path_segments_mut()
.ok()?
.push("proxy")
.push(proxy_query.finish().as_str())
.push(&url.path()[1..]);
streaming_url.set_query(url.query());
Some(streaming_url.to_string())
}
_ => Some(url.to_string()),
pub fn streaming_url(&self, streaming_server_url: Option<&Url>) -> Option<String> {
match (&self.source, streaming_server_url) {
(StreamSource::Url { url }, Some(streaming_server_url)) if url.scheme() != "magnet" => {
match &self.behavior_hints.proxy_headers {
Some(StreamProxyHeaders { request, response }) => {
let mut streaming_url = streaming_server_url.to_owned();
let mut proxy_query = form_urlencoded::Serializer::new(String::new());
let origin = format!("{}://{}", url.scheme(), url.authority());
proxy_query.append_pair("d", origin.as_str());
proxy_query.extend_pairs(
request
.iter()
.map(|header| ("h", format!("{}:{}", header.0, header.1))),
);
proxy_query.extend_pairs(
response
.iter()
.map(|header| ("r", format!("{}:{}", header.0, header.1))),
);
streaming_url
.path_segments_mut()
.ok()?
.push("proxy")
.push(proxy_query.finish().as_str())
.push(&url.path()[1..]);
streaming_url.set_query(url.query());
Some(streaming_url.to_string())
}
_ => Some(url.to_string()),
}
(
StreamSource::Torrent {
info_hash,
file_idx,
announce,
},
streaming_server_url,
) => {
let mut url = streaming_server_url.to_owned();
match url.path_segments_mut() {
Ok(mut path) => {
path.push(&hex::encode(info_hash));
// When fileIndex is not provided use -1, which will tell the
// streaming server to choose the file with the largest size from the torrent
path.push(
&file_idx.map_or_else(|| "-1".to_string(), |idx| idx.to_string()),
);
}
_ => return None,
};
if !announce.is_empty() {
let mut query = url.query_pairs_mut();
query.extend_pairs(announce.iter().map(|tracker| ("tr", tracker)));
};
Some(url.to_string())
}
(StreamSource::YouTube { yt_id }, streaming_server_url) => {
let mut url = streaming_server_url.to_owned();
match url.path_segments_mut() {
Ok(mut path) => {
path.push("yt");
path.push(
&utf8_percent_encode(yt_id, URI_COMPONENT_ENCODE_SET).to_string(),
);
}
_ => return None,
};
Some(url.to_string())
}
_ => None,
}
} else {
None
(
StreamSource::Torrent {
info_hash,
file_idx,
announce,
},
Some(streaming_server_url),
) => {
let mut url = streaming_server_url.to_owned();
match url.path_segments_mut() {
Ok(mut path) => {
path.push(&hex::encode(info_hash));
// When fileIndex is not provided use -1, which will tell the
// streaming server to choose the file with the largest size from the torrent
path.push(
&file_idx.map_or_else(|| "-1".to_string(), |idx| idx.to_string()),
);
}
_ => return None,
};
if !announce.is_empty() {
let mut query = url.query_pairs_mut();
query.extend_pairs(announce.iter().map(|tracker| ("tr", tracker)));
};
Some(url.to_string())
}
(StreamSource::YouTube { yt_id }, Some(streaming_server_url)) => {
let mut url = streaming_server_url.to_owned();
match url.path_segments_mut() {
Ok(mut path) => {
path.push("yt");
path.push(
&utf8_percent_encode(yt_id, URI_COMPONENT_ENCODE_SET).to_string(),
);
}
_ => return None,
};
Some(url.to_string())
}
_ => None,
}
}
pub fn youtube_url(&self) -> Option<String> {
Expand Down

0 comments on commit be8ebd6

Please sign in to comment.