mod cli;
use crate::cli::{Commands, DoArgs, WPEpubCli};
use anyhow::Context;
use anyhow::Result;
use clap::Parser;
use ik_mini::InkittClient;
use ik_mini_epub::{download_story_to_folder, login};
use reqwest::Client;
use tracing::info;
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() -> Result<()> {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("ik_epub_cli=info,warn"));
tracing_subscriber::fmt().with_env_filter(filter).init();
let http_client = Client::builder()
.cookie_store(true)
.user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36")
.build()?;
let cli = WPEpubCli::parse();
info!("CLI arguments parsed successfully");
match cli.command {
Commands::Do(args) => handle_do_command(&http_client, args)
.await
.context("Failed to process story")?,
}
Ok(())
}
async fn handle_do_command(client: &Client, args: DoArgs) -> Result<()> {
info!(id = args.id, "Handling 'do' command");
info!(user = %args.email, "Authenticating & Creating InkittClient...");
let ik_client = InkittClient::new();
login(&ik_client, &args.email, &args.password)
.await
.context("Login failed. Check your credentials.")?;
info!("Login successful. Starting download...");
let output_dir = args
.output_path
.unwrap_or_else(|| std::env::current_dir().expect("Failed to get current directory"));
let story_response = download_story_to_folder(
&ik_client,
client,
args.id,
args.include_images,
args.semaphore as usize,
&output_dir,
)
.await?;
let story_title = story_response.metadata.title;
info!(title = %story_title, "Story processing completed successfully");
Ok(())
}