Language Migration #21
@@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::fs::{read_dir, DirEntry};
|
use std::fs::{read_dir, DirEntry};
|
||||||
use std::io::{Error, Read, Result};
|
use std::io::{Error, Read, Result, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
@@ -12,9 +12,9 @@ use tokio::runtime::Runtime;
|
|||||||
|
|
||||||
use crate::models::song::Album;
|
use crate::models::song::Album;
|
||||||
use crate::models::{self, song};
|
use crate::models::{self, song};
|
||||||
use crate::parsers;
|
|
||||||
use crate::syncers;
|
use crate::syncers;
|
||||||
use crate::utilities;
|
use crate::utilities;
|
||||||
|
use crate::{constants, parsers};
|
||||||
use crate::{exit_program, managers};
|
use crate::{exit_program, managers};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
@@ -157,12 +157,40 @@ impl CommitManager {
|
|||||||
del.delete_song(&token, &song);
|
del.delete_song(&token, &song);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement
|
|
||||||
fn download_song(&self) {
|
fn download_song(&self) {
|
||||||
println!("Deleting song");
|
println!("Deleting song");
|
||||||
|
let dwn = self.ica_action.retrieve_flag_value(&String::from("-b"));
|
||||||
|
let num: i32 = dwn.parse::<i32>().unwrap();
|
||||||
|
|
||||||
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
|
api: models::api::API::default(),
|
||||||
|
ica_act: self.ica_action.clone(),
|
||||||
|
};
|
||||||
|
prsr.parse_api();
|
||||||
|
|
||||||
|
let api = prsr.retrieve_api();
|
||||||
|
let token = self.parse_token(&api);
|
||||||
|
|
||||||
|
let mut dwn_loader = syncers::download::Download { api: api.clone() };
|
||||||
|
let mut song = models::song::Song::default();
|
||||||
|
song.id = Some(num);
|
||||||
|
let result_fut = dwn_loader.download_song(&token, &song);
|
||||||
|
let result = Runtime::new().unwrap().block_on(result_fut);
|
||||||
|
match result {
|
||||||
|
Ok(o) => {
|
||||||
|
println!("Success");
|
||||||
|
let mut filename = String::from("audio");
|
||||||
|
filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||||
|
let data = o.as_bytes();
|
||||||
|
let mut file = std::fs::File::create(filename).expect("Failed to save");
|
||||||
|
file.write_all(&data).expect("ff");
|
||||||
|
}
|
||||||
|
Err(er) => {
|
||||||
|
println!("Error {:?}", er);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Implement
|
|
||||||
fn retrieve_object(&self) {
|
fn retrieve_object(&self) {
|
||||||
println!("Deleting song");
|
println!("Deleting song");
|
||||||
let rt = self.ica_action.retrieve_flag_value(&String::from("-rt"));
|
let rt = self.ica_action.retrieve_flag_value(&String::from("-rt"));
|
||||||
|
|||||||
+30
-12
@@ -14,11 +14,29 @@ impl Default for Download {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum MyError {
|
||||||
|
Request(reqwest::Error),
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
impl Download {
|
impl Download {
|
||||||
pub async fn download_song(&self, token: &models::token::Token, song: &models::song::Song) {
|
pub async fn download_song(
|
||||||
|
&mut self,
|
||||||
|
token: &models::token::Token,
|
||||||
|
song: &models::song::Song,
|
||||||
|
) -> Result<String, MyError> {
|
||||||
|
self.api.endpoint = String::from("song/data/download");
|
||||||
let url = self.retrieve_url(&song);
|
let url = self.retrieve_url(&song);
|
||||||
let access_token = token.bearer_token();
|
let access_token = token.bearer_token();
|
||||||
let client = reqwest::Client::new();
|
|
||||||
|
let mut headers = reqwest::header::HeaderMap::new();
|
||||||
|
headers.insert(
|
||||||
|
reqwest::header::AUTHORIZATION,
|
||||||
|
http::header::HeaderValue::from_str(&access_token.clone()).unwrap(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let client = reqwest::Client::builder().build().unwrap();
|
||||||
let response = client
|
let response = client
|
||||||
.get(&url)
|
.get(&url)
|
||||||
.header(reqwest::header::AUTHORIZATION, &access_token)
|
.header(reqwest::header::AUTHORIZATION, &access_token)
|
||||||
@@ -28,16 +46,15 @@ impl Download {
|
|||||||
|
|
||||||
match response.status() {
|
match response.status() {
|
||||||
reqwest::StatusCode::OK => {
|
reqwest::StatusCode::OK => {
|
||||||
// on success, parse our JSON to an APIResponse
|
let data = response.text();
|
||||||
/*
|
match data.await {
|
||||||
let s = response.json::<Vec<Track>>().await;
|
Ok(e) => {
|
||||||
match s {
|
return Ok(e);
|
||||||
Ok(parsed) => {
|
}
|
||||||
println!("\nSuccess!");
|
Err(er) => {
|
||||||
|
println!("Error {:?}", er);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(_) => println!("Hm, the response didn't match the shape we expected."),
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
reqwest::StatusCode::UNAUTHORIZED => {
|
reqwest::StatusCode::UNAUTHORIZED => {
|
||||||
println!("Need to grab a new token");
|
println!("Need to grab a new token");
|
||||||
@@ -46,10 +63,11 @@ impl Download {
|
|||||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return Err(MyError::Other(String::from("Error downloading")));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||||
// let mut url: String = String::new();
|
|
||||||
let api = &self.api;
|
let api = &self.api;
|
||||||
let mut url: String = String::from(&api.url);
|
let mut url: String = String::from(&api.url);
|
||||||
url += &String::from("api/");
|
url += &String::from("api/");
|
||||||
|
|||||||
Reference in New Issue
Block a user