diff --git a/src/main.rs b/src/main.rs index 5e46d89..8361d49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ -use crate::rentry::{build_client, delete_paste, CSRFData}; +use std::collections::VecDeque; + +use crate::rentry::{build_client, delete_paste, CSRFData, DeleteError}; mod rentry; @@ -6,23 +8,56 @@ const PASTE_NAME: &str = "Hyprland-controversy"; fn main() { let client = build_client(); - let csrf_data = CSRFData::get(&client, PASTE_NAME); + let mut csrf_data = CSRFData::get(&client, PASTE_NAME); - let edit_code = "f"; - match delete_paste(&client, &csrf_data, edit_code, PASTE_NAME) { - Ok(_) => println!("Paste deleted successfully"), - Err(e) => match e { - rentry::DeleteError::RequestFailure(msg) => eprintln!("Request error (non 2XX): {msg}"), - rentry::DeleteError::UnexpectedRedirect(msg) => eprintln!("Unexpected redirect: {msg}"), - rentry::DeleteError::FormError(msg) => { - if msg == "Invalid edit code." { - eprintln!("Invalid edit code") - } else { - eprintln!("Form error: {msg}") - } + let mut edit_codes = VecDeque::from([ + "qwack", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", + "v", "w", "x", "y", "z", "a", "b", "c", + ]); + + while let Some(edit_code) = edit_codes.pop_front() { + match delete_paste(&client, &csrf_data, edit_code, PASTE_NAME) { + Ok(_) => { + println!("Paste deleted successfully with edit code: {edit_code}"); + return; } - rentry::DeleteError::UnknownFailure(msg) => eprintln!("Unknown failure: {msg}"), - rentry::DeleteError::ReqwestError(e) => eprintln!("Request failed with: {e:?}"), - }, + Err(e) => match e { + DeleteError::FormFieldError(msg) if msg == "Invalid edit code." => { + println!("Invalid edit code {edit_code}"); + } + DeleteError::FormFieldError(msg) => { + eprintln!("Form error: {msg}"); + return; + } + DeleteError::FormError(msg) + if msg == "You have hit the limit, please wait a bit. e345" => + { + println!("Hit the limit, waiting..."); + csrf_data = CSRFData::get(&client, PASTE_NAME); + std::thread::sleep(std::time::Duration::from_secs(30)); + edit_codes.push_front(edit_code); + } + DeleteError::FormError(msg) => { + eprintln!("Form error: {msg}"); + return; + } + DeleteError::RequestFailure(msg) => { + eprintln!("Request error (non 2XX): {msg}"); + return; + } + DeleteError::UnexpectedRedirect(msg) => { + eprintln!("Unexpected redirect: {msg}"); + return; + } + DeleteError::UnknownFailure(msg) => { + eprintln!("Unknown failure: {msg}"); + return; + } + DeleteError::ReqwestError(e) => { + eprintln!("Request failed with: {e:?}"); + return; + } + }, + } } } diff --git a/src/rentry.rs b/src/rentry.rs index b73d6f7..64bcf5a 100644 --- a/src/rentry.rs +++ b/src/rentry.rs @@ -63,9 +63,10 @@ impl CSRFData { pub enum DeleteError { ReqwestError(reqwest::Error), UnexpectedRedirect(String), - FormError(String), + FormFieldError(String), RequestFailure(String), UnknownFailure(String), + FormError(String), } impl From for DeleteError { @@ -98,7 +99,13 @@ impl From for DeleteError { Selector::parse("fieldset > div.text-danger.messages > ul.errorlist > li").unwrap(); if let Some(element) = document.select(&selector).next() { let txt = element.text().collect::(); - return DeleteError::FormError(txt); + return DeleteError::FormFieldError(txt.trim().to_string()); + } + + let selector = Selector::parse("form > ul.messages > li.text-error").unwrap(); + if let Some(element) = document.select(&selector).next() { + let txt = element.text().collect::(); + return DeleteError::FormError(txt.trim().to_string()); } return DeleteError::UnknownFailure(format!("unknown failure (2XX): {raw_txt}"));