mini_admin_bot/src/rewrite_links/links_to_rewrite.rs

68 lines
1.8 KiB
Rust

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",
"x.com" => "fxtwitter.com",
};
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),
None => return None,
};
Some(match ret.split_once('?') {
Some((data, _trash)) => String::from(data),
None => ret,
})
}
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
}
}
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 String::from(*domain).contains("vm.tiktok.com") {
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<String, curl::Error> {
let mut easy = Easy::new();
easy.url(url.as_str())?;
easy.perform()?;
Ok(match easy.redirect_url()? {
Some(furl) => String::from(furl),
None => url,
})
}