replace zip command with zip lib, do languages properly i suppose, and other stuff i do not remember

This commit is contained in:
2025-06-03 00:25:00 +02:00
parent af002b0149
commit 6d4ffa209a
4 changed files with 824 additions and 179 deletions

View File

@@ -1,6 +1,11 @@
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use response_deserializer::{ChapterImages, SearchResult};
use std::fs::File;
use std::io::Write;
use std::path::Path;
use zip::write::{SimpleFileOptions, ZipWriter};
use zip::CompressionMethod;
mod response_deserializer;
mod select;
@@ -15,7 +20,7 @@ type Client = ClientWithMiddleware;
#[tokio::main]
async fn main() {
let config = util::args();
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(10);
let client = ClientBuilder::new(
reqwest::Client::builder()
.user_agent("manga-cli/version-0.1")
@@ -32,73 +37,26 @@ async fn main() {
];
let limit = config.result_limit;
let results = if let Some(query) = config.search {
match query {
util::ConfigSearch::Query(query) => search(client, &query, &filters, limit).await,
util::ConfigSearch::Id(_) => todo!(),
}
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 input = util::get_input("Enter search query: ");
search(client, &input, &filters, limit).await
};
let cover_ex = match config.cover_size {
util::CoverSize::Full => "",
util::CoverSize::W256 => ".256.jpg",
util::CoverSize::W512 => ".512.jpg",
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 mut entry_futures = Vec::new();
for result in results.data.iter() {
let mut entry = Entry::new(result.attributes.title.en.clone());
if let Some(year) = result.attributes.year {
entry.add_info("year", year);
}
let id = result.id.to_string();
entry.add_info("id", &id);
entry.add_info("status", result.attributes.status.to_string());
entry.add_info(
"content rating",
result.attributes.content_rating.to_string(),
);
if let Some(chapters) = result.attributes.last_chapter {
entry.add_info("chapters", chapters);
}
if let Some(volumes) = result.attributes.last_volume {
entry.add_info("volumes", volumes);
}
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
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);
entry.set_image(result);
entry
};
entry_futures.push(future);
}
}
let entries = futures::future::join_all(entry_futures).await;
let choice = match select::select(&entries) {
Ok(v) => v,
Err(e) => {
eprintln!("ERROR: Failed to select: {:?}", e);
std::process::exit(1);
}
};
let choice_id = &results.data[choice as usize].id;
let bonus = if let Some(bonus) = config.bonus {
bonus
} else {
@@ -111,33 +69,72 @@ async fn main() {
}
};
let selection_type = if let Some(selection_type) = config.selection_type {
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);
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 => loop {
let input = util::get_input("Choose chapters: ").replace(" ", "");
if let Some(selection) = util::choose_chapters(input.as_str()) {
break util::SelectionType::Chapter(selection);
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" => 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);
"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" => {
let input = util::get_input("Choose chapters: ").replace(" ", "");
if let Some(selection) = util::choose_chapters(input.as_str()) {
break 'outer util::SelectionType::Chapter(selection);
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);
}
}
}
}
_ => {
@@ -146,7 +143,7 @@ async fn main() {
}
}
};
let mut chapters = match get_chapters(client, choice_id).await {
let mut chapters = match get_chapters(client, &manga.id).await {
Ok(v) => v,
Err(e) => {
eprintln!("ERROR: {:#?}", e);
@@ -166,11 +163,9 @@ async fn main() {
let selected_chapters =
util::get_chapters_from_selection(util::Selection::new(selection_type, bonus), &chapters);
let mut chapters_image_data = Vec::new();
let mut i = 0;
for chapter in &selected_chapters {
// rate limits beware
let r = loop {
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()
@@ -195,40 +190,40 @@ async fn main() {
}
}
};
std::thread::sleep(std::time::Duration::from_millis(800));
println!("downloaded chapter json image data: {i}");
i += 1;
chapters_image_data.push(r);
}
let chapters = futures::future::join_all(
chapters_image_data
.iter()
.enumerate()
.map(|(i, image_data)| {
download_chapter_images(client, image_data, selected_chapters[i])
}),
)
.await;
for (i, chapter) in chapters.iter().enumerate() {
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.unwrap();
let path = if let Some(v) = selected_chapters[i].attributes.volume {
format!(
"images/{}/volume_{:0>3}/chapter{:0>3}_image_{:0>3}.png",
results.data[choice as usize].attributes.title.en, v, chapter_n, j
)
let chapter_n = selected_chapters[i].attributes.chapter;
let chapter_text = if let Some(n) = chapter_n {
n.to_string()
} else {
format!(
"images/{}/chapter{:0>3}_image_{:0>3}.png",
results.data[choice as usize].attributes.title.en, chapter_n, j
)
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 selected_chapters[i].attributes.volume.is_some()
if create_volumes
&& selected_chapters[i].attributes.volume.is_some()
&& !&path.parent().unwrap().exists()
{
if !path.parent().unwrap().parent().unwrap().exists() {
@@ -245,7 +240,6 @@ async fn main() {
}
}
let title = &results.data[choice as usize].attributes.title;
if create_volumes {
let mut volumes = Vec::new();
selected_chapters
@@ -257,14 +251,55 @@ async fn main() {
}
});
for volume in volumes {
let path = format!("images/{}/volume_{:0>3}", title.en, volume);
let file_name = format!("{} - Volume {:0>3}.cbz", title.en, volume);
std::process::Command::new("/usr/bin/zip")
.args(["-j", "-r", &file_name, &path])
.spawn()
.unwrap()
.wait()
.unwrap();
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();
}
}
}
@@ -289,7 +324,7 @@ async fn download_chapter_images(
.await
.unwrap();
println!(
"Downloaded volume: {:?}, chapter: {:?}, title: {}, [{}/{}]",
"\x1b[1A\x1b[2KDownloaded volume: {:?}, chapter: {:?}, title: {}, [{}/{}]",
chapter.attributes.volume,
chapter.attributes.chapter,
chapter.attributes.title,
@@ -356,3 +391,80 @@ async fn search(
.unwrap();
response_deserializer::deserializer(&json)
}
async fn select_manga_from_search(
client: &Client,
config: &util::Config,
results: &SearchResult,
) -> u16 {
let cover_ex = match config.cover_size {
util::CoverSize::Full => "",
util::CoverSize::W256 => ".256.jpg",
util::CoverSize::W512 => ".512.jpg",
};
let mut entry_futures = Vec::new();
for result in results.data.iter() {
let mut entry = Entry::new(result.attributes.title.en.clone());
if let Some(year) = result.attributes.year {
entry.add_info("year", year);
}
let id = result.id.to_string();
entry.add_info("id", &id);
entry.add_info("status", result.attributes.status.to_string());
entry.add_info(
"content rating",
result.attributes.content_rating.to_string(),
);
if let Some(chapters) = result.attributes.last_chapter {
entry.add_info("chapters", chapters);
}
if let Some(volumes) = result.attributes.last_volume {
entry.add_info("volumes", volumes);
}
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
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);
entry.set_image(result);
entry
};
entry_futures.push(future);
}
}
let entries = futures::future::join_all(entry_futures).await;
let choice = match select::select(&entries) {
Ok(v) => v,
Err(e) => {
eprintln!("ERROR: Failed to select: {:?}", e);
std::process::exit(1);
}
};
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)
}