Merge branch 'master' into rocket

This commit is contained in:
Antoine Gersant 2018-11-10 23:44:20 -08:00
commit 0cd82a338d
4 changed files with 174 additions and 515 deletions

613
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -17,10 +17,9 @@ error-chain = "0.12.0"
getopts = "0.2.15"
id3 = "0.2.3"
image = "0.20.0"
rustfm-scrobble = "0.9.1"
rustfm-scrobble = { git = "https://github.com/agersant/rustfm-scrobble" }
lewton = "0.9.1"
log = "0.4.5"
md5 = "0.5.0"
metaflac = "0.1.8"
mp3-duration = "0.1.0"
rand = "0.5.5"
@ -32,7 +31,6 @@ rust-crypto = "0.2.36"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde-xml-rs = "0.2.1"
simplelog = "0.5.2"
toml = "0.4.5"

View file

@ -1,9 +1,4 @@
use md5;
use reqwest;
use rustfm_scrobble::{Scrobble, Scrobbler};
use serde_xml_rs::deserialize;
use std::collections::HashMap;
use std::io::Read;
use std::path::Path;
use crate::db::ConnectionSource;
@ -14,7 +9,6 @@ use crate::vfs::VFSSource;
const LASTFM_API_KEY: &str = "02b96c939a2b451c31dfd67add1f696e";
const LASTFM_API_SECRET: &str = "0f25a80ceef4b470b5cb97d99d4b3420";
const LASTFM_API_ROOT: &str = "http://ws.audioscrobbler.com/2.0/";
#[derive(Debug, Deserialize)]
struct AuthResponseSessionName {
@ -63,30 +57,14 @@ pub fn link<T>(db: &T, username: &str, token: &str) -> Result<(), errors::Error>
where
T: ConnectionSource + VFSSource,
{
let mut params = HashMap::new();
params.insert("token".to_string(), token.to_string());
params.insert("api_key".to_string(), LASTFM_API_KEY.to_string());
let mut response = match api_request("auth.getSession", &params) {
Ok(r) => r,
Err(_) => bail!(errors::ErrorKind::LastFMAuthError),
};
let mut body = String::new();
response.read_to_string(&mut body)?;
if !response.status().is_success() {
bail!(errors::ErrorKind::LastFMAuthError)
}
let auth_response: AuthResponse = match deserialize(body.as_bytes()) {
Ok(d) => d,
Err(_) => bail!(errors::ErrorKind::LastFMDeserializationError),
};
let mut scrobbler = Scrobbler::new(LASTFM_API_KEY.into(), LASTFM_API_SECRET.into());
let auth_response = scrobbler.authenticate_with_token(token.to_string())?;
user::lastfm_link(
db,
username,
&auth_response.session.name.body,
&auth_response.session.key.body,
&auth_response.name,
&auth_response.key,
)
}
@ -120,43 +98,3 @@ where
scrobbler.now_playing(scrobble)?;
Ok(())
}
fn api_request(
method: &str,
params: &HashMap<String, String>,
) -> Result<reqwest::Response, reqwest::Error> {
let mut url = LASTFM_API_ROOT.to_string();
url.push_str("?");
url.push_str(&format!("method={}&", method));
for (k, v) in params.iter() {
url.push_str(&format!("{}={}&", k, v));
}
let api_signature = get_signature(method, params);
url.push_str(&format!("api_sig={}", api_signature));
let client = reqwest::ClientBuilder::new().build()?;
let request = client.get(url.as_str());
request.send()
}
fn get_signature(method: &str, params: &HashMap<String, String>) -> String {
let mut signature_data = params.clone();
signature_data.insert("method".to_string(), method.to_string());
let mut param_names = Vec::new();
for param_name in signature_data.keys() {
param_names.push(param_name);
}
param_names.sort();
let mut signature = String::new();
for param_name in param_names {
signature.push_str((param_name.to_string() + signature_data[param_name].as_str()).as_str())
}
signature.push_str(LASTFM_API_SECRET);
let digest = md5::compute(signature.as_bytes());
format!("{:X}", digest)
}

View file

@ -17,7 +17,6 @@ extern crate getopts;
extern crate id3;
extern crate image;
extern crate lewton;
extern crate md5;
extern crate metaflac;
extern crate mp3_duration;
extern crate rand;
@ -32,7 +31,6 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_xml_rs;
extern crate toml;
#[macro_use]
extern crate log;