Skip to content

Commit

Permalink
auto download and run qbittorrent in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Chikage0o0 committed Oct 13, 2023
1 parent 6f95063 commit 87b2bba
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion plugins/qbittorrent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn run(client: Client, plugin_folder: impl AsRef<std::path::Path>) ->

let setting = setting::get_or_init(plugin_folder)?;

if setting.use_internal_qbit {}


Ok(())
}
47 changes: 42 additions & 5 deletions plugins/qbittorrent/src/qbit/binary.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use anyhow::Result;

use std::{path::Path, process::Child};
use std::{
path::Path,
process::{Child, Stdio},
};

#[allow(dead_code)]
fn get_download_link() -> Result<String> {
if !(cfg!(target_os = "linux")) {
if cfg!(target_os = "linux") != true {
return Err(anyhow::anyhow!("only support linux"));
}

Expand All @@ -21,10 +24,10 @@ fn get_download_link() -> Result<String> {
}
Ok(link)
}

#[allow(dead_code)]
fn download_binary(path: impl AsRef<Path>) -> Result<()> {
let link = get_download_link()?;
let resp = reqwest::blocking::get(&link)?;
let resp = reqwest::blocking::get(link)?;
let binary = resp.bytes()?;
std::fs::create_dir_all(path.as_ref().parent().unwrap())?;
std::fs::write(&path, binary)?;
Expand All @@ -35,7 +38,7 @@ fn download_binary(path: impl AsRef<Path>) -> Result<()> {
}
Ok(())
}

#[allow(dead_code)]
pub fn run(runtime_folder: impl AsRef<Path>, port: u16) -> Result<Child> {
let binary_path = runtime_folder.as_ref().join("qbittorrent-nox");
if !binary_path.exists() {
Expand Down Expand Up @@ -73,8 +76,42 @@ pub fn run(runtime_folder: impl AsRef<Path>, port: u16) -> Result<Child> {
"--profile={}",
qbittorrent_folder.to_string_lossy()
));
cmd.stdout(Stdio::null());

let child = cmd.spawn()?;

Ok(child)
}

#[cfg(test)]
mod test {

use std::path::PathBuf;

use super::*;

#[test]
fn test_running() {
let runtime_folder = PathBuf::from("/tmp/qbit");
let mut child = run(&runtime_folder, 8080).unwrap();
std::thread::sleep(std::time::Duration::from_secs(10));

// is still running
assert!(child.try_wait().unwrap().is_none());
let logs = runtime_folder
.join("config")
.join("qBittorrent")
.join("data")
.join("logs")
.join("qbittorrent.log");

if let Ok(logs) = std::fs::read_to_string(logs) {
assert!(logs.contains("Web UI: Now listening on IP"));
} else {
panic!("no logs");
}

child.kill().unwrap();
std::fs::remove_dir_all("/tmp/qbit").unwrap();
}
}
8 changes: 4 additions & 4 deletions plugins/webhook/src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub struct Setting {
}

impl Setting {
pub async fn to_hashmap(self, client: &Client) -> Result<HashMap<String, Room>> {
pub async fn to_hashmap(&self, client: &Client) -> Result<HashMap<String, Room>> {
let mut hashmap = HashMap::new();
for room_id in self.room_id {
let room = Room::new(&client, &room_id).await?;
hashmap.insert(room_id, room);
for room_id in &self.room_id {
let room = Room::new(client, room_id).await?;
hashmap.insert(room_id.clone(), room);
}
Ok(hashmap)
}
Expand Down
8 changes: 4 additions & 4 deletions plugins/yande_popular/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub async fn sync(setting_hashmap: &HashMap<RoomSetting, (DB, Room)>) -> Result<
image_list.extend(list);
}

let download_list = yande::get_download_list(&image_list, &db).await?;
let download_list = yande::get_download_list(&image_list, db).await?;

for (id, img_data) in download_list {
for (id, url) in img_data.url.iter() {
Expand All @@ -47,7 +47,7 @@ pub async fn sync(setting_hashmap: &HashMap<RoomSetting, (DB, Room)>) -> Result<
Ok(path) => path,
Err(e) => {
log::error!("download failed: {}", e);
return Err(e.into());
return Err(e);
}
};

Expand All @@ -56,7 +56,7 @@ pub async fn sync(setting_hashmap: &HashMap<RoomSetting, (DB, Room)>) -> Result<
Ok(path) => path,
Err(e) => {
log::error!("resize {id} failed: {}", e);
return Err(e.into());
return Err(e);
}
}
} else {
Expand All @@ -72,7 +72,7 @@ pub async fn sync(setting_hashmap: &HashMap<RoomSetting, (DB, Room)>) -> Result<
}
Err(e) => {
log::error!("upload failed: {}", e);
return Err(e.into());
return Err(e);
}
}

Expand Down
6 changes: 3 additions & 3 deletions plugins/yande_popular/src/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ pub struct Setting {
}

impl Setting {
pub async fn to_hashmap(self, client: &Client) -> Result<HashMap<RoomSetting, (DB, Room)>> {
pub async fn to_hashmap(&self, client: &Client) -> Result<HashMap<RoomSetting, (DB, Room)>> {
let mut hashmap = HashMap::new();
for setting in self.room {
for setting in &self.room {
let db = DB::open(&setting.db_path);
std::fs::create_dir_all(Path::new(&setting.tmp_path)).unwrap_or_else(|e| {
log::error!("create tmp dir failed: {}", e);
});
let room = Room::new(&client, &setting.room_id).await?;
let room = Room::new(client, &setting.room_id).await?;
hashmap.insert(setting.clone(), (db, room));
}
Ok(hashmap)
Expand Down

0 comments on commit 87b2bba

Please sign in to comment.