From 41bc58905072f86aec0c44b85858d0428518563e Mon Sep 17 00:00:00 2001 From: lifegpc Date: Wed, 2 Mar 2022 22:49:42 +0800 Subject: [PATCH] remove /jump.php track links --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/main.rs | 1 + src/parser/description.rs | 8 +++++++- src/pixiv_link.rs | 17 +++++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c49508..ba8ac80 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1075,6 +1075,7 @@ dependencies = [ "reqwest", "spin_on", "tokio", + "urlparse", "utf16string", "winapi", ] @@ -1702,6 +1703,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "urlparse" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "110352d4e9076c67839003c7788d8604e24dcded13e0b375af3efaa8cf468517" + [[package]] name = "utf16string" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 08762d8..603486d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,13 @@ html_parser = "0.6.2" int-enum = { version = "0.4", optional = true } lazy_static = "1.4" json = "0.12" -utf16string = { version= "0.2", optional = true } regex = "1" reqwest = { version = "0.11", features = ["brotli", "deflate", "gzip", "rustls-tls", "socks", "stream"] } RustyXML = "0.3" spin_on = "0.1.1" tokio = { version = "1.17", features = ["rt", "macros", "rt-multi-thread", "time"] } +urlparse = "0.7" +utf16string = { version= "0.2", optional = true } [build-dependencies] cmake = "0.1" diff --git a/src/main.rs b/src/main.rs index 8055231..088c8bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ extern crate lazy_static; extern crate tokio; extern crate regex; extern crate reqwest; +extern crate urlparse; #[cfg(feature = "utf16string")] extern crate utf16string; extern crate xml; diff --git a/src/parser/description.rs b/src/parser/description.rs index 164f072..258023d 100644 --- a/src/parser/description.rs +++ b/src/parser/description.rs @@ -1,4 +1,5 @@ use crate::gettext; +use crate::pixiv_link::remove_track; use html_parser::Dom; use html_parser::Node; use std::collections::HashMap; @@ -98,7 +99,8 @@ impl DescriptionParser { if href.is_some() { let href = href.unwrap(); if href.is_some() { - node.add_attr("href", href.as_ref().unwrap()); + let link = remove_track(href.as_ref().unwrap()); + node.add_attr("href", link.as_str()); } } } @@ -171,4 +173,8 @@ fn test_parse_description() { Some(String::from("a [a\n[bc](a.com)d](b.com)\ndata")), parse_description("a a
bcd
data") ); + assert_eq!( + Some(String::from("https://a.com")), + parse_description("https://a.com") + ) } diff --git a/src/pixiv_link.rs b/src/pixiv_link.rs index 1c51d31..29fc61b 100644 --- a/src/pixiv_link.rs +++ b/src/pixiv_link.rs @@ -1,6 +1,7 @@ use crate::data::json::ToJson; use json::JsonValue; use regex::Regex; +use reqwest::IntoUrl; use std::convert::TryInto; lazy_static! { @@ -98,3 +99,19 @@ impl TryInto for &PixivID { } } } + +pub fn remove_track(url: U) -> String { + let s = String::from(url.as_str()); + let u = urlparse::urlparse(s.as_str()); + let path = u.path.as_str(); + if path.ends_with("/jump.php") { + if u.query.is_some() { + let q = u.query.as_ref().unwrap(); + let re = urlparse::unquote(q); + if re.is_ok() { + return re.unwrap(); + } + } + } + s +}