feat: Select market to support track relinking (#76)

This commit is contained in:
grufkork 2024-07-14 23:43:46 +02:00 committed by GitHub
parent d45267b4d3
commit f83b989f5e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 9 deletions

View file

@ -80,6 +80,7 @@ async fn start() {
&settings.password, &settings.password,
&settings.client_id, &settings.client_id,
&settings.client_secret, &settings.client_secret,
settings.market_country_code,
) )
.await .await
{ {

View file

@ -1,5 +1,6 @@
use crate::downloader::DownloaderConfig; use crate::downloader::DownloaderConfig;
use crate::error::SpotifyError; use crate::error::SpotifyError;
use aspotify::CountryCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::{ use tokio::{
@ -22,6 +23,7 @@ pub struct Settings {
pub client_secret: String, pub client_secret: String,
pub refresh_ui_seconds: u64, pub refresh_ui_seconds: u64,
pub downloader: DownloaderConfig, pub downloader: DownloaderConfig,
pub market_country_code: Option<CountryCode>,
} }
// On UNIX systems (eg. Linux, *BSD, even macOS), follow the // On UNIX systems (eg. Linux, *BSD, even macOS), follow the
@ -59,6 +61,7 @@ impl Settings {
client_secret: client_secret.to_string(), client_secret: client_secret.to_string(),
refresh_ui_seconds: 1, refresh_ui_seconds: 1,
downloader: DownloaderConfig::new(), downloader: DownloaderConfig::new(),
market_country_code: None,
} }
} }

View file

@ -1,6 +1,6 @@
use aspotify::{ use aspotify::{
Album, Artist, Client, ClientCredentials, ItemType, Playlist, PlaylistItemType, Track, Album, Artist, Client, ClientCredentials, CountryCode, ItemType, Market, Playlist,
TrackSimplified, PlaylistItemType, Track, TrackSimplified,
}; };
use librespot::core::authentication::Credentials; use librespot::core::authentication::Credentials;
use librespot::core::cache::Cache; use librespot::core::cache::Cache;
@ -16,6 +16,7 @@ pub struct Spotify {
// librespotify sessopm // librespotify sessopm
pub session: Session, pub session: Session,
pub spotify: Client, pub spotify: Client,
pub market: Option<Market>,
} }
impl Spotify { impl Spotify {
@ -25,6 +26,7 @@ impl Spotify {
password: &str, password: &str,
client_id: &str, client_id: &str,
client_secret: &str, client_secret: &str,
market_country_code: Option<CountryCode>,
) -> Result<Spotify, SpotifyError> { ) -> Result<Spotify, SpotifyError> {
// librespot // librespot
let credentials = Credentials::with_password(username, password); let credentials = Credentials::with_password(username, password);
@ -43,7 +45,15 @@ impl Spotify {
}; };
let spotify = Client::new(credentials); let spotify = Client::new(credentials);
Ok(Spotify { session, spotify }) Ok(Spotify {
session,
spotify,
market: if let Some(countrycode) = market_country_code {
Some(Market::Country(countrycode))
} else {
None
},
})
} }
/// Parse URI or URL into URI /// Parse URI or URL into URI
@ -78,15 +88,19 @@ impl Spotify {
let id = parts[1]; let id = parts[1];
match parts[0] { match parts[0] {
"track" => { "track" => {
let track = self.spotify.tracks().get_track(id, None).await?; let track = self.spotify.tracks().get_track(id, self.market).await?;
Ok(SpotifyItem::Track(track.data)) Ok(SpotifyItem::Track(track.data))
} }
"playlist" => { "playlist" => {
let playlist = self.spotify.playlists().get_playlist(id, None).await?; let playlist = self
.spotify
.playlists()
.get_playlist(id, self.market)
.await?;
Ok(SpotifyItem::Playlist(playlist.data)) Ok(SpotifyItem::Playlist(playlist.data))
} }
"album" => { "album" => {
let album = self.spotify.albums().get_album(id, None).await?; let album = self.spotify.albums().get_album(id, self.market).await?;
Ok(SpotifyItem::Album(album.data)) Ok(SpotifyItem::Album(album.data))
} }
"artist" => { "artist" => {
@ -119,7 +133,7 @@ impl Spotify {
let page = self let page = self
.spotify .spotify
.playlists() .playlists()
.get_playlists_items(id, 100, offset, None) .get_playlists_items(id, 100, offset, self.market)
.await?; .await?;
items.append( items.append(
&mut page &mut page
@ -152,7 +166,7 @@ impl Spotify {
let page = self let page = self
.spotify .spotify
.albums() .albums()
.get_album_tracks(id, 50, offset, None) .get_album_tracks(id, 50, offset, self.market)
.await?; .await?;
items.append(&mut page.data.items.to_vec()); items.append(&mut page.data.items.to_vec());
@ -172,7 +186,7 @@ impl Spotify {
let page = self let page = self
.spotify .spotify
.artists() .artists()
.get_artist_albums(id, None, 50, offset, None) .get_artist_albums(id, None, 50, offset, self.market)
.await?; .await?;
for album in &mut page.data.items.iter() { for album in &mut page.data.items.iter() {
@ -193,6 +207,7 @@ impl Clone for Spotify {
Self { Self {
session: self.session.clone(), session: self.session.clone(),
spotify: Client::new(self.spotify.credentials.clone()), spotify: Client::new(self.spotify.credentials.clone()),
market: self.market.clone(),
} }
} }
} }