Add association of urls after import

Signed-off-by: trivernis <trivernis@protonmail.com>
main
trivernis 2 years ago
parent 5ce7fc8cdd
commit 4d8062b16f
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
Cargo.lock generated

@ -928,7 +928,7 @@ dependencies = [
[[package]]
name = "hydrus-utils"
version = "0.4.0"
version = "0.4.1"
dependencies = [
"clap",
"color-eyre",

@ -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"

@ -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(())
}

@ -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(())
}

@ -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<T3Data> {
/// Resolves reddit redirects
#[tracing::instrument(level = "debug")]
async fn resolve_redirects(url: &str) -> Result<String> {
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<String> {
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]

Loading…
Cancel
Save