Fix obtaining delete result status

This commit is contained in:
ItsDrike 2024-04-18 19:55:45 +02:00
parent 8ffa244ce1
commit b76274e361
Signed by: ItsDrike
GPG key ID: FA2745890B7048C0

View file

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