Skip to content

Commit

Permalink
Fixed #52
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgauche committed Jun 6, 2023
1 parent df16daf commit 8364239
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
27 changes: 25 additions & 2 deletions src/tasks/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,7 +51,26 @@ pub fn spawn_api_task(updater_s: Arc<Sender<ManagerMessage>>) {
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 {
Expand Down Expand Up @@ -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((
Expand Down
12 changes: 10 additions & 2 deletions src/term/item_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,19 @@ impl<Action: Clone> ListItem<Action> {
}

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) {
Expand Down
23 changes: 9 additions & 14 deletions ytpapi2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<Self> {
trace!("Creating new YoutubeMusicInstance");
let rest_client = reqwest::ClientBuilder::default()
Expand Down Expand Up @@ -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");
Expand All @@ -337,10 +327,11 @@ impl YoutubeMusicInstance {
}
pub async fn get_library(
&self,
endpoint: &Endpoint,
mut n_continuations: usize,
) -> Result<Vec<YoutubeMusicPlaylistRef>> {
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}");
Expand Down Expand Up @@ -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),
}
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit 8364239

Please sign in to comment.