use phf::phf_map; use curl::easy::Easy; 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 { let ret = match URLS.get(domain.as_str()) { Some(fixed_domain) => url.replacen(domain.as_str(), fixed_domain.0, 1), None => return None, }; Some(match ret.split_once('?') { Some((data, _trash)) => String::from(data), None => ret, }) } pub fn check_domains(text: String) -> bool { for domain in URLS.keys() { if text.contains(domain) { return true; } } false } 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 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(word), String::from(*domain)); } } } (String::from("Error"), String::from("Error")) } fn get_tiktok_redirection(url: String) -> Result { let mut easy = Easy::new(); easy.url(url.as_str())?; easy.perform()?; Ok(match easy.redirect_url()? { Some(furl) => String::from(furl), 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()) ); } #[test] fn test_rewrite_refered_instagram() { let url_and_domain = get_domain_from_text("https://www.instagram.com/reel/DJAE4JXSvHn/".to_string()); let domain = filter_string(url_and_domain.0, url_and_domain.1); assert_eq!( domain, Some("https://ddinstagram.com/reel/DJAE4JXSvHn/".to_string()) ); }