mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Refactor
This commit is contained in:
111
src/bots/approved_bot/modules/download/callback_data.rs
Normal file
111
src/bots/approved_bot/modules/download/callback_data.rs
Normal file
@@ -0,0 +1,111 @@
|
||||
use std::str::FromStr;
|
||||
|
||||
use regex::Regex;
|
||||
use strum_macros::EnumIter;
|
||||
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadQueryData {
|
||||
DownloadData { book_id: u32, file_type: String },
|
||||
}
|
||||
|
||||
impl ToString for DownloadQueryData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadQueryData::DownloadData { book_id, file_type } => {
|
||||
format!("d_{book_id}_{file_type}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DownloadQueryData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^d_(?P<book_id>\d+)_(?P<file_type>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
let file_type: String = caps["file_type"].to_string();
|
||||
|
||||
Ok(DownloadQueryData::DownloadData { book_id, file_type })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadArchiveQueryData {
|
||||
Sequence { id: u32, file_type: String },
|
||||
Author { id: u32, file_type: String },
|
||||
Translator { id: u32, file_type: String }
|
||||
}
|
||||
|
||||
impl ToString for DownloadArchiveQueryData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadArchiveQueryData::Sequence { id, file_type } => format!("da_s_{id}_{file_type}"),
|
||||
DownloadArchiveQueryData::Author { id, file_type } => format!("da_a_{id}_{file_type}"),
|
||||
DownloadArchiveQueryData::Translator { id, file_type } => format!("da_t_{id}_{file_type}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DownloadArchiveQueryData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^da_(?P<obj_type>[s|a|t])_(?P<id>\d+)_(?P<file_type>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let id: u32 = caps["id"].parse().unwrap();
|
||||
let file_type: String = caps["file_type"].to_string();
|
||||
|
||||
Ok(
|
||||
match caps["obj_type"].to_string().as_str() {
|
||||
"s" => DownloadArchiveQueryData::Sequence { id, file_type },
|
||||
"a" => DownloadArchiveQueryData::Author { id, file_type },
|
||||
"t" => DownloadArchiveQueryData::Translator { id, file_type },
|
||||
_ => return Err(strum::ParseError::VariantNotFound)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CheckArchiveStatus {
|
||||
pub task_id: String
|
||||
}
|
||||
|
||||
impl ToString for CheckArchiveStatus {
|
||||
fn to_string(&self) -> String {
|
||||
format!("check_da_{}", self.task_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for CheckArchiveStatus {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^check_da_(?P<task_id>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let task_id: String = caps["task_id"].parse().unwrap();
|
||||
|
||||
Ok(CheckArchiveStatus { task_id })
|
||||
}
|
||||
}
|
||||
77
src/bots/approved_bot/modules/download/commads.rs
Normal file
77
src/bots/approved_bot/modules/download/commads.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use regex::Regex;
|
||||
use strum_macros::EnumIter;
|
||||
|
||||
use crate::bots::approved_bot::modules::utils::CommandParse;
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StartDownloadCommand {
|
||||
pub id: u32,
|
||||
}
|
||||
|
||||
impl ToString for StartDownloadCommand {
|
||||
fn to_string(&self) -> String {
|
||||
let StartDownloadCommand { id } = self;
|
||||
format!("/d_{id}")
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for StartDownloadCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/d_(?P<book_id>\d+)$").unwrap();
|
||||
|
||||
let full_bot_name = format!("@{bot_name}");
|
||||
let after_replace = s.replace(&full_bot_name, "");
|
||||
|
||||
let caps = re.captures(&after_replace);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
|
||||
Ok(StartDownloadCommand { id: book_id })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadArchiveCommand {
|
||||
Sequence { id: u32},
|
||||
Author { id: u32 },
|
||||
Translator { id: u32 }
|
||||
}
|
||||
|
||||
impl ToString for DownloadArchiveCommand {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadArchiveCommand::Sequence { id } => format!("/da_s_{id}"),
|
||||
DownloadArchiveCommand::Author { id } => format!("/da_a_{id}"),
|
||||
DownloadArchiveCommand::Translator { id } => format!("/da_t_{id}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for DownloadArchiveCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/da_(?P<type>[s|a|t])_(?P<id>\d+)$").unwrap();
|
||||
|
||||
let full_bot_name = format!("@{bot_name}");
|
||||
let after_replace = s.replace(&full_bot_name, "");
|
||||
|
||||
let caps = re.captures(&after_replace);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let obj_id: u32 = caps["id"].parse().unwrap();
|
||||
|
||||
match &caps["type"] {
|
||||
"s" => Ok(DownloadArchiveCommand::Sequence { id: obj_id }),
|
||||
"a" => Ok(DownloadArchiveCommand::Author { id: obj_id }),
|
||||
"t" => Ok(DownloadArchiveCommand::Translator { id: obj_id }),
|
||||
_ => Err(strum::ParseError::VariantNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
use std::{str::FromStr, time::Duration};
|
||||
pub mod commads;
|
||||
pub mod callback_data;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::Utc;
|
||||
use futures::TryStreamExt;
|
||||
use regex::Regex;
|
||||
use strum_macros::EnumIter;
|
||||
|
||||
|
||||
use teloxide::{
|
||||
adaptors::{CacheMe, Throttle},
|
||||
dispatching::UpdateFilterExt,
|
||||
@@ -28,193 +31,16 @@ use crate::{
|
||||
batch_downloader::{create_task, get_task, TaskStatus}
|
||||
|
||||
},
|
||||
tools::filter_callback_query,
|
||||
tools::filter_callback_query, modules::download::callback_data::DownloadArchiveQueryData,
|
||||
},
|
||||
BotHandlerInternal,
|
||||
},
|
||||
bots_manager::BotCache,
|
||||
};
|
||||
|
||||
use super::utils::{filter_command, CommandParse};
|
||||
use self::{callback_data::{CheckArchiveStatus, DownloadQueryData}, commads::{StartDownloadCommand, DownloadArchiveCommand}};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StartDownloadCommand {
|
||||
pub id: u32,
|
||||
}
|
||||
|
||||
impl ToString for StartDownloadCommand {
|
||||
fn to_string(&self) -> String {
|
||||
let StartDownloadCommand { id } = self;
|
||||
format!("/d_{id}")
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for StartDownloadCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/d_(?P<book_id>\d+)$").unwrap();
|
||||
|
||||
let full_bot_name = format!("@{bot_name}");
|
||||
let after_replace = s.replace(&full_bot_name, "");
|
||||
|
||||
let caps = re.captures(&after_replace);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
|
||||
Ok(StartDownloadCommand { id: book_id })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadQueryData {
|
||||
DownloadData { book_id: u32, file_type: String },
|
||||
}
|
||||
|
||||
impl ToString for DownloadQueryData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadQueryData::DownloadData { book_id, file_type } => {
|
||||
format!("d_{book_id}_{file_type}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DownloadQueryData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^d_(?P<book_id>\d+)_(?P<file_type>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
let file_type: String = caps["file_type"].to_string();
|
||||
|
||||
Ok(DownloadQueryData::DownloadData { book_id, file_type })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadArchiveCommand {
|
||||
Sequence { id: u32},
|
||||
Author { id: u32 },
|
||||
Translator { id: u32 }
|
||||
}
|
||||
|
||||
impl ToString for DownloadArchiveCommand {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadArchiveCommand::Sequence { id } => format!("/da_s_{id}"),
|
||||
DownloadArchiveCommand::Author { id } => format!("/da_a_{id}"),
|
||||
DownloadArchiveCommand::Translator { id } => format!("/da_t_{id}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for DownloadArchiveCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/da_(?P<type>[s|a|t])_(?P<id>\d+)$").unwrap();
|
||||
|
||||
let full_bot_name = format!("@{bot_name}");
|
||||
let after_replace = s.replace(&full_bot_name, "");
|
||||
|
||||
let caps = re.captures(&after_replace);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let obj_id: u32 = caps["id"].parse().unwrap();
|
||||
|
||||
match &caps["type"] {
|
||||
"s" => Ok(DownloadArchiveCommand::Sequence { id: obj_id }),
|
||||
"a" => Ok(DownloadArchiveCommand::Author { id: obj_id }),
|
||||
"t" => Ok(DownloadArchiveCommand::Translator { id: obj_id }),
|
||||
_ => Err(strum::ParseError::VariantNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadArchiveQueryData {
|
||||
Sequence { id: u32, file_type: String },
|
||||
Author { id: u32, file_type: String },
|
||||
Translator { id: u32, file_type: String }
|
||||
}
|
||||
|
||||
impl ToString for DownloadArchiveQueryData {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadArchiveQueryData::Sequence { id, file_type } => format!("da_s_{id}_{file_type}"),
|
||||
DownloadArchiveQueryData::Author { id, file_type } => format!("da_a_{id}_{file_type}"),
|
||||
DownloadArchiveQueryData::Translator { id, file_type } => format!("da_t_{id}_{file_type}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DownloadArchiveQueryData {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^da_(?P<obj_type>[s|a|t])_(?P<id>\d+)_(?P<file_type>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let id: u32 = caps["id"].parse().unwrap();
|
||||
let file_type: String = caps["file_type"].to_string();
|
||||
|
||||
Ok(
|
||||
match caps["obj_type"].to_string().as_str() {
|
||||
"s" => DownloadArchiveQueryData::Sequence { id, file_type },
|
||||
"a" => DownloadArchiveQueryData::Author { id, file_type },
|
||||
"t" => DownloadArchiveQueryData::Translator { id, file_type },
|
||||
_ => return Err(strum::ParseError::VariantNotFound)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CheckArchiveStatus {
|
||||
pub task_id: String
|
||||
}
|
||||
|
||||
impl ToString for CheckArchiveStatus {
|
||||
fn to_string(&self) -> String {
|
||||
format!("check_da_{}", self.task_id)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for CheckArchiveStatus {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^check_da_(?P<task_id>\w+)$").unwrap();
|
||||
|
||||
let caps = re.captures(s);
|
||||
let caps = match caps {
|
||||
Some(v) => v,
|
||||
None => return Err(strum::ParseError::VariantNotFound),
|
||||
};
|
||||
|
||||
let task_id: String = caps["task_id"].parse().unwrap();
|
||||
|
||||
Ok(CheckArchiveStatus { task_id })
|
||||
}
|
||||
}
|
||||
use super::utils::filter_command;
|
||||
|
||||
|
||||
fn get_check_keyboard(task_id: String) -> InlineKeyboardMarkup {
|
||||
@@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine};
|
||||
use reqwest::StatusCode;
|
||||
use std::fmt;
|
||||
|
||||
use crate::{config, bots::approved_bot::modules::download::DownloadQueryData};
|
||||
use crate::{config, bots::approved_bot::modules::download::callback_data::DownloadQueryData};
|
||||
|
||||
use self::types::{CachedMessage, DownloadFile, DownloadLink};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cmp::min;
|
||||
|
||||
use crate::bots::approved_bot::modules::download::{StartDownloadCommand, DownloadArchiveCommand};
|
||||
use crate::bots::approved_bot::modules::download::commads::{StartDownloadCommand, DownloadArchiveCommand};
|
||||
|
||||
use super::types::{
|
||||
Author, AuthorBook, Book, BookAuthor, BookGenre, SearchBook, Sequence, Translator,
|
||||
|
||||
Reference in New Issue
Block a user