From e1d70f424ed118e5c1c46c83ee7f2ac07c9718f8 Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Thu, 18 Apr 2024 21:27:37 +0200 Subject: [PATCH] Don't focus on deletion, just find the edit code --- src/main.rs | 20 ++++++++++---------- src/rentry.rs | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a48c03..eb0cb91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use crate::rentry::{build_client, delete_paste, CSRFData, DeleteError}; +use crate::rentry::{build_client, edit_paste, CSRFData, EditError}; mod rentry; @@ -53,20 +53,20 @@ fn main() { let mut edit_codes = EditCodeGenerator::default(); while let Some(edit_code) = edit_codes.next() { - match delete_paste(&client, &csrf_data, &edit_code, PASTE_NAME) { + match edit_paste(&client, &csrf_data, &edit_code, PASTE_NAME) { Ok(_) => { println!("Paste deleted successfully with edit code: {edit_code}"); return; } Err(e) => match e { - DeleteError::FormFieldError(msg) if msg == "Invalid edit code." => { + EditError::FormFieldError(msg) if msg == "Invalid edit code." => { println!("Invalid edit code {edit_code}"); } - DeleteError::FormFieldError(msg) => { + EditError::FormFieldError(msg) => { eprintln!("Form error: {msg}"); return; } - DeleteError::FormError(msg) + EditError::FormError(msg) if msg == "You have hit the limit, please wait a bit. e345" => { println!("Hit the limit, waiting..."); @@ -74,23 +74,23 @@ fn main() { std::thread::sleep(std::time::Duration::from_secs(30)); edit_codes.retry_last().unwrap(); } - DeleteError::FormError(msg) => { + EditError::FormError(msg) => { eprintln!("Form error: {msg}"); return; } - DeleteError::RequestFailure(msg) => { + EditError::RequestFailure(msg) => { eprintln!("Request error (non 2XX): {msg}"); return; } - DeleteError::UnexpectedRedirect(msg) => { + EditError::UnexpectedRedirect(msg) => { eprintln!("Unexpected redirect: {msg}"); return; } - DeleteError::UnknownFailure(msg) => { + EditError::UnknownFailure(msg) => { eprintln!("Unknown failure: {msg}"); return; } - DeleteError::ReqwestError(e) => { + EditError::ReqwestError(e) => { eprintln!("Request failed with: {e:?}"); return; } diff --git a/src/rentry.rs b/src/rentry.rs index 64bcf5a..58ee54c 100644 --- a/src/rentry.rs +++ b/src/rentry.rs @@ -60,7 +60,7 @@ impl CSRFData { } #[derive(Debug)] -pub enum DeleteError { +pub enum EditError { ReqwestError(reqwest::Error), UnexpectedRedirect(String), FormFieldError(String), @@ -69,13 +69,13 @@ pub enum DeleteError { FormError(String), } -impl From for DeleteError { +impl From for EditError { fn from(err: reqwest::Error) -> Self { - DeleteError::ReqwestError(err) + EditError::ReqwestError(err) } } -impl From for DeleteError { +impl From for EditError { fn from(res: Response) -> Self { let status = res.status(); let headers = res.headers().clone(); @@ -99,16 +99,16 @@ 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::FormFieldError(txt.trim().to_string()); + return EditError::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 EditError::FormError(txt.trim().to_string()); } - return DeleteError::UnknownFailure(format!("unknown failure (2XX): {raw_txt}")); + return EditError::UnknownFailure(format!("unknown failure (2XX): {raw_txt}")); } let selector = Selector::parse("span").unwrap(); @@ -120,16 +120,16 @@ impl From for DeleteError { .collect::(); let err_txt = err_txt.trim().to_string(); - DeleteError::RequestFailure(err_txt) + EditError::RequestFailure(err_txt) } } -pub fn delete_paste( +pub fn edit_paste( client: &Client, csrf_data: &CSRFData, edit_code: &str, paste_name: &str, -) -> Result<(), DeleteError> { +) -> Result<(), EditError> { let url = BASE_URL.to_string() + paste_name + "/edit"; let res = client @@ -145,7 +145,6 @@ pub fn delete_paste( ("new_edit_code", ""), ("new_url", ""), ("text", ""), - ("delete", "delete"), ]) .send()?; @@ -156,7 +155,7 @@ pub fn delete_paste( } } - Err(DeleteError::from(res)) + Err(EditError::from(res)) } pub fn build_client() -> Client {