use builder pattern for getting manga

This commit is contained in:
2025-06-30 22:14:49 +02:00
parent 920d9ee25a
commit 1a29600d88
7 changed files with 970 additions and 532 deletions

View File

@@ -1,160 +1,59 @@
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use response_deserializer::{ChapterImages, SearchResult};
use error::{ChapterImageError, ChapterImagesError};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use client::{MangaClient, MangaClientBuilder, SearchFilter};
use response_deserializer::{Chapter, Id, Language, Manga};
use select::Entry;
use std::future::Future;
use std::pin::Pin;
use zip::write::{SimpleFileOptions, ZipWriter};
use zip::CompressionMethod;
use util::SelectionType;
mod client;
mod error;
mod response_deserializer;
mod select;
mod test;
mod util;
mod error;
mod client;
use response_deserializer::{Chapter, Id};
use select::Entry;
const BASE: &str = "https://api.mangadex.org";
type Client = ClientWithMiddleware;
const BASE: &str = "https://api.mangadex.dev";
#[tokio::main]
async fn main() {
let config = util::args();
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(10);
let client = ClientBuilder::new(
reqwest::Client::builder()
.user_agent("manga-cli/version-0.1")
.build()
.unwrap(),
)
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();
let client = &client;
let filters = [
// ("publicationDemographic[]", "seinen"),
// ("status[]", "completed"),
// ("contentRating[]", "suggestive"),
];
let limit = config.result_limit;
let manga_client =
MangaClientBuilder::new()
.bonus(config.bonus.unwrap_or_else(util::ask_bonus))
.cover(config.cover.unwrap_or_else(util::ask_cover))
.result_limit(config.result_limit.unwrap_or(5))
.cover_size(util::CoverSize::W512)
.selection_type(
config
.selection_type
.unwrap_or_else(util::ask_selection_type),
)
.selection_range(
config
.selection_range
.unwrap_or_else(util::ask_selection_range),
)
.search(config.search.unwrap_or_else(|| {
util::ConfigSearch::Query(util::get_input("Enter search query: "))
}))
.search_filter(SearchFilter::default())
.language(Language::default())
.build();
let search_result = manga_client.get_manga().await;
let mut choice = 0;
let results = if let Some(util::ConfigSearch::Id(set_id)) = config.search {
let id_query_result: response_deserializer::IdQueryResult =
id_query_get_info(client, &set_id).await;
vec![id_query_result.data]
} else {
let results = if let Some(ref query) = config.search {
match query {
util::ConfigSearch::Query(query) => search(client, query, &filters, limit).await,
_ => unreachable!(),
}
} else {
let input = util::get_input("Enter search query: ");
search(client, &input, &filters, limit).await
};
choice = select_manga_from_search(client, &config, &results).await;
results.data
};
let manga = &results[choice as usize];
let bonus = if let Some(bonus) = config.bonus {
bonus
} else {
loop {
match util::get_input("Read bonus chapters? [y/n] : ").as_str() {
"y" | "yes" => break true,
"n" | "no" => break false,
_ => continue,
}
}
};
let selection_type = if let Some(selection_type) = config.selection_type {
if let Some(selection_range) = config.selection_range {
match selection_type {
util::ConfigSelectionType::Volume => {
if let Some(selection) = util::choose_volumes(selection_range.as_str()) {
util::SelectionType::Volume(selection)
} else {
std::process::exit(1)
}
}
util::ConfigSelectionType::Chapter => {
if let Some(selection) = util::choose_chapters(selection_range.as_str()) {
util::SelectionType::Chapter(selection)
} else {
std::process::exit(1)
}
}
}
} else {
match selection_type {
util::ConfigSelectionType::Volume => loop {
let input = util::get_input("Choose volumes: ").replace(" ", "");
if let Some(selection) = util::choose_volumes(input.as_str()) {
break util::SelectionType::Volume(selection);
}
},
util::ConfigSelectionType::Chapter => loop {
let input = util::get_input("Choose chapters: ").replace(" ", "");
if let Some(selection) = util::choose_chapters(input.as_str()) {
break util::SelectionType::Chapter(selection);
}
},
}
}
} else {
'outer: loop {
match util::get_input("Select by volume or chapter? [v/c] : ").as_str() {
"v" | "volume" => {
if let Some(ref selection_range) = config.selection_range {
if let Some(selection) = util::choose_volumes(selection_range.as_str()) {
break util::SelectionType::Volume(selection);
} else {
std::process::exit(1);
}
} else {
loop {
let input = util::get_input("Choose volumes: ").replace(" ", "");
if let Some(selection) = util::choose_volumes(input.as_str()) {
break 'outer util::SelectionType::Volume(selection);
}
}
}
}
"c" | "chapter" => {
if let Some(ref selection_range) = config.selection_range {
if let Some(selection) = util::choose_chapters(selection_range.as_str()) {
break util::SelectionType::Chapter(selection);
} else {
std::process::exit(1);
}
} else {
loop {
let input = util::get_input("Choose chapters: ").replace(" ", "");
if let Some(selection) = util::choose_chapters(input.as_str()) {
break 'outer util::SelectionType::Chapter(selection);
}
}
}
}
_ => {
eprintln!("Invalid input");
}
}
}
};
let mut chapters = match get_chapters(client, &manga.id).await {
if search_result.len() > 1 {
choice = select_manga_from_search(&manga_client, &search_result).await;
}
let manga = &search_result[choice as usize];
let mut chapters = match manga_client.get_chapters(&manga.id).await {
Ok(v) => v,
Err(e) => {
eprintln!("ERROR: {e:#?}");
std::process::exit(1);
}
};
println!("Downloading {} chapters", chapters.len());
chapters.sort_by(|a, b| {
a.attributes
.chapter
@@ -162,263 +61,35 @@ async fn main() {
.partial_cmp(&b.attributes.chapter.unwrap_or(-1.))
.unwrap()
});
dbg!("got chapters");
let create_volumes = matches!(
manga_client.selection.selection_type,
SelectionType::Volume(_)
);
let selected_chapters = util::get_chapters_from_selection(manga_client.selection, &chapters);
let create_volumes = matches!(selection_type, util::SelectionType::Volume(_));
let selected_chapters =
util::get_chapters_from_selection(util::Selection::new(selection_type, bonus), &chapters);
let title = &manga.attributes.title.en;
let title = &manga.attributes.title;
for (i, chapter) in selected_chapters.iter().enumerate() {
let chapter_image_data = loop {
let json = client
.get(format!("{BASE}/at-home/server/{}", chapter.id))
.send()
.await
.unwrap()
.text()
.await
.unwrap();
let result = response_deserializer::deserialize_chapter_images(&json);
match result {
Ok(v) => break v,
Err(e) => match e {
ChapterImagesError::Image(i) => match i {
ChapterImageError::Result(s) => {
eprintln!("chapter image error: {s}");
std::process::exit(1);
}
},
ChapterImagesError::Content(e) => {
if e.result != "error" {
panic!("brotha, api gone wrong (wild)");
}
for error in e.errors {
if error.status == 429 {
eprintln!("you sent too many requests");
}
std::thread::sleep(std::time::Duration::from_millis(20000));
}
}
},
}
};
println!(
"\x1b[1A\x1b[2Kdownloaded chapter json image data: [{i}/{}]",
selected_chapters.len()
);
let chapter =
download_chapter_images(client, &chapter_image_data, selected_chapters[i]).await;
match chapter {
Ok(chapter) => {
for (j, image) in chapter.iter().enumerate() {
let chapter_n = selected_chapters[i].attributes.chapter;
let chapter_text = if let Some(n) = chapter_n {
n.to_string()
} else {
String::from("_none")
};
let chapter_path = format!(
"images/{}/chapter_{:0>3}_image_{:0>3}.png",
title.en, chapter_text, j
);
let path = if let Some(v) = selected_chapters[i].attributes.volume {
if create_volumes {
format!(
"images/{}/volume_{:0>3}/chapter_{:0>3}_image_{:0>3}.png",
title.en, v, chapter_text, j
)
} else {
chapter_path
}
} else {
chapter_path
};
let path = std::path::Path::new(&path);
if create_volumes
&& selected_chapters[i].attributes.volume.is_some()
&& !&path.parent().unwrap().exists()
{
if !path.parent().unwrap().parent().unwrap().exists() {
std::fs::create_dir(path.parent().unwrap().parent().unwrap()).unwrap();
}
std::fs::create_dir(path.parent().unwrap()).unwrap();
}
image.save(path).unwrap();
}
}
Err(e) => {
panic!("{e}");
}
}
}
util::download_the_stuff(
&manga_client.client,
&selected_chapters,
title,
create_volumes,
)
.await;
if create_volumes {
let mut volumes = Vec::new();
selected_chapters
.iter()
.filter_map(|m| m.attributes.volume)
.for_each(|v| {
if !volumes.contains(&v) {
volumes.push(v);
}
});
for volume in volumes {
let image_paths = format!("images/{}/volume_{:0>3}", title.en, volume);
let image_paths = Path::new(&image_paths);
let zip_file_path = format!("{} - Volume {:0>3}.cbz", title.en, volume);
let zip_file_path = Path::new(&zip_file_path);
let zip_file = File::create(zip_file_path).unwrap();
let mut zip = ZipWriter::new(zip_file);
let options =
SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
for entry in std::fs::read_dir(image_paths).unwrap() {
let entry = entry.unwrap();
zip.start_file(entry.file_name().to_str().unwrap(), options)
.unwrap();
let buffer = std::fs::read(entry.path()).unwrap();
zip.write_all(&buffer).unwrap();
}
zip.finish().unwrap();
}
} else {
for chapter in selected_chapters {
let chapter = chapter.attributes.chapter.unwrap();
let image_paths = format!("images/{}", title.en);
let image_paths = Path::new(&image_paths);
let zip_file_path = format!("{} - Chapter {:0>3}.cbz", title.en, chapter);
let zip_file_path = Path::new(&zip_file_path);
let zip_file = File::create(zip_file_path).unwrap();
let mut zip = ZipWriter::new(zip_file);
let options =
SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
for entry in std::fs::read_dir(image_paths).unwrap() {
let entry = entry.unwrap();
if entry.path().is_dir() {
continue;
}
zip.start_file(entry.file_name().to_str().unwrap(), options)
.unwrap();
let buffer = std::fs::read(entry.path()).unwrap();
zip.write_all(&buffer).unwrap();
}
zip.finish().unwrap();
}
}
util::create_volumes_or_chapters(&selected_chapters, create_volumes, title);
}
async fn download_chapter_images(
client: &Client,
image_data: &ChapterImages,
chapter: &Chapter,
) -> Result<Vec<image::DynamicImage>, reqwest_middleware::Error> {
let mut data_futures = vec![];
for (i, file_name) in image_data.chapter.data.iter().enumerate() {
let base_url: &str = image_data.base_url.as_str();
let hash: &str = image_data.chapter.hash.as_str();
let future = async move {
let data = client
.clone()
.get(format!("{base_url}/data/{hash}/{file_name}"))
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
println!(
"\x1b[1A\x1b[2KDownloaded volume: {:?}, chapter: {:?}, title: {}, [{}/{}]",
chapter.attributes.volume,
chapter.attributes.chapter,
chapter.attributes.title,
i,
chapter.attributes.pages
);
data
};
data_futures.push(future);
}
Ok(futures::future::join_all(data_futures)
.await
.iter()
.map(|m| image::load_from_memory(m).unwrap())
.collect())
}
async fn get_chapters(client: &Client, id: &Id) -> Result<Vec<Chapter>, reqwest_middleware::Error> {
let limit = 50;
let limit = limit.to_string();
let params = [("limit", limit.as_str()), ("translatedLanguage[]", "en")];
let url = format!("{BASE}/manga/{id}/feed");
let json = client.get(url).query(&params).send().await?.text().await?;
let mut result = response_deserializer::deserialize_chapter_feed(&json).unwrap();
let mut total_chapters_received = result.limit;
while total_chapters_received < result.total {
let offset = total_chapters_received.to_string();
let params = [
("limit", limit.as_str()),
("translatedLanguage[]", "en"),
("offset", offset.as_str()),
];
let url = format!("{BASE}/manga/{id}/feed");
let json = client.get(url).query(&params).send().await?.text().await?;
let mut new_result = response_deserializer::deserialize_chapter_feed(&json).unwrap();
result.data.append(&mut new_result.data);
total_chapters_received += result.limit;
}
assert_eq!(result.data.len(), result.total as usize);
Ok(result.data)
}
async fn search(
client: &Client,
query: &str,
filters: &[(&str, &str)],
limit: u32,
) -> SearchResult {
let params = [
("title", query),
("limit", &limit.to_string()),
("includes[]", "cover_art"),
];
let json = client
.get(format!("{BASE}/manga"))
.query(&params)
.query(filters)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
response_deserializer::deserializer(&json).unwrap()
}
async fn select_manga_from_search(
client: &Client,
config: &util::Config,
results: &SearchResult,
) -> u16 {
let cover_ex = match config.cover_size {
async fn select_manga_from_search(client: &MangaClient, results: &[Manga]) -> u16 {
let cover_ex = match client.cover_size {
util::CoverSize::Full => "",
util::CoverSize::W256 => ".256.jpg",
util::CoverSize::W512 => ".512.jpg",
};
use std::future::Future;
let mut entry_futures: Vec<Pin<Box<dyn Future<Output = Entry>>>> = Vec::new();
for result in results.data.iter() {
assert!(!results.is_empty());
for result in results.iter() {
let mut entry = Entry::new(result.attributes.title.en.clone());
if let Some(year) = result.attributes.year {
entry.add_info("year", year);
@@ -439,32 +110,38 @@ async fn select_manga_from_search(
if let Some(cover_data) = &result.relationships[2].attributes {
// The lib used for converting to sixel is abysmally slow for larger images, this
// should be in a future to allow for multithreaded work
if config.cover != Some(false) {
let future = async move {
let image_url = format!(
"https://uploads.mangadex.org/covers/{id}/{}{cover_ex}",
&cover_data.file_name
);
let data = client
.get(&image_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
// Cover data should only be present if used
assert!(client.cover);
let future = async move {
let image_url = format!(
"https://uploads.mangadex.org/covers/{id}/{}{cover_ex}",
&cover_data.file_name
);
let data = client
.client
.get(&image_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
entry.set_image(result);
entry
};
entry_futures.push(Box::pin(future));
} else {
entry_futures.push(Box::pin(async move { entry }));
}
entry.set_image(result);
entry
};
entry_futures.push(Box::pin(future));
} else {
entry_futures.push(Box::pin(async move { entry }));
}
}
let entries = futures::future::join_all(entry_futures).await;
let entries: Vec<Entry> = futures::future::join_all(entry_futures).await;
assert!(!entries.is_empty());
if entries.len() == 1 {
return 0;
}
let choice = match select::select(&entries) {
Ok(v) => v,
Err(e) => {
@@ -474,15 +151,3 @@ async fn select_manga_from_search(
};
choice
}
async fn id_query_get_info(client: &Client, id: &Id) -> response_deserializer::IdQueryResult {
let json = client
.get(format!("{BASE}/manga/{id}"))
.send()
.await
.unwrap()
.text()
.await
.unwrap();
response_deserializer::deserialize_id_query(&json).unwrap()
}