diff --git a/src/main.rs b/src/main.rs index c125e09..734586b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ -use reqwest::{blocking::Client, header::HeaderValue}; +use reqwest::{blocking::Client, blocking::ClientBuilder, header::HeaderValue, redirect::Policy}; use scraper::{Html, Selector}; const URL: &str = "https://rentry.co/Hyprland-controversy/edit"; enum DeleteResult { - Success(String), + Success, InvalidEditCode, - OtherFailure(String), + UnknownFailure(String), + RequestFailure(String), } #[derive(Debug)] @@ -70,18 +71,22 @@ fn attempt_delete(client: &Client, csrf_data: &CSRFData, edit_code: &str) -> Del .send() .unwrap(); + // Get these before obtaining text, as that consumes the response let status = res.status(); + let headers = res.headers().clone(); let raw_txt = res.text().unwrap(); let document = Html::parse_document(&raw_txt); - if status.is_success() { - let selector = Selector::parse("ul.messages > li.text-success").unwrap(); - if let Some(element) = document.select(&selector).next() { - let txt = element.text().collect::(); - return DeleteResult::Success(txt); + if status.is_redirection() { + let location = headers.get("Location").unwrap().to_str().unwrap(); + if location == "/" { + return DeleteResult::Success; } + return DeleteResult::RequestFailure("unexpected redirection: ".to_string() + location); + } + if status.is_success() { let selector = Selector::parse("fieldset > div.text-danger.messages > ul.errorlist > li").unwrap(); if let Some(element) = document.select(&selector).next() { @@ -89,10 +94,10 @@ fn attempt_delete(client: &Client, csrf_data: &CSRFData, edit_code: &str) -> Del if txt == "Invalid edit code." { return DeleteResult::InvalidEditCode; } - return DeleteResult::OtherFailure(txt); + return DeleteResult::UnknownFailure("form error: ".to_string() + &txt); } - return DeleteResult::OtherFailure("unknown failure".to_string()); + return DeleteResult::UnknownFailure(format!("unknown failure: {raw_txt}")); } let selector = Selector::parse("span").unwrap(); @@ -104,17 +109,21 @@ fn attempt_delete(client: &Client, csrf_data: &CSRFData, edit_code: &str) -> Del .collect::(); let err_txt = err_txt.trim().to_string(); - DeleteResult::OtherFailure(err_txt) + DeleteResult::RequestFailure(err_txt) } fn main() { - let client = Client::new(); + let client = ClientBuilder::new() + .redirect(Policy::none()) + .build() + .unwrap(); let csrf_data = get_csrf_data(&client); println!("Token: {csrf_data:?}"); - match attempt_delete(&client, &csrf_data, "") { - DeleteResult::Success(txt) => println!("Successfully deleted: {txt}!"), + match attempt_delete(&client, &csrf_data, "qwack") { + DeleteResult::Success => println!("Successfully deleted!"), DeleteResult::InvalidEditCode => println!("Failed to delete: Invalid edit code!"), - DeleteResult::OtherFailure(txt) => println!("Failed to delete: {txt}!"), + DeleteResult::RequestFailure(txt) => println!("Failed to delete (req): {txt}!"), + DeleteResult::UnknownFailure(txt) => println!("Failed to delete (unknown): {txt}!"), } }