allow for more config and cli args

This commit is contained in:
2025-05-28 17:33:00 +02:00
parent 5905bd6b15
commit af002b0149
5 changed files with 302 additions and 107 deletions

View File

@@ -18,25 +18,36 @@ async fn main() {
let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3);
let client = ClientBuilder::new(
reqwest::Client::builder()
.user_agent("Chrome/127")
.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 results = if let Some(query) = config.search {
search(&client, &query, &filters, limit).await
match query {
util::ConfigSearch::Query(query) => search(client, &query, &filters, limit).await,
util::ConfigSearch::Id(_) => todo!(),
}
} else {
let input = util::get_input("Enter search query: ");
search(&client, &input, &filters, limit).await
search(client, &input, &filters, limit).await
};
let mut entries = vec![];
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 {
@@ -56,23 +67,30 @@ async fn main() {
entry.add_info("volumes", volumes);
}
if let Some(cover_data) = &result.relationships[2].attributes {
let data = client
.get(format!(
"https://uploads.mangadex.org/covers/{id}/{}",
// 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
))
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
);
let data = client
.get(&image_url)
.send()
.await
.unwrap()
.bytes()
.await
.unwrap();
let result = util::convert_to_sixel(&data);
entry.set_image(result)
entry.set_image(result);
entry
};
entry_futures.push(future);
}
entries.push(entry);
}
let entries = futures::future::join_all(entry_futures).await;
let choice = match select::select(&entries) {
Ok(v) => v,
Err(e) => {
@@ -92,7 +110,43 @@ async fn main() {
}
}
};
let mut chapters = match get_chapters(&client, choice_id).await {
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);
}
},
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);
}
},
"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);
}
}
_ => {
eprintln!("Invalid input");
}
}
}
};
let mut chapters = match get_chapters(client, choice_id).await {
Ok(v) => v,
Err(e) => {
eprintln!("ERROR: {:#?}", e);
@@ -106,67 +160,113 @@ async fn main() {
.partial_cmp(&b.attributes.chapter.unwrap_or(-1.))
.unwrap()
});
dbg!("got chapters");
let selection_type = loop {
match util::get_input("Select by volume or chapter? [v/c] : ").as_str() {
"v" | "volume" => break util::SelectionType::Volume(util::choose_volumes()),
"c" | "chapter" => break util::SelectionType::Chapter(util::choose_chapters()),
_ => {
eprintln!("Invalid input");
continue;
}
}
};
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 mut chapter_json_futures = vec![];
let mut chapters_image_data = Vec::new();
let mut i = 0;
for chapter in &selected_chapters {
let chapter_id = &chapter.id;
let client = &client;
let future = async move {
client
.get(format!("{BASE}/at-home/server/{}", chapter_id))
// rate limits beware
let r = loop {
let json = client
.get(format!("{BASE}/at-home/server/{}", chapter.id))
.send()
.await
.unwrap()
.text()
.await
.unwrap()
.unwrap();
let result = response_deserializer::deserialize_chapter_images(&json);
match result {
Ok(v) => break v,
Err(e) => {
if e.result != "error" {
panic!("brotha, api gone wrong (wild)");
}
for error in e.errors {
if error.status == 429 {
println!("you sent too many requests");
}
std::thread::sleep(std::time::Duration::from_millis(20000));
}
}
}
};
chapter_json_futures.push(future);
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_image_data: Vec<ChapterImages> = futures::future::join_all(chapter_json_futures)
.await
.iter()
.map(|m| response_deserializer::deserialize_chapter_images(m))
.collect();
let mut chapter_futures = vec![];
for (i, image_data) in chapters_image_data.iter().enumerate() {
chapter_futures.push(download_chapter_images(
&client,
image_data,
selected_chapters[i],
));
}
let chapters = futures::future::join_all(chapter_futures).await;
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() {
match chapter {
Ok(chapter) => {
for (j, image) in chapter.iter().enumerate() {
image
.save(format!("images/chapter{:0>3}_image_{:0>3}.png", i, j))
.unwrap();
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
)
} else {
format!(
"images/{}/chapter{:0>3}_image_{:0>3}.png",
results.data[choice as usize].attributes.title.en, chapter_n, j
)
};
let path = std::path::Path::new(&path);
if 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);
panic!("{e}");
}
}
}
let title = &results.data[choice as usize].attributes.title;
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 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();
}
}
}
async fn download_chapter_images(
@@ -208,7 +308,7 @@ async fn download_chapter_images(
}
async fn get_chapters(client: &Client, id: &Id) -> Result<Vec<Chapter>, reqwest_middleware::Error> {
let limit = 100;
let limit = 50;
let limit = limit.to_string();
let params = [("limit", limit.as_str()), ("translatedLanguage[]", "en")];
let url = format!("{BASE}/manga/{id}/feed");