refactor: use generic string arguments

This commit is contained in:
ThetaDev 2022-11-30 13:17:28 +01:00
parent 94b55711cb
commit 8097873fe1
12 changed files with 111 additions and 60 deletions

View file

@ -41,12 +41,13 @@ enum Params {
}
impl RustyPipeQuery {
async fn _channel_videos(
async fn _channel_videos<S: AsRef<str>>(
&self,
channel_id: &str,
channel_id: S,
params: Params,
operation: &str,
) -> Result<Channel<Paginator<VideoItem>>, Error> {
let channel_id = channel_id.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QChannel {
context,
@ -57,41 +58,42 @@ impl RustyPipeQuery {
self.execute_request::<response::Channel, _, _>(
ClientType::Desktop,
operation,
channel_id,
channel_id.as_ref(),
"browse",
&request_body,
)
.await
}
pub async fn channel_videos(
pub async fn channel_videos<S: AsRef<str>>(
&self,
channel_id: &str,
channel_id: S,
) -> Result<Channel<Paginator<VideoItem>>, Error> {
self._channel_videos(channel_id, Params::Videos, "channel_videos")
.await
}
pub async fn channel_shorts(
pub async fn channel_shorts<S: AsRef<str>>(
&self,
channel_id: &str,
channel_id: S,
) -> Result<Channel<Paginator<VideoItem>>, Error> {
self._channel_videos(channel_id, Params::Shorts, "channel_shorts")
.await
}
pub async fn channel_livestreams(
pub async fn channel_livestreams<S: AsRef<str>>(
&self,
channel_id: &str,
channel_id: S,
) -> Result<Channel<Paginator<VideoItem>>, Error> {
self._channel_videos(channel_id, Params::Live, "channel_livestreams")
.await
}
pub async fn channel_playlists(
pub async fn channel_playlists<S: AsRef<str>>(
&self,
channel_id: &str,
channel_id: S,
) -> Result<Channel<Paginator<PlaylistItem>>, Error> {
let channel_id = channel_id.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QChannel {
context,
@ -109,7 +111,11 @@ impl RustyPipeQuery {
.await
}
pub async fn channel_info(&self, channel_id: &str) -> Result<Channel<ChannelInfo>, Error> {
pub async fn channel_info<S: AsRef<str>>(
&self,
channel_id: S,
) -> Result<Channel<ChannelInfo>, Error> {
let channel_id = channel_id.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QChannel {
context,

View file

@ -9,10 +9,10 @@ use crate::{
use super::{response, RustyPipeQuery};
impl RustyPipeQuery {
pub async fn channel_rss(&self, channel_id: &str) -> Result<ChannelRss, Error> {
pub async fn channel_rss<S: AsRef<str>>(&self, channel_id: S) -> Result<ChannelRss, Error> {
let url = format!(
"https://www.youtube.com/feeds/videos.xml?channel_id={}",
channel_id
channel_id.as_ref()
);
let xml = self
.client

View file

@ -26,11 +26,13 @@ struct QBrowseParams<'a> {
}
impl RustyPipeQuery {
pub async fn music_artist(
pub async fn music_artist<S: AsRef<str>>(
&self,
artist_id: &str,
artist_id: S,
all_albums: bool,
) -> Result<MusicArtist, Error> {
let artist_id = artist_id.as_ref();
if all_albums {
let visitor_data = self.get_ytm_visitor_data().await?;
let context = self

View file

@ -37,7 +37,8 @@ struct QRadio<'a> {
}
impl RustyPipeQuery {
pub async fn music_details(&self, video_id: &str) -> Result<TrackDetails, Error> {
pub async fn music_details<S: AsRef<str>>(&self, video_id: S) -> Result<TrackDetails, Error> {
let video_id = video_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QMusicDetails {
context,
@ -57,7 +58,8 @@ impl RustyPipeQuery {
.await
}
pub async fn music_lyrics(&self, lyrics_id: &str) -> Result<Lyrics, Error> {
pub async fn music_lyrics<S: AsRef<str>>(&self, lyrics_id: S) -> Result<Lyrics, Error> {
let lyrics_id = lyrics_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QBrowse {
context,
@ -74,7 +76,8 @@ impl RustyPipeQuery {
.await
}
pub async fn music_related(&self, related_id: &str) -> Result<MusicRelated, Error> {
pub async fn music_related<S: AsRef<str>>(&self, related_id: S) -> Result<MusicRelated, Error> {
let related_id = related_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QBrowse {
context,
@ -91,7 +94,11 @@ impl RustyPipeQuery {
.await
}
pub async fn music_radio(&self, radio_id: &str) -> Result<Paginator<TrackItem>, Error> {
pub async fn music_radio<S: AsRef<str>>(
&self,
radio_id: S,
) -> Result<Paginator<TrackItem>, Error> {
let radio_id = radio_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QRadio {
context,
@ -112,15 +119,20 @@ impl RustyPipeQuery {
.await
}
pub async fn music_radio_track(&self, video_id: &str) -> Result<Paginator<TrackItem>, Error> {
self.music_radio(&format!("RDAMVM{}", video_id)).await
pub async fn music_radio_track<S: AsRef<str>>(
&self,
video_id: S,
) -> Result<Paginator<TrackItem>, Error> {
self.music_radio(&format!("RDAMVM{}", video_id.as_ref()))
.await
}
pub async fn music_radio_playlist(
pub async fn music_radio_playlist<S: AsRef<str>>(
&self,
playlist_id: &str,
playlist_id: S,
) -> Result<Paginator<TrackItem>, Error> {
self.music_radio(&format!("RDAMPL{}", playlist_id)).await
self.music_radio(&format!("RDAMPL{}", playlist_id.as_ref()))
.await
}
}

View file

@ -16,7 +16,11 @@ use super::{
};
impl RustyPipeQuery {
pub async fn music_playlist(&self, playlist_id: &str) -> Result<MusicPlaylist, Error> {
pub async fn music_playlist<S: AsRef<str>>(
&self,
playlist_id: S,
) -> Result<MusicPlaylist, Error> {
let playlist_id = playlist_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QBrowse {
context,
@ -33,7 +37,8 @@ impl RustyPipeQuery {
.await
}
pub async fn music_album(&self, album_id: &str) -> Result<MusicAlbum, Error> {
pub async fn music_album<S: AsRef<str>>(&self, album_id: S) -> Result<MusicAlbum, Error> {
let album_id = album_id.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QBrowse {
context,

View file

@ -50,7 +50,8 @@ enum Params {
}
impl RustyPipeQuery {
pub async fn music_search(&self, query: &str) -> Result<MusicSearchResult, Error> {
pub async fn music_search<S: AsRef<str>>(&self, query: S) -> Result<MusicSearchResult, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QSearch {
context,
@ -68,25 +69,26 @@ impl RustyPipeQuery {
.await
}
pub async fn music_search_tracks(
pub async fn music_search_tracks<S: AsRef<str>>(
&self,
query: &str,
query: S,
) -> Result<MusicSearchFiltered<TrackItem>, Error> {
self._music_search_tracks(query, Params::Tracks).await
}
pub async fn music_search_videos(
pub async fn music_search_videos<S: AsRef<str>>(
&self,
query: &str,
query: S,
) -> Result<MusicSearchFiltered<TrackItem>, Error> {
self._music_search_tracks(query, Params::Videos).await
}
async fn _music_search_tracks(
async fn _music_search_tracks<S: AsRef<str>>(
&self,
query: &str,
query: S,
params: Params,
) -> Result<MusicSearchFiltered<TrackItem>, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QSearch {
context,
@ -104,10 +106,11 @@ impl RustyPipeQuery {
.await
}
pub async fn music_search_albums(
pub async fn music_search_albums<S: AsRef<str>>(
&self,
query: &str,
query: S,
) -> Result<MusicSearchFiltered<AlbumItem>, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QSearch {
context,
@ -146,16 +149,16 @@ impl RustyPipeQuery {
.await
}
pub async fn music_search_playlists(
pub async fn music_search_playlists<S: AsRef<str>>(
&self,
query: &str,
query: S,
) -> Result<MusicSearchFiltered<MusicPlaylistItem>, Error> {
self._music_search_playlists(query, Params::Playlists).await
}
pub async fn music_search_playlists_filter(
pub async fn music_search_playlists_filter<S: AsRef<str>>(
&self,
query: &str,
query: S,
community: bool,
) -> Result<MusicSearchFiltered<MusicPlaylistItem>, Error> {
self._music_search_playlists(
@ -168,11 +171,12 @@ impl RustyPipeQuery {
.await
}
async fn _music_search_playlists(
async fn _music_search_playlists<S: AsRef<str>>(
&self,
query: &str,
query: S,
params: Params,
) -> Result<MusicSearchFiltered<MusicPlaylistItem>, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QSearch {
context,
@ -190,7 +194,11 @@ impl RustyPipeQuery {
.await
}
pub async fn music_search_suggestion(&self, query: &str) -> Result<Vec<String>, Error> {
pub async fn music_search_suggestion<S: AsRef<str>>(
&self,
query: S,
) -> Result<Vec<String>, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::DesktopMusic, true, None).await;
let request_body = QSearchSuggestion {
context,

View file

@ -10,12 +10,13 @@ use super::response::music_item::{map_queue_item, MusicListMapper, PlaylistPanel
use super::{response, ClientType, MapResponse, QContinuation, RustyPipeQuery};
impl RustyPipeQuery {
pub async fn continuation<T: FromYtItem>(
pub async fn continuation<T: FromYtItem, S: AsRef<str>>(
&self,
ctoken: &str,
ctoken: S,
endpoint: ContinuationEndpoint,
visitor_data: Option<&str>,
) -> Result<Paginator<T>, Error> {
let ctoken = ctoken.as_ref();
if endpoint.is_music() {
let context = self
.get_context(ClientType::DesktopMusic, true, visitor_data)

View file

@ -58,7 +58,8 @@ struct QContentPlaybackContext {
}
impl RustyPipeQuery {
pub async fn player(&self, video_id: &str) -> Result<VideoPlayer, Error> {
pub async fn player<S: AsRef<str>>(&self, video_id: S) -> Result<VideoPlayer, Error> {
let video_id = video_id.as_ref();
let q1 = self.clone();
let android_res = q1.player_from_client(video_id, ClientType::Android).await;
@ -85,11 +86,12 @@ impl RustyPipeQuery {
}
}
pub async fn player_from_client(
pub async fn player_from_client<S: AsRef<str>>(
&self,
video_id: &str,
video_id: S,
client_type: ClientType,
) -> Result<VideoPlayer, Error> {
let video_id = video_id.as_ref();
let (context, deobf) = tokio::join!(
self.get_context(client_type, false, None),
self.client.get_deobf()

View file

@ -14,7 +14,8 @@ use crate::{
use super::{response, ClientType, MapResponse, MapResult, QBrowse, QContinuation, RustyPipeQuery};
impl RustyPipeQuery {
pub async fn playlist(&self, playlist_id: &str) -> Result<Playlist, Error> {
pub async fn playlist<S: AsRef<str>>(&self, playlist_id: S) -> Result<Playlist, Error> {
let playlist_id = playlist_id.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QBrowse {
context,
@ -31,10 +32,11 @@ impl RustyPipeQuery {
.await
}
pub async fn playlist_continuation(
pub async fn playlist_continuation<S: AsRef<str>>(
&self,
ctoken: &str,
ctoken: S,
) -> Result<Paginator<PlaylistVideo>, Error> {
let ctoken = ctoken.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QContinuation {
context,

View file

@ -21,7 +21,8 @@ struct QSearch<'a> {
}
impl RustyPipeQuery {
pub async fn search(&self, query: &str) -> Result<SearchResult, Error> {
pub async fn search<S: AsRef<str>>(&self, query: S) -> Result<SearchResult, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QSearch {
context,
@ -39,11 +40,12 @@ impl RustyPipeQuery {
.await
}
pub async fn search_filter(
pub async fn search_filter<S: AsRef<str>>(
&self,
query: &str,
query: S,
filter: &SearchFilter,
) -> Result<SearchResult, Error> {
let query = query.as_ref();
let context = self.get_context(ClientType::Desktop, true, None).await;
let request_body = QSearch {
context,
@ -61,9 +63,9 @@ impl RustyPipeQuery {
.await
}
pub async fn search_suggestion(&self, query: &str) -> Result<Vec<String>, Error> {
pub async fn search_suggestion<S: AsRef<str>>(&self, query: S) -> Result<Vec<String>, Error> {
let url = url::Url::parse_with_params("https://suggestqueries-clients6.youtube.com/complete/search?client=youtube&gs_rn=64&gs_ri=youtube&ds=yt&cp=1&gs_id=4&xhr=t&xssi=t",
&[("hl", self.opts.lang.to_string()), ("gl", self.opts.country.to_string()), ("q", query.to_string())]
&[("hl", self.opts.lang.to_string()), ("gl", self.opts.country.to_string()), ("q", query.as_ref().to_owned())]
).map_err(|_| Error::Other("could not build url".into()))?;
let response = self

View file

@ -20,8 +20,12 @@ struct QResolveUrl<'a> {
}
impl RustyPipeQuery {
pub async fn resolve_url(self, url: &str, resolve_albums: bool) -> Result<UrlTarget, Error> {
let (url, params) = util::url_to_params(url)?;
pub async fn resolve_url<S: AsRef<str>>(
self,
url: S,
resolve_albums: bool,
) -> Result<UrlTarget, Error> {
let (url, params) = util::url_to_params(url.as_ref())?;
let mut is_shortlink = url.domain().and_then(|d| match d {
"youtu.be" => Some(true),

View file

@ -1609,8 +1609,15 @@ async fn music_search_episode() {
.await
.unwrap();
let track = &res.items.items[0];
assert_eq!(track.id, "Zq_-LDy7AgE");
let (i, track) = &res
.items
.items
.iter()
.enumerate()
.find(|(_, a)| a.id == "Zq_-LDy7AgE")
.unwrap();
assert!(*i < 3);
assert_eq!(
track.title,
"Blond - Da muss man dabei gewesen sein: Das Hörspiel - Fall #1"