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:
@@ -34,12 +34,12 @@ use crate::{
|
||||
use super::utils::{filter_command, CommandParse};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DownloadData {
|
||||
pub struct DownloadDataCommand {
|
||||
pub format: String,
|
||||
pub id: u32,
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for DownloadData {
|
||||
impl CommandParse<Self> for DownloadDataCommand {
|
||||
fn parse(s: &str, bot_name: &str) -> Result<Self, strum::ParseError> {
|
||||
let re = Regex::new(r"^/d_(?P<file_format>[a-zA-Z0-9]+)_(?P<book_id>\d+)$").unwrap();
|
||||
|
||||
@@ -55,7 +55,7 @@ impl CommandParse<Self> for DownloadData {
|
||||
let file_format = &caps["file_format"];
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
|
||||
Ok(DownloadData {
|
||||
Ok(DownloadDataCommand {
|
||||
format: file_format.to_string(),
|
||||
id: book_id,
|
||||
})
|
||||
@@ -63,18 +63,18 @@ impl CommandParse<Self> for DownloadData {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StartDownloadData {
|
||||
pub struct StartDownloadCommand {
|
||||
pub id: u32,
|
||||
}
|
||||
|
||||
impl ToString for StartDownloadData {
|
||||
impl ToString for StartDownloadCommand {
|
||||
fn to_string(&self) -> String {
|
||||
let StartDownloadData { id } = self;
|
||||
let StartDownloadCommand { id } = self;
|
||||
format!("/d_{id}")
|
||||
}
|
||||
}
|
||||
|
||||
impl CommandParse<Self> for StartDownloadData {
|
||||
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();
|
||||
|
||||
@@ -89,7 +89,7 @@ impl CommandParse<Self> for StartDownloadData {
|
||||
|
||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
||||
|
||||
Ok(StartDownloadData { id: book_id })
|
||||
Ok(StartDownloadCommand { id: book_id })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,6 +127,47 @@ impl FromStr for DownloadQueryData {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, EnumIter)]
|
||||
pub enum DownloadArchiveCommands {
|
||||
Sequence { id: u32, file_format: String },
|
||||
Author { id: u32, file_format: String },
|
||||
Translator { id: u32, file_format: String }
|
||||
}
|
||||
|
||||
impl ToString for DownloadArchiveCommands {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
DownloadArchiveCommands::Sequence { id, file_format } => format!("da_s_{id}_{file_format}"),
|
||||
DownloadArchiveCommands::Author { id, file_format } => format!("da_a_{id}_{file_format}"),
|
||||
DownloadArchiveCommands::Translator { id, file_format } => format!("da_t_{id}_{file_format}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for DownloadArchiveCommands {
|
||||
type Err = strum::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let re = Regex::new(r"^/da_(?P<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 obj_id: u32 = caps["id"].parse().unwrap();
|
||||
let file_type: String = caps["file_type"].to_string();
|
||||
|
||||
match &caps["type"] {
|
||||
"s" => Ok(DownloadArchiveCommands::Sequence { id: obj_id, file_format: file_type }),
|
||||
"a" => Ok(DownloadArchiveCommands::Author { id: obj_id, file_format: file_type }),
|
||||
"t" => Ok(DownloadArchiveCommands::Translator { id: obj_id, file_format: file_type }),
|
||||
_ => Err(strum::ParseError::VariantNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn _send_cached(
|
||||
message: &Message,
|
||||
bot: &CacheMe<Throttle<Bot>>,
|
||||
@@ -149,7 +190,7 @@ async fn _send_cached(
|
||||
async fn send_cached_message(
|
||||
message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: DownloadData,
|
||||
download_data: DownloadDataCommand,
|
||||
donation_notification_cache: Cache<ChatId, bool>,
|
||||
need_delete_message: bool,
|
||||
) -> BotHandlerInternal {
|
||||
@@ -204,7 +245,7 @@ async fn _send_downloaded_file(
|
||||
async fn send_with_download_from_channel(
|
||||
message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: DownloadData,
|
||||
download_data: DownloadDataCommand,
|
||||
donation_notification_cache: Cache<ChatId, bool>,
|
||||
need_delete_message: bool,
|
||||
) -> BotHandlerInternal {
|
||||
@@ -226,7 +267,7 @@ async fn download_handler(
|
||||
message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
cache: BotCache,
|
||||
download_data: DownloadData,
|
||||
download_data: DownloadDataCommand,
|
||||
donation_notification_cache: Cache<ChatId, bool>,
|
||||
need_delete_message: bool,
|
||||
) -> BotHandlerInternal {
|
||||
@@ -257,7 +298,7 @@ async fn download_handler(
|
||||
async fn get_download_keyboard_handler(
|
||||
message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: StartDownloadData,
|
||||
download_data: StartDownloadCommand,
|
||||
) -> BotHandlerInternal {
|
||||
let book = match get_book(download_data.id).await {
|
||||
Ok(v) => v,
|
||||
@@ -304,12 +345,12 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
|
||||
dptree::entry()
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
.chain(filter_command::<DownloadData>())
|
||||
.chain(filter_command::<DownloadDataCommand>())
|
||||
.endpoint(
|
||||
|message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
cache: BotCache,
|
||||
download_data: DownloadData,
|
||||
download_data: DownloadDataCommand,
|
||||
app_state: AppState| async move {
|
||||
download_handler(
|
||||
message,
|
||||
@@ -325,11 +366,11 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
|
||||
)
|
||||
.branch(
|
||||
Update::filter_message()
|
||||
.chain(filter_command::<StartDownloadData>())
|
||||
.chain(filter_command::<StartDownloadCommand>())
|
||||
.endpoint(
|
||||
|message: Message,
|
||||
bot: CacheMe<Throttle<Bot>>,
|
||||
download_data: StartDownloadData| async move {
|
||||
download_data: StartDownloadCommand| async move {
|
||||
get_download_keyboard_handler(message, bot, download_data).await
|
||||
},
|
||||
),
|
||||
@@ -349,7 +390,7 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
|
||||
cq.message.unwrap(),
|
||||
bot,
|
||||
cache,
|
||||
DownloadData {
|
||||
DownloadDataCommand {
|
||||
format: file_type,
|
||||
id: book_id,
|
||||
},
|
||||
|
||||
@@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine};
|
||||
use reqwest::StatusCode;
|
||||
use std::fmt;
|
||||
|
||||
use crate::{bots::approved_bot::modules::download::DownloadData, config};
|
||||
use crate::{bots::approved_bot::modules::download::DownloadDataCommand, config};
|
||||
|
||||
use self::types::{CachedMessage, DownloadFile};
|
||||
|
||||
@@ -22,9 +22,9 @@ impl fmt::Display for DownloadError {
|
||||
impl std::error::Error for DownloadError {}
|
||||
|
||||
pub async fn get_cached_message(
|
||||
download_data: &DownloadData,
|
||||
download_data: &DownloadDataCommand,
|
||||
) -> Result<CachedMessage, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let DownloadData { format, id } = download_data;
|
||||
let DownloadDataCommand { format, id } = download_data;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
@@ -47,9 +47,9 @@ pub async fn get_cached_message(
|
||||
}
|
||||
|
||||
pub async fn download_file(
|
||||
download_data: &DownloadData,
|
||||
download_data: &DownloadDataCommand,
|
||||
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
||||
let DownloadData { format, id } = download_data;
|
||||
let DownloadDataCommand { format, id } = download_data;
|
||||
|
||||
let response = reqwest::Client::new()
|
||||
.get(format!(
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::cmp::min;
|
||||
|
||||
use crate::bots::approved_bot::modules::download::StartDownloadData;
|
||||
use crate::bots::approved_bot::modules::download::StartDownloadCommand;
|
||||
|
||||
use super::types::{
|
||||
Author, AuthorBook, Book, BookAuthor, BookGenre, SearchBook, Sequence, Translator,
|
||||
@@ -416,7 +416,7 @@ impl Format for Book {
|
||||
false => "".to_string(),
|
||||
};
|
||||
|
||||
let download_command = (StartDownloadData { id: self.id }).to_string();
|
||||
let download_command = (StartDownloadCommand { id: self.id }).to_string();
|
||||
let download_links = format!("Скачать:\n📥{download_command}");
|
||||
|
||||
let required_data_len: usize = format!("{book_title}{annotations}{download_links}").len();
|
||||
|
||||
Reference in New Issue
Block a user