update dependencies and add rewrite urls

This commit is contained in:
Guillermo Roche 2024-09-24 21:09:06 +02:00
parent c94df04eed
commit deec5840fc
Signed by: groche97
GPG Key ID: 041FB85BEEA4B9B0
8 changed files with 1468 additions and 540 deletions

1833
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ edition = "2018"
[dependencies] [dependencies]
#teloxide = { version = "0.11.3", features = ["macros", "auto-send"] } #teloxide = { version = "0.11.3", features = ["macros", "auto-send"] }
teloxide = { version = "0.12.2", features = ["macros", "auto-send"] } teloxide = { version = "0.12", features = ["macros", "auto-send"] }
futures = "0.3.5" futures = "0.3.5"
log = "0.4" log = "0.4"
pretty_env_logger = "0.4.0" pretty_env_logger = "0.4.0"
@ -20,6 +20,9 @@ chrono = "*"
sqlite = "*" sqlite = "*"
lazy_static = "1.4.0" lazy_static = "1.4.0"
image = "*" image = "*"
turbojpeg = "*"
phf = { version = "0.11", features = ["macros"] }
curl = "*"
[profile.release] [profile.release]
lto = true lto = true

View File

@ -36,10 +36,10 @@ impl<'a> Database {
let mut statement = builder let mut statement = builder
.prepare("SELECT id_stiker FROM bmedia WHERE id_stiker = ? and media_type=?") .prepare("SELECT id_stiker FROM bmedia WHERE id_stiker = ? and media_type=?")
.unwrap(); .unwrap();
statement.bind(1, stiker_id).unwrap(); statement.bind((1, stiker_id)).unwrap();
statement.bind(2, media_type).unwrap(); statement.bind((2, media_type)).unwrap();
statement.next().unwrap(); statement.next().unwrap();
match statement.read::<String>(0) { match statement.read::<String, _>(0) {
Ok(_s) => true, Ok(_s) => true,
Err(_e) => false, Err(_e) => false,
} }
@ -50,12 +50,12 @@ impl<'a> Database {
let mut statement = builder let mut statement = builder
.prepare("INSERT INTO bmedia (id_stiker, id_group, media_type) VALUES (?,?,?)") .prepare("INSERT INTO bmedia (id_stiker, id_group, media_type) VALUES (?,?,?)")
.unwrap(); .unwrap();
statement.bind(1, stiker_id).unwrap(); statement.bind((1, stiker_id)).unwrap();
statement.bind(2, group_id).unwrap(); statement.bind((2, group_id)).unwrap();
statement.bind(3, media_type).unwrap(); statement.bind((3, media_type)).unwrap();
match statement.next(){ match statement.next(){
Ok(s)=>true, Ok(_s)=>true,
Err(e)=>false, Err(_e)=>false,
} }
} }
} }

View File

@ -6,6 +6,7 @@ mod ban_stiker;
mod database; mod database;
mod spoiler_mangas; mod spoiler_mangas;
mod telegram_utils; mod telegram_utils;
mod rewrite_links;
#[derive(BotCommands, Clone)] #[derive(BotCommands, Clone)]
#[command(rename_rule = "lowercase", description = "These commands are supported:")] #[command(rename_rule = "lowercase", description = "These commands are supported:")]
@ -70,6 +71,10 @@ async fn run(){
dptree::filter(move |msg: Message| (is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string()))) dptree::filter(move |msg: Message| (is_photo(msg.clone()) && p2.compar_party(&msg.chat.id.to_string())))
.endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)), .endpoint(|msg: Message, bot: Bot| spoiler_mangas::check_image(msg, bot)),
) )
.branch(
dptree::filter(move |msg: Message| rewrite_links::check_contain_links::contain_links(msg.clone()))
.endpoint(|msg: Message, bot: Bot| rewrite_links::check_contain_links::fix_links(msg, bot)),
)
.branch( .branch(
dptree::filter(move |msg: Message| permissions.compar_party(&msg.chat.id.to_string())) dptree::filter(move |msg: Message| permissions.compar_party(&msg.chat.id.to_string()))
.endpoint(|msg: Message, bot: Bot| pole_dialogue::exe_pole(msg, bot)), .endpoint(|msg: Message, bot: Bot| pole_dialogue::exe_pole(msg, bot)),

View File

@ -92,9 +92,9 @@ impl<'a> DatabasePole{
let mut statement = builder let mut statement = builder
.prepare("SELECT date FROM last WHERE id_group = ? order by date desc") .prepare("SELECT date FROM last WHERE id_group = ? order by date desc")
.unwrap(); .unwrap();
statement.bind(1, group_id).unwrap(); statement.bind((1, group_id)).unwrap();
statement.next().unwrap(); statement.next().unwrap();
statement.read::<String>(0) statement.read::<String, _>(0)
} }
pub fn write_points(&self, group_id : &str, user_id: &str, user_name: &str, date: &str, points: i64){ pub fn write_points(&self, group_id : &str, user_id: &str, user_name: &str, date: &str, points: i64){
@ -102,27 +102,27 @@ impl<'a> DatabasePole{
let mut statement = builder let mut statement = builder
.prepare("SELECT npole FROM poles WHERE user = ? AND id_group = ?") .prepare("SELECT npole FROM poles WHERE user = ? AND id_group = ?")
.unwrap(); .unwrap();
statement.bind(1, user_id).unwrap(); statement.bind((1, user_id)).unwrap();
statement.bind(2, group_id).unwrap(); statement.bind((2, group_id)).unwrap();
match statement.next().unwrap() { match statement.next().unwrap() {
State::Row => { State::Row => {
let mut statement2 = builder let mut statement2 = builder
.prepare("UPDATE poles SET npole=? WHERE user=? AND id_group=?") .prepare("UPDATE poles SET npole=? WHERE user=? AND id_group=?")
.unwrap(); .unwrap();
statement2.bind(1, statement.read::<i64>(0).unwrap()+points).unwrap(); statement2.bind((1, statement.read::<i64, _>(0).unwrap()+points)).unwrap();
statement2.bind(2, user_id).unwrap(); statement2.bind((2, user_id)).unwrap();
statement2.bind(3, group_id).unwrap(); statement2.bind((3, group_id)).unwrap();
statement2.next().unwrap(); statement2.next().unwrap();
} }
State::Done => { State::Done => {
let mut statement2 = builder let mut statement2 = builder
.prepare("INSERT INTO poles (npole, user, user_name, id_group) VALUES (?,?,?,?)") .prepare("INSERT INTO poles (npole, user, user_name, id_group) VALUES (?,?,?,?)")
.unwrap(); .unwrap();
statement2.bind(1, points).unwrap(); statement2.bind((1, points)).unwrap();
statement2.bind(2, user_id).unwrap(); statement2.bind((2, user_id)).unwrap();
statement2.bind(3, user_name).unwrap(); statement2.bind((3, user_name)).unwrap();
statement2.bind(4, group_id).unwrap(); statement2.bind((4, group_id)).unwrap();
statement2.next().unwrap(); statement2.next().unwrap();
} }
@ -131,10 +131,10 @@ impl<'a> DatabasePole{
let mut statement3 = builder let mut statement3 = builder
.prepare("INSERT INTO last (date, id_group, user, points) VALUES (?,?,?,?)") .prepare("INSERT INTO last (date, id_group, user, points) VALUES (?,?,?,?)")
.unwrap(); .unwrap();
statement3.bind(1, date).unwrap(); statement3.bind((1, date)).unwrap();
statement3.bind(2, group_id).unwrap(); statement3.bind((2, group_id)).unwrap();
statement3.bind(3, user_id).unwrap(); statement3.bind((3, user_id)).unwrap();
statement3.bind(4, points).unwrap(); statement3.bind((4, points)).unwrap();
statement3.next().unwrap(); statement3.next().unwrap();
//while let State::Row = statement3.next().unwrap() { //while let State::Row = statement3.next().unwrap() {
// log::info!("name = {}", statement.read::<String>(0).unwrap()); // log::info!("name = {}", statement.read::<String>(0).unwrap());
@ -146,11 +146,11 @@ impl<'a> DatabasePole{
let mut statement = builder let mut statement = builder
.prepare("SELECT points FROM last WHERE id_group = ? AND user = ? AND date = :date") .prepare("SELECT points FROM last WHERE id_group = ? AND user = ? AND date = :date")
.unwrap(); .unwrap();
statement.bind(1, group_id).unwrap(); statement.bind((1, group_id)).unwrap();
statement.bind(2, user_id).unwrap(); statement.bind((2, user_id)).unwrap();
statement.bind_by_name(":date", date).unwrap(); statement.bind((3, date)).unwrap();
match statement.next().unwrap() { match statement.next().unwrap() {
State::Row => statement.read::<i64>(0).unwrap(), State::Row => statement.read::<i64, _>(0).unwrap(),
State::Done => 0, State::Done => 0,
} }
} }
@ -158,12 +158,12 @@ impl<'a> DatabasePole{
pub fn check_group_points(&self, group_id : &str, date: &str) -> i64{ pub fn check_group_points(&self, group_id : &str, date: &str) -> i64{
let builder = self.ins_connection.lock().unwrap(); let builder = self.ins_connection.lock().unwrap();
let mut statement = builder let mut statement = builder
.prepare("SELECT points FROM last WHERE id_group = ? AND date = :date") .prepare("SELECT points FROM last WHERE id_group = ? AND date = ?")
.unwrap(); .unwrap();
statement.bind(1, group_id).unwrap(); statement.bind((1, group_id)).unwrap();
statement.bind_by_name(":date", date).unwrap(); statement.bind((2, date)).unwrap();
match statement.next().unwrap() { match statement.next().unwrap() {
State::Row => statement.read::<i64>(0).unwrap(), State::Row => statement.read::<i64, _>(0).unwrap(),
State::Done => 0, State::Done => 0,
} }
} }
@ -173,13 +173,13 @@ impl<'a> DatabasePole{
let mut statement = builder let mut statement = builder
.prepare("SELECT npole, user_name FROM poles where id_group=? order by npole desc limit 10") .prepare("SELECT npole, user_name FROM poles where id_group=? order by npole desc limit 10")
.unwrap(); .unwrap();
statement.bind(1, group_id).unwrap(); statement.bind((1, group_id)).unwrap();
let mut ret = Vec::new(); let mut ret = Vec::new();
for i in 1 .. 10 { for i in 1 .. 10 {
match statement.next().unwrap() { match statement.next().unwrap() {
State::Row => ret.push(( State::Row => ret.push((
statement.read::<i64>(0).unwrap(), statement.read::<i64, _>(0).unwrap(),
statement.read::<String>(1).unwrap() statement.read::<String, _>(1).unwrap()
)), )),
State::Done => return ret, State::Done => return ret,
} }

View File

@ -0,0 +1,28 @@
use teloxide::prelude::*;
use teloxide::{
net::Download,
requests::{Requester,MultipartRequest},
types::{MessageEntity, MessageEntityKind},
payloads::SendPhoto,
Bot,
};
use crate::rewrite_links::links_to_rewrite;
pub fn contain_links(msg: Message) -> bool {
match msg.text() {
Some(text) => links_to_rewrite::check_domains(String::from(text)),
None => false,
}
}
pub async fn fix_links(msg: Message, bot: Bot) -> anyhow::Result<()> {
let text = msg.text().unwrap();
let url_and_domain = links_to_rewrite::get_domain_from_text(String::from(text));
bot.send_message(msg.chat.id,
links_to_rewrite::filter_string(
url_and_domain.0,
url_and_domain.1
).unwrap()).await?;
Ok(())
}

View File

@ -0,0 +1,67 @@
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,
})
}

2
src/rewrite_links/mod.rs Normal file
View File

@ -0,0 +1,2 @@
mod links_to_rewrite;
pub mod check_contain_links;