Compare commits

..

No commits in common. "6d0302d3bf8e060a5d50afe3b9cef2486d7b8b4c" and "0101fc0c6b87581d14ff1fb0b8bcd3c301ee5bb9" have entirely different histories.

6 changed files with 32 additions and 96 deletions

View file

@ -414,8 +414,7 @@ impl MusicListMapper {
Ok(MusicEntityType::Playlist)
}
PageType::Channel => {
// There may be broken YT channels from the artist search. They can be skipped.
Ok(MusicEntityType::Artist)
Err(format!("channel items unsupported. id: {}", id))
}
}
}

View file

@ -139,7 +139,7 @@ impl PageType {
pub(crate) fn to_url_target(self, id: String) -> UrlTarget {
match self {
PageType::Artist => UrlTarget::Channel { id },
PageType::Album => UrlTarget::Album { id },
PageType::Album => UrlTarget::Playlist { id },
PageType::Channel => UrlTarget::Channel { id },
PageType::Playlist => UrlTarget::Playlist { id },
}

View file

@ -20,7 +20,7 @@ struct QResolveUrl<'a> {
}
impl RustyPipeQuery {
pub async fn resolve_url(self, url: &str, resolve_albums: bool) -> Result<UrlTarget, Error> {
pub async fn resolve_url(self, url: &str) -> Result<UrlTarget, Error> {
let (url, params) = util::url_to_params(url)?;
let mut is_shortlink = url.domain().and_then(|d| match d {
@ -61,17 +61,8 @@ impl RustyPipeQuery {
.ok_or(Error::Other(Cow::Borrowed("invalid url: no playlist id")))?
.to_string();
// YouTube Music album has to be resolved by the YTM API
if resolve_albums && id.starts_with(util::PLAYLIST_ID_ALBUM_PREFIX) {
self._navigation_resolve_url(
&format!("/playlist?list={}", id),
ClientType::DesktopMusic,
)
.await
} else {
Ok(UrlTarget::Playlist { id })
}
}
// Channel vanity URL or youtu.be shortlink
Some(mut id) => {
if id == "c" || id == "user" {
@ -95,8 +86,7 @@ impl RustyPipeQuery {
}
Some(false) => {
// Vanity URL (e.g. youtube.com/LinusTechTips) has to be resolved by the Innertube API
self._navigation_resolve_url(url.path(), ClientType::Desktop)
.await
self._navigation_resolve_url(url.path()).await
}
None => {
// We dont have the original YT domain, so this can be both
@ -108,10 +98,7 @@ impl RustyPipeQuery {
.is_match(url.path())
.unwrap_or_default()
{
match self
._navigation_resolve_url(url.path(), ClientType::Desktop)
.await
{
match self._navigation_resolve_url(url.path()).await {
Ok(target) => Ok(target),
Err(Error::Extraction(ExtractionError::ContentUnavailable(e))) => {
match util::VIDEO_ID_REGEX.is_match(id).unwrap_or_default() {
@ -144,19 +131,14 @@ impl RustyPipeQuery {
Ok(target)
}
pub async fn resolve_string(
self,
string: &str,
resolve_albums: bool,
) -> Result<UrlTarget, Error> {
pub async fn resolve_string(self, string: &str) -> Result<UrlTarget, Error> {
// URL with protocol
if string.starts_with("http://") || string.starts_with("https://") {
self.resolve_url(string, resolve_albums).await
self.resolve_url(string).await
}
// URL without protocol
else if string.contains('/') && string.contains('.') {
self.resolve_url(&format!("https://{}", string), resolve_albums)
.await
self.resolve_url(&format!("https://{}", string)).await
}
// ID only
else if util::VIDEO_ID_REGEX.is_match(string).unwrap_or_default() {
@ -169,54 +151,28 @@ impl RustyPipeQuery {
id: string.to_owned(),
})
} else if util::PLAYLIST_ID_REGEX.is_match(string).unwrap_or_default() {
if resolve_albums && string.starts_with(util::PLAYLIST_ID_ALBUM_PREFIX) {
self._navigation_resolve_url(
&format!("/playlist?list={}", string),
ClientType::DesktopMusic,
)
.await
} else {
Ok(UrlTarget::Playlist {
id: string.to_owned(),
})
}
} else if util::ALBUM_ID_REGEX.is_match(string).unwrap_or_default() {
Ok(UrlTarget::Album {
id: string.to_owned(),
})
}
// Channel name only
else if util::VANITY_PATH_REGEX.is_match(string).unwrap_or_default() {
self._navigation_resolve_url(
&format!("/{}", string.trim_start_matches('/')),
ClientType::Desktop,
)
self._navigation_resolve_url(&format!("/{}", string.trim_start_matches('/')))
.await
} else {
Err(Error::Other("invalid input string".into()))
}
}
async fn _navigation_resolve_url(
&self,
url_path: &str,
ctype: ClientType,
) -> Result<UrlTarget, Error> {
let context = self.get_context(ctype, true, None).await;
async fn _navigation_resolve_url(&self, url_path: &str) -> Result<UrlTarget, Error> {
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QResolveUrl {
context,
url: format!(
"https://{}.youtube.com{}",
match ctype {
ClientType::DesktopMusic => "music",
_ => "www",
},
url_path
),
url: format!("https://www.youtube.com{}", url_path),
};
self.execute_request::<response::ResolvedUrl, _, _>(
ctype,
ClientType::Desktop,
"channel_id",
&request_body.url,
"navigation/resolve_url",
@ -233,24 +189,23 @@ impl MapResponse<UrlTarget> for response::ResolvedUrl {
_lang: Language,
_deobf: Option<&crate::deobfuscate::Deobfuscator>,
) -> Result<MapResult<UrlTarget>, ExtractionError> {
let browse_endpoint = self
.endpoint
.browse_endpoint
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("No browse ID")))?;
let page_type = self
.endpoint
.command_metadata
.map(|c| c.web_command_metadata.web_page_type)
.or_else(|| {
browse_endpoint
.browse_endpoint_context_supported_configs
.map(|c| c.browse_endpoint_context_music_config.page_type)
})
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("No page type")))?;
.ok_or(ExtractionError::InvalidData(Cow::Borrowed(
"No command metadata",
)))?
.web_command_metadata
.web_page_type;
let id = self
.endpoint
.browse_endpoint
.ok_or(ExtractionError::InvalidData(Cow::Borrowed("No browse ID")))?
.browse_id;
Ok(MapResult {
c: page_type.to_url_target(browse_endpoint.browse_id),
c: page_type.to_url_target(id),
warnings: Vec::new(),
})
}

View file

@ -41,7 +41,6 @@ pub enum UrlTarget {
Video { id: String, start_time: u32 },
Channel { id: String },
Playlist { id: String },
Album { id: String },
}
impl ToString for UrlTarget {
@ -67,11 +66,6 @@ impl UrlTarget {
UrlTarget::Playlist { id } => {
format!("{}/playlist?list={}", yt_host, id)
}
UrlTarget::Album { id } => {
// The official album URLs use the playlist ID
// This looks weird, but it works
format!("{}/channel/{}", yt_host, id)
}
}
}
@ -95,12 +89,6 @@ impl UrlTarget {
false => Err(Error::Other("invalid playlist id".into())),
}
}
UrlTarget::Album { id } => {
match util::ALBUM_ID_REGEX.is_match(id).unwrap_or_default() {
true => Ok(()),
false => Err(Error::Other("invalid album id".into())),
}
}
}
}
}

View file

@ -23,9 +23,7 @@ pub static VIDEO_ID_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^[A-Za-z0-9_-
pub static CHANNEL_ID_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^UC[A-Za-z0-9_-]{22}$").unwrap());
pub static PLAYLIST_ID_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(?:PL|RD|OLAK)[A-Za-z0-9_-]{30,}$").unwrap());
pub static ALBUM_ID_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^MPREb_[A-Za-z0-9_-]{11}$").unwrap());
Lazy::new(|| Regex::new(r"^(?:PL|RD)[A-Za-z0-9_-]{30,}$").unwrap());
pub static VANITY_PATH_REGEX: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^/?(?:(?:c\/|user\/)?[A-z0-9]+)|(?:@[A-z0-9-_.]+)$").unwrap());
@ -34,7 +32,6 @@ pub const DOT_SEPARATOR: &str = " • ";
/// YouTube Music name (author of official playlists)
pub const YT_MUSIC_NAME: &str = "YouTube Music";
pub const VARIOUS_ARTISTS: &str = "Various Artists";
pub const PLAYLIST_ID_ALBUM_PREFIX: &str = "OLAK";
const CONTENT_PLAYBACK_NONCE_ALPHABET: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

View file

@ -1165,11 +1165,10 @@ async fn search_suggestion_empty() {
#[case("https://piped.mha.fi/dQw4w9WgXcQ", UrlTarget::Channel {id: "UCoG6BrhgmivrkcbEHcYtK4Q".to_owned()})]
// Both a video ID and a channel name + video time param => returns video
#[case("https://piped.mha.fi/dQw4w9WgXcQ?t=0", UrlTarget::Video {id: "dQw4w9WgXcQ".to_owned(), start_time: 0})]
#[case("https://music.youtube.com/playlist?list=OLAK5uy_k0yFrZlFRgCf3rLPza-lkRmCrtLPbK9pE", UrlTarget::Album {id: "MPREb_GyH43gCvdM5".to_owned()})]
#[tokio::test]
async fn resolve_url(#[case] url: &str, #[case] expect: UrlTarget) {
let rp = RustyPipe::builder().strict().build();
let target = rp.query().resolve_url(url, true).await.unwrap();
let target = rp.query().resolve_url(url).await.unwrap();
assert_eq!(target, expect);
}
@ -1183,12 +1182,10 @@ async fn resolve_url(#[case] url: &str, #[case] expect: UrlTarget) {
#[case("dQw4w9WgXcQ", UrlTarget::Video {id: "dQw4w9WgXcQ".to_owned(), start_time: 0})]
#[case("PL4lEESSgxM_5O81EvKCmBIm_JT5Q7JeaI", UrlTarget::Playlist {id: "PL4lEESSgxM_5O81EvKCmBIm_JT5Q7JeaI".to_owned()})]
#[case("RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk", UrlTarget::Playlist {id: "RDCLAK5uy_kFQXdnqMaQCVx2wpUM4ZfbsGCDibZtkJk".to_owned()})]
#[case("OLAK5uy_k0yFrZlFRgCf3rLPza-lkRmCrtLPbK9pE", UrlTarget::Album {id: "MPREb_GyH43gCvdM5".to_owned()})]
#[case("MPREb_GyH43gCvdM5", UrlTarget::Album {id: "MPREb_GyH43gCvdM5".to_owned()})]
#[tokio::test]
async fn resolve_string(#[case] string: &str, #[case] expect: UrlTarget) {
let rp = RustyPipe::builder().strict().build();
let target = rp.query().resolve_string(string, true).await.unwrap();
let target = rp.query().resolve_string(string).await.unwrap();
assert_eq!(target, expect);
}
@ -1197,7 +1194,7 @@ async fn resolve_channel_not_found() {
let rp = RustyPipe::builder().strict().build();
let err = rp
.query()
.resolve_url("https://www.youtube.com/feeqegnhq3rkwghjq43ruih43io3", true)
.resolve_url("https://www.youtube.com/feeqegnhq3rkwghjq43ruih43io3")
.await
.unwrap_err();