From 10651a57651646d808fe375e51e902e9ff6d77a1 Mon Sep 17 00:00:00 2001 From: Chikage Date: Fri, 13 Oct 2023 12:59:46 +0900 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9B=BE=E5=BA=8A=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/yande_popular/Cargo.toml | 2 +- plugins/yande_popular/src/lib.rs | 106 ++++++++++++++++------------- plugins/yande_popular/src/yande.rs | 3 - 3 files changed, 60 insertions(+), 51 deletions(-) diff --git a/plugins/yande_popular/Cargo.toml b/plugins/yande_popular/Cargo.toml index 2973586..2bbfcf0 100644 --- a/plugins/yande_popular/Cargo.toml +++ b/plugins/yande_popular/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" anyhow = "1" matrix_bot_core = { path = "../../matrix_bot_core" } image_compressor = { git = "https://github.com/Chikage0o0/image_compressor/", branch = "main" } -reqwest = "0.11.22" +reqwest = { version = "0.11.6" } log = "0.4.14" select = "0.6.0" sled = { version = "0.34.7" } diff --git a/plugins/yande_popular/src/lib.rs b/plugins/yande_popular/src/lib.rs index e815a1d..3ea9e56 100644 --- a/plugins/yande_popular/src/lib.rs +++ b/plugins/yande_popular/src/lib.rs @@ -1,7 +1,9 @@ -use std::path::Path; +use std::{collections::HashMap, path::Path}; use anyhow::Result; -use matrix_bot_core::matrix::client::Client; +use db::DB; +use matrix_bot_core::matrix::{client::Client, room::Room}; +use setting::RoomSetting; use crate::setting::Setting; @@ -19,61 +21,71 @@ pub async fn run(client: Client, plugin_folder: impl AsRef) -> Result<()> loop { log::info!("start scan"); - for (setting, (db, room)) in setting_hashmap.iter() { - log::info!("scan: {}", setting.room_id); - let mut image_list = Vec::new(); + sync(&setting_hashmap).await.unwrap_or_else(|e| { + log::error!("scan failed: {}", e); + }); + tokio::time::sleep(tokio::time::Duration::from_secs(60 * 60)).await; + } +} - for url in setting.yande_url.iter() { - let list = yande::get_image_list(url).await?; - image_list.extend(list); - } +pub async fn sync(setting_hashmap: &HashMap) -> Result<()> { + for (setting, (db, room)) in setting_hashmap.iter() { + log::info!("scan: {}", setting.room_id); + let mut image_list = Vec::new(); + + for url in setting.yande_url.iter() { + let list = yande::get_image_list(url).await?; + 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() { + log::info!("prepare download: {}", id); + let path = match yande::download_img(*id, url, &setting.tmp_path).await { + Ok(path) => path, + Err(e) => { + log::error!("download failed: {}", e); + return Err(e.into()); + } + }; - for (id, img_data) in download_list { - let msg = format!( - "来源:[https://yande.re/post/show/{id}](https://yande.re/post/show/{id})" - ); - room.send_msg(&msg, true) - .await - .unwrap_or_else(|e| log::error!("send msg failed: {}", e)); - for (id, url) in img_data.url.iter() { - log::info!("prepare download: {}", id); - let path = match yande::download_img(*id, url, &setting.tmp_path).await { + let path = if let Some(size) = setting.resize { + match resize::resize_and_compress(&path, size) { Ok(path) => path, Err(e) => { - log::error!("download failed: {}", e); - continue; + log::error!("resize {id} failed: {}", e); + return Err(e.into()); } - }; + } + } else { + path + }; - let path = if let Some(size) = setting.resize { - match resize::resize_and_compress(&path, size) { - Ok(path) => path, - Err(e) => { - log::error!("resize {id} failed: {}", e); - continue; - } - } - } else { - path - }; - - log::info!("upload: {}", id); - - room.send_attachment(&path) - .await - .unwrap_or_else(|e| log::error!("send attachment failed: {}", e)); + log::info!("upload: {}", id); - std::fs::remove_file(&path).unwrap_or_else(|e| { - log::error!("remove file failed: {}", e); - }); + match room.send_attachment(&path).await { + Ok(_) => { + log::info!("upload: {} done", id); + db.insert(&id.to_string())?; + } + Err(e) => { + log::error!("upload failed: {}", e); + return Err(e.into()); + } } + + std::fs::remove_file(&path).unwrap_or_else(|e| { + log::error!("remove file failed: {}", e); + }); } - log::info!("scan: {} done", setting.room_id); - db.auto_remove()?; + let msg = + format!("来源:[https://yande.re/post/show/{id}](https://yande.re/post/show/{id})"); + room.send_msg(&msg, true).await?; } - - tokio::time::sleep(tokio::time::Duration::from_secs(60 * 60)).await; + log::info!("scan: {} done", setting.room_id); + db.auto_remove()?; } + Ok(()) } diff --git a/plugins/yande_popular/src/yande.rs b/plugins/yande_popular/src/yande.rs index 4b9c9a9..dace1ee 100644 --- a/plugins/yande_popular/src/yande.rs +++ b/plugins/yande_popular/src/yande.rs @@ -186,9 +186,6 @@ pub async fn get_download_list(image_list: &[i64], db: &DB) -> Result { } download_list.insert(id, img_data.clone()); - for (id, _) in img_data.url.iter() { - db.insert(&id.to_string())?; - } } Ok(download_list)