mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Add download archive command handler
This commit is contained in:
@@ -33,35 +33,6 @@ use crate::{
|
|||||||
|
|
||||||
use super::utils::{filter_command, CommandParse};
|
use super::utils::{filter_command, CommandParse};
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct DownloadDataCommand {
|
|
||||||
pub format: String,
|
|
||||||
pub id: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
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 file_format = &caps["file_format"];
|
|
||||||
let book_id: u32 = caps["book_id"].parse().unwrap();
|
|
||||||
|
|
||||||
Ok(DownloadDataCommand {
|
|
||||||
format: file_format.to_string(),
|
|
||||||
id: book_id,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct StartDownloadCommand {
|
pub struct StartDownloadCommand {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
@@ -144,10 +115,8 @@ impl ToString for DownloadArchiveCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for DownloadArchiveCommand {
|
impl CommandParse<Self> for DownloadArchiveCommand {
|
||||||
type Err = strum::ParseError;
|
fn parse(s: &str, bot_name: &str) -> Result<Self, 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+)$").unwrap();
|
let re = Regex::new(r"^/da_(?P<type>[s|a|t])_(?P<id>\d+)$").unwrap();
|
||||||
|
|
||||||
let caps = re.captures(s);
|
let caps = re.captures(s);
|
||||||
@@ -189,7 +158,7 @@ async fn _send_cached(
|
|||||||
async fn send_cached_message(
|
async fn send_cached_message(
|
||||||
message: Message,
|
message: Message,
|
||||||
bot: CacheMe<Throttle<Bot>>,
|
bot: CacheMe<Throttle<Bot>>,
|
||||||
download_data: DownloadDataCommand,
|
download_data: DownloadQueryData,
|
||||||
donation_notification_cache: Cache<ChatId, bool>,
|
donation_notification_cache: Cache<ChatId, bool>,
|
||||||
need_delete_message: bool,
|
need_delete_message: bool,
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
@@ -244,7 +213,7 @@ async fn _send_downloaded_file(
|
|||||||
async fn send_with_download_from_channel(
|
async fn send_with_download_from_channel(
|
||||||
message: Message,
|
message: Message,
|
||||||
bot: CacheMe<Throttle<Bot>>,
|
bot: CacheMe<Throttle<Bot>>,
|
||||||
download_data: DownloadDataCommand,
|
download_data: DownloadQueryData,
|
||||||
donation_notification_cache: Cache<ChatId, bool>,
|
donation_notification_cache: Cache<ChatId, bool>,
|
||||||
need_delete_message: bool,
|
need_delete_message: bool,
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
@@ -266,7 +235,7 @@ async fn download_handler(
|
|||||||
message: Message,
|
message: Message,
|
||||||
bot: CacheMe<Throttle<Bot>>,
|
bot: CacheMe<Throttle<Bot>>,
|
||||||
cache: BotCache,
|
cache: BotCache,
|
||||||
download_data: DownloadDataCommand,
|
download_data: DownloadQueryData,
|
||||||
donation_notification_cache: Cache<ChatId, bool>,
|
donation_notification_cache: Cache<ChatId, bool>,
|
||||||
need_delete_message: bool,
|
need_delete_message: bool,
|
||||||
) -> BotHandlerInternal {
|
) -> BotHandlerInternal {
|
||||||
@@ -340,29 +309,19 @@ async fn get_download_keyboard_handler(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn get_download_archive_keyboard_handler(
|
||||||
|
message: Message, bot: CacheMe<Throttle<Bot>>, _command: DownloadArchiveCommand
|
||||||
|
) -> BotHandlerInternal {
|
||||||
|
bot
|
||||||
|
.send_message(message.chat.id, "Функция в разработке")
|
||||||
|
.reply_to_message_id(message.id)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_download_hander() -> crate::bots::BotHandler {
|
pub fn get_download_hander() -> crate::bots::BotHandler {
|
||||||
dptree::entry()
|
dptree::entry()
|
||||||
.branch(
|
|
||||||
Update::filter_message()
|
|
||||||
.chain(filter_command::<DownloadDataCommand>())
|
|
||||||
.endpoint(
|
|
||||||
|message: Message,
|
|
||||||
bot: CacheMe<Throttle<Bot>>,
|
|
||||||
cache: BotCache,
|
|
||||||
download_data: DownloadDataCommand,
|
|
||||||
app_state: AppState| async move {
|
|
||||||
download_handler(
|
|
||||||
message,
|
|
||||||
bot,
|
|
||||||
cache,
|
|
||||||
download_data,
|
|
||||||
app_state.chat_donation_notifications_cache,
|
|
||||||
false,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.branch(
|
.branch(
|
||||||
Update::filter_message()
|
Update::filter_message()
|
||||||
.chain(filter_command::<StartDownloadCommand>())
|
.chain(filter_command::<StartDownloadCommand>())
|
||||||
@@ -383,23 +342,25 @@ pub fn get_download_hander() -> crate::bots::BotHandler {
|
|||||||
bot: CacheMe<Throttle<Bot>>,
|
bot: CacheMe<Throttle<Bot>>,
|
||||||
cache: BotCache,
|
cache: BotCache,
|
||||||
app_state: AppState| async move {
|
app_state: AppState| async move {
|
||||||
match download_query_data {
|
download_handler(
|
||||||
DownloadQueryData::DownloadData { book_id, file_type } => {
|
cq.message.unwrap(),
|
||||||
download_handler(
|
bot,
|
||||||
cq.message.unwrap(),
|
cache,
|
||||||
bot,
|
download_query_data,
|
||||||
cache,
|
app_state.chat_donation_notifications_cache,
|
||||||
DownloadDataCommand {
|
true,
|
||||||
format: file_type,
|
)
|
||||||
id: book_id,
|
.await
|
||||||
},
|
|
||||||
app_state.chat_donation_notifications_cache,
|
|
||||||
true,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
.branch(
|
||||||
|
Update::filter_message()
|
||||||
|
.chain(filter_command::<DownloadArchiveCommand>())
|
||||||
|
.endpoint(|
|
||||||
|
message: Message, bot: CacheMe<Throttle<Bot>>, command: DownloadArchiveCommand
|
||||||
|
| async move {
|
||||||
|
get_download_archive_keyboard_handler(message, bot, command).await
|
||||||
|
})
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use base64::{engine::general_purpose, Engine};
|
|||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use crate::{bots::approved_bot::modules::download::DownloadDataCommand, config};
|
use crate::{config, bots::approved_bot::modules::download::DownloadQueryData};
|
||||||
|
|
||||||
use self::types::{CachedMessage, DownloadFile};
|
use self::types::{CachedMessage, DownloadFile};
|
||||||
|
|
||||||
@@ -22,9 +22,9 @@ impl fmt::Display for DownloadError {
|
|||||||
impl std::error::Error for DownloadError {}
|
impl std::error::Error for DownloadError {}
|
||||||
|
|
||||||
pub async fn get_cached_message(
|
pub async fn get_cached_message(
|
||||||
download_data: &DownloadDataCommand,
|
download_data: &DownloadQueryData,
|
||||||
) -> Result<CachedMessage, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<CachedMessage, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let DownloadDataCommand { format, id } = download_data;
|
let DownloadQueryData::DownloadData { book_id: id, file_type: format } = download_data;
|
||||||
|
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = client
|
let response = client
|
||||||
@@ -47,9 +47,9 @@ pub async fn get_cached_message(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn download_file(
|
pub async fn download_file(
|
||||||
download_data: &DownloadDataCommand,
|
download_data: &DownloadQueryData,
|
||||||
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
) -> Result<DownloadFile, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
let DownloadDataCommand { format, id } = download_data;
|
let DownloadQueryData::DownloadData { book_id: id, file_type: format } = download_data;
|
||||||
|
|
||||||
let response = reqwest::Client::new()
|
let response = reqwest::Client::new()
|
||||||
.get(format!(
|
.get(format!(
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ impl FormatTitle for BookAuthor {
|
|||||||
|
|
||||||
let command = (DownloadArchiveCommand::Author { id: *id }).to_string();
|
let command = (DownloadArchiveCommand::Author { id: *id }).to_string();
|
||||||
|
|
||||||
format!("👤 {last_name} {first_name} {middle_name}\n{command}")
|
format!("👤 {last_name} {first_name} {middle_name}\nСкачать все книги архивом: {command}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +69,7 @@ impl FormatTitle for BookTranslator {
|
|||||||
|
|
||||||
let command = (DownloadArchiveCommand::Translator { id: *id }).to_string();
|
let command = (DownloadArchiveCommand::Translator { id: *id }).to_string();
|
||||||
|
|
||||||
format!("👤 {last_name} {first_name} {middle_name}\n{command}")
|
format!("👤 {last_name} {first_name} {middle_name}\nСкачать все книги архивом: {command}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ impl FormatTitle for Sequence {
|
|||||||
|
|
||||||
let command = (DownloadArchiveCommand::Sequence { id: *id }).to_string();
|
let command = (DownloadArchiveCommand::Sequence { id: *id }).to_string();
|
||||||
|
|
||||||
format!("📚 {name}\n{command}")
|
format!("📚 {name}\nСкачать все книги архивом: {command}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -215,3 +215,30 @@ pub async fn get_uploaded_books(
|
|||||||
|
|
||||||
_make_request("/api/v1/books/", params).await
|
_make_request("/api/v1/books/", params).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn _get_author_books_available_types(
|
||||||
|
id: u32,
|
||||||
|
allowed_langs: Vec<String>,
|
||||||
|
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let params = get_allowed_langs_params(allowed_langs);
|
||||||
|
|
||||||
|
_make_request(format!("/api/v1/authors/{id}/available_types").as_str(), params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn _get_translator_books_available_types(
|
||||||
|
id: u32,
|
||||||
|
allowed_langs: Vec<String>,
|
||||||
|
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let params = get_allowed_langs_params(allowed_langs);
|
||||||
|
|
||||||
|
_make_request(format!("/api/v1/translators/{id}/available_types").as_str(), params).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn _get_sequence_books_available_types(
|
||||||
|
id: u32,
|
||||||
|
allowed_langs: Vec<String>
|
||||||
|
) -> Result<Vec<String>, Box<dyn std::error::Error + Send + Sync>> {
|
||||||
|
let params = get_allowed_langs_params(allowed_langs);
|
||||||
|
|
||||||
|
_make_request(format!("/api/v1/sequences/{id}/available_types").as_str(), params).await
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user