Don't focus on deletion, just find the edit code
This commit is contained in:
parent
9f000cbdcb
commit
e1d70f424e
20
src/main.rs
20
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;
|
||||
}
|
||||
|
|
|
@ -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<reqwest::Error> for DeleteError {
|
||||
impl From<reqwest::Error> for EditError {
|
||||
fn from(err: reqwest::Error) -> Self {
|
||||
DeleteError::ReqwestError(err)
|
||||
EditError::ReqwestError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Response> for DeleteError {
|
||||
impl From<Response> for EditError {
|
||||
fn from(res: Response) -> Self {
|
||||
let status = res.status();
|
||||
let headers = res.headers().clone();
|
||||
|
@ -99,16 +99,16 @@ impl From<Response> 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::<String>();
|
||||
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::<String>();
|
||||
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<Response> for DeleteError {
|
|||
.collect::<String>();
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue