clean warnings and add tests
This commit is contained in:
@@ -2,17 +2,19 @@ use phf::phf_map;
|
||||
|
||||
use curl::easy::Easy;
|
||||
|
||||
static URLS: phf::Map<&'static str, &'static str> = phf_map! {
|
||||
"www.tiktok.com" => "vxtiktok.com",
|
||||
"vm.tiktok.com" => "vxtiktok.com",
|
||||
"lite.tiktok.com" => "vxtiktok.com",
|
||||
"https://x.com" => "https://fxtwitter.com",
|
||||
"https://twitter.com" => "https://fxtwitter.com",
|
||||
static URLS: phf::Map<&'static str, (&'static str, bool)> = phf_map! {
|
||||
"www.tiktok.com" => ("vxtiktok.com", false),
|
||||
"vm.tiktok.com" => ("vxtiktok.com", true),
|
||||
"vt.tiktok.com" => ("vxtiktok.com", true),
|
||||
"lite.tiktok.com" => ("vxtiktok.com", true),
|
||||
"www.instagram.com" => ("ddinstagram.com", false),
|
||||
"https://x.com" => ("https://fxtwitter.com", false),
|
||||
"https://twitter.com" => ("https://fxtwitter.com",false),
|
||||
};
|
||||
|
||||
pub fn filter_string(url: String, domain: String) -> Option<String> {
|
||||
let ret = match URLS.get(domain.as_str()) {
|
||||
Some(fixed_domain) => url.replacen(domain.as_str(),fixed_domain,1),
|
||||
Some(fixed_domain) => url.replacen(domain.as_str(), fixed_domain.0, 1),
|
||||
None => return None,
|
||||
};
|
||||
|
||||
@@ -22,19 +24,10 @@ pub fn filter_string(url: String, domain: String) -> Option<String> {
|
||||
})
|
||||
}
|
||||
|
||||
fn get_domain(url: String) -> Option<String> {
|
||||
for domain in URLS.keys() {
|
||||
if url.contains(domain) {
|
||||
return Some(String::from(*domain))
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn check_domains(text: String) -> bool {
|
||||
for domain in URLS.keys() {
|
||||
if text.contains(domain) {
|
||||
return true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
@@ -44,14 +37,14 @@ pub fn get_domain_from_text(text: String) -> (String, String) {
|
||||
for word in text.split(' ') {
|
||||
for domain in URLS.keys() {
|
||||
if word.contains(domain) {
|
||||
if String::from(*domain).contains("vm.tiktok.com") || String::from(*domain).contains("lite.tiktok.com") {
|
||||
if URLS[domain].1 {
|
||||
let url = match get_tiktok_redirection(String::from(word)) {
|
||||
Ok(furl) => furl,
|
||||
Err(_e) => String::from(word),
|
||||
};
|
||||
return (String::from(url), String::from("www.tiktok.com"))
|
||||
return (String::from(url), String::from("www.tiktok.com"));
|
||||
}
|
||||
return (String::from(word), String::from(*domain))
|
||||
return (String::from(word), String::from(*domain));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -67,3 +60,37 @@ fn get_tiktok_redirection(url: String) -> Result<String, curl::Error> {
|
||||
None => url,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_domains() {
|
||||
assert_eq!(
|
||||
true,
|
||||
check_domains("https://vm.tiktok.com/ZGeouHd2t/".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
false,
|
||||
check_domains("https://randomwebsite.com".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_clean_tiktok() {
|
||||
let domain = get_domain_from_text(
|
||||
"https://www.tiktok.com/@kramkang/video/7417808362957589778".to_string(),
|
||||
);
|
||||
assert_eq!(domain.1, "www.tiktok.com");
|
||||
assert_eq!(
|
||||
domain.0,
|
||||
"https://www.tiktok.com/@kramkang/video/7417808362957589778"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rewrite_refered_tiktok() {
|
||||
let url_and_domain = get_domain_from_text("https://vm.tiktok.com/ZGeouHd2t/".to_string());
|
||||
let domain = filter_string(url_and_domain.0, url_and_domain.1);
|
||||
assert_eq!(
|
||||
domain,
|
||||
Some("https://vxtiktok.com/@/video/7417808362957589778".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user