From c6ffa62423659cf7d82802b9e0343f149c780de3 Mon Sep 17 00:00:00 2001 From: A Farzat Date: Sun, 1 Mar 2026 22:05:20 +0300 Subject: Add authentication using cookies --- src/main.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 998cc1f..33c8d82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ +mod http_client; + +use anyhow::Result; use clap::Parser; +use http_client::build_authenticated_client; /// Download and generate an EPUB from Safari Books Online. #[derive(Parser, Debug)] @@ -7,22 +11,37 @@ struct Args { /// The Book digits ID that you want to download. #[arg(required = true)] bookid: String, - + /// Path to the cookies.json file. + #[arg(long, default_value = "cookies.json")] + cookies: String, /// Do not delete the log file on success. #[arg(long = "preserve-log")] preserve_log: bool, } -fn main() { +#[tokio::main] +async fn main() -> Result<()> { // Parse the command line arguments let args = Args::parse(); println!("Welcome to SafariBooks Rust Port!"); println!("Target Book ID: {}", args.bookid); - if args.preserve_log { - println!("Logs will be preserved"); + // Initialise the HTTP client. + println!("Loading cookies and initialising the HTTP client..."); + let client = build_authenticated_client(&args.cookies)?; + + // Quick test request to verify authentication. + let profile_url = "https://learning.oreilly.com/profile/"; + let response = client.get(profile_url).send().await?; + if response.status().is_success() { + println!("Successfully authenticated!"); + } else { + println!( + "Authentication might have failed. Status code: {}", + response.status() + ); } - // TODO: Proceed to load cookies and setup the HTTP client... + Ok(()) } -- cgit v1.2.3-70-g09d2