From 8364239677052c582e3febf40c63059ba9a04c4e Mon Sep 17 00:00:00 2001 From: Laurent Gaucheron Date: Tue, 6 Jun 2023 14:57:02 +0200 Subject: [PATCH] Fixed #52 --- src/tasks/api.rs | 27 +++++++++++++++++++++++++-- src/term/item_list.rs | 12 ++++++++++-- ytpapi2/src/lib.rs | 23 +++++++++-------------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/tasks/api.rs b/src/tasks/api.rs index d2df09e..a7fd892 100644 --- a/src/tasks/api.rs +++ b/src/tasks/api.rs @@ -8,7 +8,7 @@ use flume::Sender; use log::{info, error}; use once_cell::sync::Lazy; use tokio::task::JoinSet; -use ytpapi2::{YoutubeMusicInstance, YoutubeMusicPlaylistRef}; +use ytpapi2::{YoutubeMusicInstance, YoutubeMusicPlaylistRef, Endpoint}; use crate::{ run_service, @@ -51,7 +51,26 @@ pub fn spawn_api_task(updater_s: Arc>) { let api_ = api.clone(); let updater_s_ = updater_s.clone(); set.spawn(async move { - let search_results = api_.get_library(2).await; + let search_results = api_.get_library(&Endpoint::MusicLikedPlaylists,2).await; + match search_results { + Ok(e) => { + for playlist in e { + spawn_browse_playlist_task( + playlist.clone(), + api_.clone(), + updater_s_.clone(), + ) + } + } + Err(e) => { + error!("{e:?}"); + } + } + }); + let api_ = api.clone(); + let updater_s_ = updater_s.clone(); + set.spawn(async move { + let search_results = api_.get_library(&Endpoint::MusicLibraryLanding,2).await; match search_results { Ok(e) => { for playlist in e { @@ -123,6 +142,10 @@ fn spawn_browse_playlist_task( let guard = performance::guard(&guard); match api.get_playlist(&playlist, 5).await { Ok(videos) => { + if videos.len() < 2{ + info!("Playlist {} is too small so skipped", playlist.name); + return; + } let _ = updater_s .send( ManagerMessage::AddElementToChooser(( diff --git a/src/term/item_list.rs b/src/term/item_list.rs index aa46b9f..cdf2fb5 100644 --- a/src/term/item_list.rs +++ b/src/term/item_list.rs @@ -110,11 +110,19 @@ impl ListItem { } pub fn select_down(&mut self) { - self.select_to(self.current_position.saturating_add(1)); + if self.current_position == self.list.len() - 1 { + self.select_to(0); + } else { + self.select_to(self.current_position.saturating_add(1)); + } } pub fn select_up(&mut self) { - self.select_to(self.current_position.saturating_sub(1)); + if self.current_position == 0 { + self.select_to(self.list.len() - 1); + } else { + self.select_to(self.current_position.saturating_sub(1)); + } } pub fn select_to(&mut self, position: usize) { diff --git a/ytpapi2/src/lib.rs b/ytpapi2/src/lib.rs index 623b3cb..198c2da 100644 --- a/ytpapi2/src/lib.rs +++ b/ytpapi2/src/lib.rs @@ -55,7 +55,7 @@ fn advanced_like() { .await .unwrap(); println!("{}", ytm.compute_sapi_hash()); - let search = ytm.get_library(0).await.unwrap(); + let search = ytm.get_library(0, &Endpoint::MusicLibraryLanding).await.unwrap(); assert_eq!(search.is_empty(), false); println!("{:?}", search[1]); println!("{:?}", ytm.get_playlist(&search[1], 0).await.unwrap()); @@ -143,16 +143,6 @@ impl YoutubeMusicInstance { Self::new(headers).await } - // pub async fn dump_file(name: &str, content: &str, log_provider: &impl Fn(String)) -> Result<()> { - // let timestamp_millis = SystemTime::now() - // .duration_since(UNIX_EPOCH) - // .unwrap() - // .as_millis(); - // tokio::fs::write(format!("dump/{timestamp_millis}-{name}"), content) - // .await - // .map_err(YoutubeMusicError::IoError) - // } - pub async fn new(headers: HeaderMap) -> Result { trace!("Creating new YoutubeMusicInstance"); let rest_client = reqwest::ClientBuilder::default() @@ -321,7 +311,7 @@ impl YoutubeMusicInstance { ) .await?, ) - .map_err(YoutubeMusicError::SerdeJson)?; + .map_err(YoutubeMusicError::SerdeJson)?; debug!("Browse response: {playlist_json}"); if playlist_json.get("error").is_some() { error!("Error in browse"); @@ -337,10 +327,11 @@ impl YoutubeMusicInstance { } pub async fn get_library( &self, + endpoint: &Endpoint, mut n_continuations: usize, ) -> Result> { let (library_json, mut continuations) = self - .browse(&Endpoint::MusicLikedPlaylists, n_continuations > 0) + .browse(endpoint, n_continuations > 0) .await?; trace!("Fetched library"); debug!("Library response: {library_json}"); @@ -505,9 +496,10 @@ pub struct SearchResults { } #[derive(Debug, Clone, PartialOrd, Eq, Ord, PartialEq, Hash)] -enum Endpoint { +pub enum Endpoint { MusicLikedPlaylists, MusicHome, + MusicLibraryLanding, Playlist(String), Search(String), } @@ -516,6 +508,7 @@ impl Endpoint { fn get_key(&self) -> String { match self { Endpoint::MusicLikedPlaylists => "browseId".to_owned(), + Endpoint::MusicLibraryLanding => "browseId".to_owned(), Endpoint::Playlist(_) => "browseId".to_owned(), Endpoint::MusicHome => "browseId".to_owned(), Endpoint::Search(_) => "query".to_owned(), @@ -524,6 +517,7 @@ impl Endpoint { fn get_param(&self) -> String { match self { Endpoint::MusicLikedPlaylists => "FEmusic_liked_playlists".to_owned(), + Endpoint::MusicLibraryLanding => "FEmusic_library_landing".to_owned(), Endpoint::Playlist(id) => id.to_owned(), Endpoint::Search(query) => query.to_owned(), Endpoint::MusicHome => "FEmusic_home".to_owned(), @@ -532,6 +526,7 @@ impl Endpoint { fn get_route(&self) -> String { match self { Endpoint::MusicLikedPlaylists => "browse".to_owned(), + Endpoint::MusicLibraryLanding => "browse".to_owned(), Endpoint::Playlist(_) => "browse".to_owned(), Endpoint::Search(_) => "search".to_owned(), Endpoint::MusicHome => "browse".to_owned(),