mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2026-03-04 07:30:49 +01:00
Rewrite to rust
This commit is contained in:
49
src/services/book_library/mod.rs
Normal file
49
src/services/book_library/mod.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
pub mod types;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::config::CONFIG;
|
||||
|
||||
async fn _make_request<T>(
|
||||
url: &str,
|
||||
params: Vec<(&str, String)>,
|
||||
) -> Result<T, Box<dyn std::error::Error + Send + Sync>>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let formated_url = format!("{}{}", CONFIG.library_url, url);
|
||||
|
||||
let response = client
|
||||
.get(formated_url)
|
||||
.query(¶ms)
|
||||
.header("Authorization", CONFIG.library_api_key.clone())
|
||||
.send()
|
||||
.await;
|
||||
|
||||
let response = match response {
|
||||
Ok(v) => v,
|
||||
Err(err) => return Err(Box::new(err)),
|
||||
};
|
||||
|
||||
let response = match response.error_for_status() {
|
||||
Ok(v) => v,
|
||||
Err(err) => return Err(Box::new(err)),
|
||||
};
|
||||
|
||||
match response.json::<T>().await {
|
||||
Ok(v) => Ok(v),
|
||||
Err(err) => Err(Box::new(err)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_sources() -> Result<types::Source, Box<dyn std::error::Error + Send + Sync>> {
|
||||
_make_request("/api/v1/sources", vec![]).await
|
||||
}
|
||||
|
||||
pub async fn get_book(
|
||||
book_id: i32,
|
||||
) -> Result<types::BookWithRemote, Box<dyn std::error::Error + Send + Sync>> {
|
||||
_make_request(format!("/api/v1/books/{book_id}").as_str(), vec![]).await
|
||||
}
|
||||
108
src/services/book_library/types.rs
Normal file
108
src/services/book_library/types.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Source {
|
||||
// id: u32,
|
||||
// name: String
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct BookAuthor {
|
||||
pub id: u32,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub middle_name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct Book {
|
||||
pub id: u32,
|
||||
pub title: String,
|
||||
pub lang: String,
|
||||
pub file_type: String,
|
||||
pub uploaded: String,
|
||||
pub authors: Vec<BookAuthor>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug, Clone)]
|
||||
pub struct BookWithRemote {
|
||||
pub id: u32,
|
||||
pub remote_id: u32,
|
||||
pub title: String,
|
||||
pub lang: String,
|
||||
pub file_type: String,
|
||||
pub uploaded: String,
|
||||
pub authors: Vec<BookAuthor>,
|
||||
}
|
||||
|
||||
impl BookWithRemote {
|
||||
pub fn from_book(book: Book, remote_id: u32) -> Self {
|
||||
Self {
|
||||
id: book.id,
|
||||
remote_id,
|
||||
title: book.title,
|
||||
lang: book.lang,
|
||||
file_type: book.file_type,
|
||||
uploaded: book.uploaded,
|
||||
authors: book.authors
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl BookAuthor {
|
||||
pub fn get_caption(self) -> String {
|
||||
let mut parts: Vec<String> = vec![];
|
||||
|
||||
if !self.last_name.is_empty() {
|
||||
parts.push(self.last_name);
|
||||
}
|
||||
|
||||
if !self.first_name.is_empty() {
|
||||
parts.push(self.first_name);
|
||||
}
|
||||
|
||||
if !self.middle_name.is_empty() {
|
||||
parts.push(self.middle_name);
|
||||
}
|
||||
|
||||
let joined_parts = parts.join(" ");
|
||||
|
||||
format!("👤 {joined_parts}")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl BookWithRemote {
|
||||
pub fn get_caption(self) -> String {
|
||||
let BookWithRemote {
|
||||
title,
|
||||
authors,
|
||||
..
|
||||
} = self;
|
||||
|
||||
let caption_title = format!("📖 {title}");
|
||||
|
||||
let author_captions: Vec<String> = authors
|
||||
.into_iter()
|
||||
.map(|a| a.get_caption())
|
||||
.collect();
|
||||
|
||||
let mut author_parts: Vec<String> = vec![];
|
||||
let mut author_parts_len = 3;
|
||||
|
||||
for author_caption in author_captions {
|
||||
if caption_title.len() + author_parts_len + author_caption.len() + 1 <= 1024 {
|
||||
author_parts_len = author_caption.len() + 1;
|
||||
author_parts.push(author_caption);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
let caption_authors = author_parts.join("\n");
|
||||
|
||||
format!("{caption_title}\n\n{caption_authors}")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user