This commit is contained in:
2023-08-09 03:32:08 +02:00
parent d702a0f95d
commit 9a03978553
3 changed files with 96 additions and 12 deletions

View File

@@ -4,6 +4,8 @@ use serde::de::DeserializeOwned;
use crate::config::CONFIG; use crate::config::CONFIG;
use self::types::{BaseBook, Page};
async fn _make_request<T>( async fn _make_request<T>(
url: &str, url: &str,
params: Vec<(&str, String)>, params: Vec<(&str, String)>,
@@ -53,16 +55,13 @@ pub async fn get_books(
page_size: u32, page_size: u32,
uploaded_gte: String, uploaded_gte: String,
uploaded_lte: String, uploaded_lte: String,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { ) -> Result<Page<BaseBook>, Box<dyn std::error::Error + Send + Sync>> {
let _params: Vec<(&str, String)> = vec![ let params: Vec<(&str, String)> = vec![
("page", page.to_string()), ("page", page.to_string()),
("page_size", page_size.to_string()), ("size", page_size.to_string()),
("uploaded_gte", uploaded_gte), ("uploaded_gte", uploaded_gte),
("uploaded_lte", uploaded_lte) ("uploaded_lte", uploaded_lte)
]; ];
// TODO _make_request(format!("/api/v1/books/base/").as_str(), params).await
// _make_request(format!("/api/v1/books/").as_str(), params).await;
Ok(())
} }

View File

@@ -36,6 +36,12 @@ pub struct BookWithRemote {
pub authors: Vec<BookAuthor>, pub authors: Vec<BookAuthor>,
} }
#[derive(Deserialize, Debug, Clone)]
pub struct BaseBook {
pub id: i32,
pub available_types: Vec<String>
}
impl BookWithRemote { impl BookWithRemote {
pub fn from_book(book: Book, remote_id: u32) -> Self { pub fn from_book(book: Book, remote_id: u32) -> Self {
Self { Self {
@@ -106,3 +112,15 @@ impl BookWithRemote {
format!("{caption_title}\n\n{caption_authors}") format!("{caption_title}\n\n{caption_authors}")
} }
} }
#[derive(Deserialize, Debug, Clone)]
pub struct Page<T> {
pub items: Vec<T>,
pub total: u32,
pub page: u32,
pub size: u32,
pub pages: u32,
}

View File

@@ -7,7 +7,7 @@ use tracing::log;
use crate::{prisma::cached_file, views::Database}; use crate::{prisma::cached_file, views::Database};
use self::{download_utils::DownloadResult, telegram_files::{download_from_telegram_files, UploadData, upload_to_telegram_files}, downloader::{get_filename, FilenameData, download_from_downloader}, book_library::get_book}; use self::{download_utils::DownloadResult, telegram_files::{download_from_telegram_files, UploadData, upload_to_telegram_files}, downloader::{get_filename, FilenameData, download_from_downloader}, book_library::{get_book, types::BaseBook, get_books}};
pub async fn get_cached_file_or_cache( pub async fn get_cached_file_or_cache(
@@ -132,8 +132,75 @@ pub async fn download_from_cache(
}) })
} }
pub async fn start_update_cache( pub async fn get_books_for_update() -> Result<Vec<BaseBook>, Box<dyn std::error::Error + Send + Sync>> {
_db: Database let mut result: Vec<BaseBook> = vec![];
) {
// TODO let page_size = 50;
let uploaded_gte = "".to_string();
let uploaded_lte = "".to_string();
let first_page = match get_books(
1,
page_size,
uploaded_gte.clone(),
uploaded_lte.clone()
).await {
Ok(v) => v,
Err(err) => return Err(err),
};
result.extend(first_page.items);
let mut current_page = 2;
let page_count = first_page.pages;
while current_page <= page_count {
let page = match get_books(current_page, page_size, uploaded_gte.clone(), uploaded_lte.clone()).await {
Ok(v) => v,
Err(err) => return Err(err),
};
result.extend(page.items);
current_page += 1;
};
Ok(result)
}
pub async fn start_update_cache(
db: Database
) {
let books = match get_books_for_update().await {
Ok(v) => v,
Err(err) => {
log::error!("{:?}", err);
return;
},
};
for book in books {
for available_type in book.available_types {
let cached_file = match db
.cached_file()
.find_unique(
cached_file::object_id_object_type(book.id, available_type.clone())
)
.exec()
.await {
Ok(v) => v,
Err(err) => {
log::error!("{:?}", err);
continue;
}
};
if cached_file.is_some() {
continue;
}
cache_file(book.id, available_type, db.clone()).await;
}
}
} }