mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-23 09:09:18 +02:00
Added new branch /v1/update
This commit is contained in:
parent
5f2026af0d
commit
f201bc7fda
5 changed files with 409 additions and 6 deletions
|
@ -2,17 +2,17 @@ mod builtin;
|
|||
mod debug;
|
||||
mod kcomebacks;
|
||||
mod projects;
|
||||
mod updates;
|
||||
mod update;
|
||||
|
||||
pub use builtin::get_builtin_routes as get_v1_builtin_routes;
|
||||
pub use debug::get_debug_routes as get_v1_debug_routes;
|
||||
pub use kcomebacks::get_kcomebacks_routes as get_v1_kcomebacks_routes;
|
||||
pub use projects::get_project_routes as get_v1_project_routes;
|
||||
pub use updates::get_updates_routes as get_v1_updates_routes;
|
||||
pub use update::get_update_routes as get_v1_updates_routes;
|
||||
|
||||
use warp::Filter;
|
||||
|
||||
pub fn get_v1_routes() -> impl warp::Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
|
||||
pub fn get_v1_routes() -> impl warp::Filter<Extract = impl warp::Reply + warp::generic::Tuple, Error = warp::Rejection> + Clone {
|
||||
return get_v1_builtin_routes()
|
||||
.or(get_v1_debug_routes())
|
||||
.or(get_v1_kcomebacks_routes())
|
||||
|
|
248
src/v1/update/mod.rs
Normal file
248
src/v1/update/mod.rs
Normal file
|
@ -0,0 +1,248 @@
|
|||
use std::fs;
|
||||
use std::io::BufRead;
|
||||
use std::process::{Stdio, Command};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task;
|
||||
use warp::Filter;
|
||||
use crate::error_responses::InternalServerError;
|
||||
use crate::Logger;
|
||||
|
||||
// DiscographyQuery
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct DiscographyQuery {
|
||||
artists: String,
|
||||
}
|
||||
|
||||
pub fn get_update_routes() -> impl warp::Filter<Extract = impl warp::Reply + warp::generic::Tuple, Error = warp::Rejection> + Clone {
|
||||
warp::path("v1").and(warp::path("update"))
|
||||
// update/kcomebacks
|
||||
// update/projects
|
||||
// update/makediscography?artists=artist1,artist2,artist3
|
||||
// update/synclikedsongs
|
||||
.and((warp::path("kcomebacks").and_then(update_kcomebacks))
|
||||
.or(warp::path("projects").and_then(update_projects))
|
||||
.or(warp::path("makediscography").map(||"Not implemented yet"))
|
||||
.or(warp::path("synclikedsongs").and_then(sync_liked_songs))
|
||||
)
|
||||
}
|
||||
|
||||
async fn update_kcomebacks() -> Result<impl warp::Reply, warp::Rejection> {
|
||||
// check if the local repository exists, if not, clone it
|
||||
if !fs::metadata("./resources/turbo_octo_potato").is_ok() {
|
||||
setup().unwrap();
|
||||
};
|
||||
|
||||
if let Err(err) = run_kcomebacks_command() {
|
||||
// Handle the error here
|
||||
eprintln!("Error: {}", err);
|
||||
// Return an appropriate response or error
|
||||
return Err(warp::reject::custom(InternalServerError));
|
||||
}
|
||||
|
||||
Ok(warp::reply::json(&json!({"status": "updating..."})))
|
||||
|
||||
}
|
||||
|
||||
async fn update_projects() -> Result<impl warp::Reply, warp::Rejection> {
|
||||
// check if the local repository exists, if not, clone it
|
||||
if !fs::metadata("./resources/turbo_octo_potato").is_ok() {
|
||||
setup().unwrap();
|
||||
};
|
||||
|
||||
if let Err(err) = run_projects_command() {
|
||||
// Handle the error here
|
||||
eprintln!("Error: {}", err);
|
||||
// Return an appropriate response or error
|
||||
return Err(warp::reject::custom(InternalServerError));
|
||||
}
|
||||
|
||||
Ok(warp::reply::json(&json!({"status": "updating..."})))
|
||||
|
||||
}
|
||||
|
||||
async fn sync_liked_songs() -> Result<impl warp::Reply, warp::Rejection> {
|
||||
// check if the local repository exists, if not, clone it
|
||||
if !fs::metadata("./resources/turbo_octo_potato").is_ok() {
|
||||
setup().unwrap();
|
||||
};
|
||||
|
||||
if let Err(err) = run_likedsongs_command() {
|
||||
// Handle the error here
|
||||
eprintln!("Error: {}", err);
|
||||
// Return an appropriate response or error
|
||||
return Err(warp::reject::custom(InternalServerError));
|
||||
}
|
||||
|
||||
Ok(warp::reply::json(&json!({"status": "updating..."})))
|
||||
|
||||
}
|
||||
|
||||
fn setup() -> Result<(), git2::Error> {
|
||||
let repository_url = "https://github.com/JonasunderscoreJones/turbo-octo-potato.git";
|
||||
let local_directory = "resources/turbo_octo_potato";
|
||||
|
||||
git2::Repository::clone(repository_url, local_directory)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// fn run_command() -> Result<(), std::io::Error> {
|
||||
// let (tx, mut rx) = mpsc::channel(1);
|
||||
|
||||
// task::spawn_blocking(move || {
|
||||
// let mut child = Command::new("python3")
|
||||
// .arg(&py_file)
|
||||
// .arg(&args)
|
||||
// .current_dir("resources/turbo_octo_potato")
|
||||
// .stdout(Stdio::piped())
|
||||
// .spawn()
|
||||
// .expect("failed to execute child");
|
||||
|
||||
// let stdout = child.stdout.as_mut().unwrap();
|
||||
|
||||
// let mut reader = std::io::BufReader::new(stdout);
|
||||
|
||||
// let mut line = String::new();
|
||||
|
||||
// loop {
|
||||
// let len = reader.read_line(&mut line).unwrap();
|
||||
// if len == 0 {
|
||||
// break;
|
||||
// }
|
||||
// tx.blocking_send(line.clone()).unwrap();
|
||||
// line.clear();
|
||||
// }
|
||||
|
||||
// child.wait().unwrap();
|
||||
// });
|
||||
|
||||
// task::spawn(async move {
|
||||
// while let Some(line) = rx.recv().await {
|
||||
// println!("{}", line);
|
||||
// }
|
||||
// });
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// run_command with python file and args as parameters
|
||||
|
||||
|
||||
fn run_kcomebacks_command() -> Result<(), std::io::Error> {
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
|
||||
task::spawn_blocking(move || {
|
||||
let mut child = Command::new("python3")
|
||||
.arg("rpopfetch.py")
|
||||
.arg("--cdn")
|
||||
.current_dir("resources/turbo_octo_potato")
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute child");
|
||||
|
||||
let stdout = child.stdout.as_mut().unwrap();
|
||||
|
||||
let mut reader = std::io::BufReader::new(stdout);
|
||||
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
let len = reader.read_line(&mut line).unwrap();
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
tx.blocking_send(line.clone()).unwrap();
|
||||
line.clear();
|
||||
}
|
||||
|
||||
child.wait().unwrap();
|
||||
});
|
||||
|
||||
task::spawn(async move {
|
||||
while let Some(line) = rx.recv().await {
|
||||
Logger::info(&format!("[/v1/kcomebacks/update]: {}", line));
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_projects_command() -> Result<(), std::io::Error> {
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
|
||||
task::spawn_blocking(move || {
|
||||
let mut child = Command::new("python3")
|
||||
.arg("update_projects.py")
|
||||
.arg("--cdn")
|
||||
.current_dir("resources/turbo_octo_potato")
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute child");
|
||||
|
||||
let stdout = child.stdout.as_mut().unwrap();
|
||||
|
||||
let mut reader = std::io::BufReader::new(stdout);
|
||||
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
let len = reader.read_line(&mut line).unwrap();
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
tx.blocking_send(line.clone()).unwrap();
|
||||
line.clear();
|
||||
}
|
||||
|
||||
child.wait().unwrap();
|
||||
});
|
||||
|
||||
task::spawn(async move {
|
||||
while let Some(line) = rx.recv().await {
|
||||
Logger::info(&format!("[/v1/projects/update]: {}", line));
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn run_likedsongs_command() -> Result<(), std::io::Error> {
|
||||
let (tx, mut rx) = mpsc::channel(1);
|
||||
|
||||
task::spawn_blocking(move || {
|
||||
let mut child = Command::new("python3")
|
||||
.arg("likedsongsync2.py")
|
||||
.current_dir("resources/turbo_octo_potato")
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.expect("failed to execute child");
|
||||
|
||||
let stdout = child.stdout.as_mut().unwrap();
|
||||
|
||||
let mut reader = std::io::BufReader::new(stdout);
|
||||
|
||||
let mut line = String::new();
|
||||
|
||||
loop {
|
||||
let len = reader.read_line(&mut line).unwrap();
|
||||
if len == 0 {
|
||||
break;
|
||||
}
|
||||
tx.blocking_send(line.clone()).unwrap();
|
||||
line.clear();
|
||||
}
|
||||
|
||||
child.wait().unwrap();
|
||||
});
|
||||
|
||||
task::spawn(async move {
|
||||
while let Some(line) = rx.recv().await {
|
||||
Logger::info(&format!("[/v1/synclikedsongs]: {}", line));
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue