summaryrefslogtreecommitdiff
path: root/src/spotify.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/spotify.rs')
-rw-r--r--src/spotify.rs45
1 files changed, 25 insertions, 20 deletions
diff --git a/src/spotify.rs b/src/spotify.rs
index 596dec3..0e18c43 100644
--- a/src/spotify.rs
+++ b/src/spotify.rs
@@ -1,9 +1,13 @@
use crate::database;
use crate::errors::Error;
use lazy_static::lazy_static;
-use rspotify::spotify::client::{ApiError, Spotify};
-use rspotify::spotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth};
-use rspotify::spotify::util::process_token;
+use rspotify::client::{ApiError, Spotify};
+use rspotify::model::page::Page;
+use rspotify::model::playlist::*;
+use rspotify::model::track::*;
+use rspotify::oauth2::{SpotifyClientCredentials, SpotifyOAuth};
+use rspotify::senum::TimeRange;
+use rspotify::util::process_token;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
@@ -19,13 +23,14 @@ macro_rules! get_items {
$index = 0;
let mut result: Vec<$t> = Vec::new();
loop {
- match $spotify_call {
- Ok(mut items) => {
+ let res: Result<Page<$t>, failure::Error> = $spotify_call.await;
+ match res {
+ Ok(mut page) => {
$index += CHUNK_SIZE;
- if items.items.is_empty() {
+ if page.items.is_empty() {
break;
}
- result.append(&mut items.items);
+ result.append(&mut page.items);
}
Err(e) => match e.downcast::<ApiError>() {
Ok(ApiError::RateLimited(x)) => {
@@ -43,10 +48,7 @@ macro_rules! get_items {
}};
}
-use rspotify::spotify::model::playlist::*;
-use rspotify::spotify::model::track::*;
-use rspotify::spotify::senum::TimeRange;
-pub fn load_profile(db_uid: i32, spotify_uid: &str, spotify: Spotify) -> Result<(), Error> {
+pub async fn load_profile(db_uid: i32, spotify_uid: &str, spotify: Spotify) -> Result<(), Error> {
let mut index;
let playlists = get_items!(
SimplifiedPlaylist,
@@ -87,8 +89,8 @@ pub fn load_profile(db_uid: i32, spotify_uid: &str, spotify: Spotify) -> Result<
None,
)
);
- for track in tracks {
- if let Err(e) = database::insert_track(db_uid, track.track, 1) {
+ for track in tracks.iter().map(|x| x.track.clone()).flatten() {
+ if let Err(e) = database::insert_track(db_uid, track, 1) {
println!("failed to load track to db: {:?}", e)
};
}
@@ -96,13 +98,16 @@ pub fn load_profile(db_uid: i32, spotify_uid: &str, spotify: Spotify) -> Result<
Ok(())
}
-pub fn auth_user(name: &str, url: String) -> Result<(String, Spotify), Error> {
- let mut guard = CACHE.lock()?;
- let mut oauth = guard.remove(name)?;
+pub async fn auth_user(name: &str, url: String) -> Result<(String, Spotify), Error> {
+ let mut oauth = {
+ let mut guard = CACHE.lock()?;
+ guard.remove(name)?
+ };
println!("auth: {:?} url: {}", oauth, url);
- let token_info = process_token(&mut oauth, &mut ("?code=".to_owned() + url.as_ref()));
+ let mut token_string = format!("?code={}", url);
+ let token_info = process_token(&mut oauth, &mut token_string);
let client_credential = SpotifyClientCredentials::default()
- .token_info(token_info?)
+ .token_info(token_info.await?)
.build();
let spotify = Spotify::default()
@@ -110,14 +115,14 @@ pub fn auth_user(name: &str, url: String) -> Result<(String, Spotify), Error> {
.build();
let user_id = spotify
.current_user()
+ .await
.map_err(|e| format!("failed to load currentuser {:?}", e))?
.id;
Ok((user_id, spotify))
}
-#[get("/token/<name>")]
pub fn token(name: String) -> Result<String, Error> {
- let state = rspotify::spotify::util::generate_random_string(16);
+ let state = rspotify::util::generate_random_string(16);
let oauth = SpotifyOAuth::default();
let oauth = oauth
.scope("playlist-read-private, playlist-read-collaborative, user-read-private, user-follow-read, user-library-read")