Skip to content

Commit

Permalink
优化图床逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Chikage0o0 committed Oct 13, 2023
1 parent bf408b3 commit 10651a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 51 deletions.
2 changes: 1 addition & 1 deletion plugins/yande_popular/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
106 changes: 59 additions & 47 deletions plugins/yande_popular/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,61 +21,71 @@ pub async fn run(client: Client, plugin_folder: impl AsRef<Path>) -> 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<RoomSetting, (DB, Room)>) -> 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(())
}
3 changes: 0 additions & 3 deletions plugins/yande_popular/src/yande.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ pub async fn get_download_list(image_list: &[i64], db: &DB) -> Result<ImgInfo> {
}

download_list.insert(id, img_data.clone());
for (id, _) in img_data.url.iter() {
db.insert(&id.to_string())?;
}
}

Ok(download_list)
Expand Down

0 comments on commit 10651a5

Please sign in to comment.