From 4d8062b16fbc25c5042022fdb03cb01b8d4cc659 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 23 Aug 2022 17:28:24 +0200 Subject: [PATCH] Add association of urls after import Signed-off-by: trivernis --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/operations/find_and_send_reddit_posts.rs | 7 ++++++- src/operations/find_and_send_twitter_posts.rs | 7 ++++++- src/utils/reddit.rs | 11 +++++++++++ 5 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de216b2..0e61ff0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -928,7 +928,7 @@ dependencies = [ [[package]] name = "hydrus-utils" -version = "0.4.0" +version = "0.4.1" dependencies = [ "clap", "color-eyre", diff --git a/Cargo.toml b/Cargo.toml index fd5947b..1919268 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hydrus-utils" -version = "0.4.1" +version = "0.4.2" edition = "2021" license = "Apache-2.0" description = "Automatically tag hydrus file by using pixiv and saucenao" diff --git a/src/operations/find_and_send_reddit_posts.rs b/src/operations/find_and_send_reddit_posts.rs index 314444e..272f56b 100644 --- a/src/operations/find_and_send_reddit_posts.rs +++ b/src/operations/find_and_send_reddit_posts.rs @@ -24,7 +24,12 @@ async fn import_post(post_url: &str, hydrus: &Hydrus) -> Result<()> { tracing::info!("Found {} images for post {}", images.len(), post_url); for url in images { - hydrus.import().url(url).run().await?; + let mut entry = hydrus.import().url(url).run().await?; + let files = entry.files().await?; + + for mut file in files { + file.associate_urls(vec![post_url.to_string()]).await?; + } } Ok(()) } diff --git a/src/operations/find_and_send_twitter_posts.rs b/src/operations/find_and_send_twitter_posts.rs index f5a7070..b50a127 100644 --- a/src/operations/find_and_send_twitter_posts.rs +++ b/src/operations/find_and_send_twitter_posts.rs @@ -31,7 +31,12 @@ async fn import_post(post_url: &str, hydrus: &Hydrus, token: &Token) -> Result<( tracing::info!("Found {} images for tweet {}", images.len(), post_url); for url in images { - hydrus.import().url(url).run().await?; + let mut entry = hydrus.import().url(url).run().await?; + let files = entry.files().await?; + + for mut file in files { + file.associate_urls(vec![post_url.to_string()]).await?; + } } Ok(()) } diff --git a/src/utils/reddit.rs b/src/utils/reddit.rs index 9ebe2c7..d999313 100644 --- a/src/utils/reddit.rs +++ b/src/utils/reddit.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use crate::Result; +use lazy_regex::regex; use reqwest::{redirect::Policy, StatusCode}; use serde::Deserialize; use serde_json::Value; @@ -101,6 +102,9 @@ async fn get_post(url: &str) -> Result { /// Resolves reddit redirects #[tracing::instrument(level = "debug")] async fn resolve_redirects(url: &str) -> Result { + if is_resolved(url) { + return Ok(url.to_string()); + } let client = reqwest::Client::builder() .redirect(Policy::none()) .build()?; @@ -111,6 +115,13 @@ async fn resolve_redirects(url: &str) -> Result { Ok(url.to_string()) } +/// Checks if the url is already in a format that can be used for retrieving information +/// about the post +fn is_resolved(url: &str) -> bool { + let r = regex!(r#"^http(s)?://(www\.)?reddit\.com/r/\S+?/comments/\S+$"#); + r.is_match(url) +} + #[cfg(test)] mod test { #[tokio::test]