proper error handling for mediawiki edits
This commit is contained in:
parent
7023e9445f
commit
81d7282e8a
|
@ -1,5 +1,6 @@
|
||||||
use std::cell::OnceCell;
|
use std::cell::OnceCell;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::f64::consts::E;
|
||||||
|
|
||||||
use chrono::{Datelike, NaiveDate, Utc};
|
use chrono::{Datelike, NaiveDate, Utc};
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
@ -75,13 +76,13 @@ impl std::fmt::Debug for Mediawiki {
|
||||||
pub enum ValidRequestTypes {
|
pub enum ValidRequestTypes {
|
||||||
Get,
|
Get,
|
||||||
Post,
|
Post,
|
||||||
PostForEditing
|
PostForEditing,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum ValidPageEdits {
|
pub enum ValidPageEdits {
|
||||||
WithPotentiallyOverriding,
|
WithPotentiallyOverriding,
|
||||||
WithoutOverriding,
|
WithoutOverriding,
|
||||||
ModifyPlenumPageAfterwards_WithoutOverriding
|
ModifyPlenumPageAfterwards_WithoutOverriding,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mediawiki {
|
impl Mediawiki {
|
||||||
|
@ -223,7 +224,6 @@ impl Mediawiki {
|
||||||
resp
|
resp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Creates a completely new wiki page with page_content and page_title as inputs
|
/// Creates a completely new wiki page with page_content and page_title as inputs
|
||||||
pub fn new_wiki_page(
|
pub fn new_wiki_page(
|
||||||
&self, page_title: &str, page_content: &str, update_main_page: ValidPageEdits,
|
&self, page_title: &str, page_content: &str, update_main_page: ValidPageEdits,
|
||||||
|
@ -243,7 +243,7 @@ impl Mediawiki {
|
||||||
}
|
}
|
||||||
|
|
||||||
let url = format!("{}/api.php", self.server_url);
|
let url = format!("{}/api.php", self.server_url);
|
||||||
|
|
||||||
let params: Box<[(&str, &str)]> = match update_main_page {
|
let params: Box<[(&str, &str)]> = match update_main_page {
|
||||||
ValidPageEdits::WithPotentiallyOverriding => {
|
ValidPageEdits::WithPotentiallyOverriding => {
|
||||||
// This means we *EDIT* the *Main Page* and do not prevent overwriting
|
// This means we *EDIT* the *Main Page* and do not prevent overwriting
|
||||||
|
@ -253,9 +253,9 @@ impl Mediawiki {
|
||||||
("title", page_title),
|
("title", page_title),
|
||||||
("text", page_content),
|
("text", page_content),
|
||||||
("token", self.csrf_token.get().unwrap()),
|
("token", self.csrf_token.get().unwrap()),
|
||||||
("bot", "true")
|
("bot", "true"),
|
||||||
])
|
])
|
||||||
}
|
},
|
||||||
ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding => {
|
ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding => {
|
||||||
// This means we *CREATE* a *new Page* and always prevent overwriting
|
// This means we *CREATE* a *new Page* and always prevent overwriting
|
||||||
Box::from([
|
Box::from([
|
||||||
|
@ -267,7 +267,7 @@ impl Mediawiki {
|
||||||
("createonly", "true"), // Prevent overwriting existing pages
|
("createonly", "true"), // Prevent overwriting existing pages
|
||||||
("bot", "true"),
|
("bot", "true"),
|
||||||
])
|
])
|
||||||
}
|
},
|
||||||
ValidPageEdits::WithoutOverriding => {
|
ValidPageEdits::WithoutOverriding => {
|
||||||
// This means we *CREATE* a *new Page* and always prevent overwriting
|
// This means we *CREATE* a *new Page* and always prevent overwriting
|
||||||
Box::from([
|
Box::from([
|
||||||
|
@ -279,7 +279,7 @@ impl Mediawiki {
|
||||||
("createonly", "true"), // Prevent overwriting existing pages
|
("createonly", "true"), // Prevent overwriting existing pages
|
||||||
("bot", "true"),
|
("bot", "true"),
|
||||||
])
|
])
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
verboseln!("Current page title: {page_title}");
|
verboseln!("Current page title: {page_title}");
|
||||||
|
@ -294,11 +294,19 @@ impl Mediawiki {
|
||||||
let response_result = serde_json::from_str::<serde_json::Value>(&request_result);
|
let response_result = serde_json::from_str::<serde_json::Value>(&request_result);
|
||||||
let response = response_result.unwrap_or_else(|e| {
|
let response = response_result.unwrap_or_else(|e| {
|
||||||
print!("Error while creating new wiki page:\n{}", e.to_string().cyan());
|
print!("Error while creating new wiki page:\n{}", e.to_string().cyan());
|
||||||
return serde_json::from_str("\"(error)\"").unwrap()
|
return serde_json::from_str("\"(error)\"").unwrap();
|
||||||
});
|
});
|
||||||
verboseln!("pos2");
|
verboseln!("pos2");
|
||||||
// Check if the page creation was successful
|
// Check if the page creation was successful
|
||||||
if let Some(edit) = response.get("edit") {
|
if let Some(error) = response.get("error") {
|
||||||
|
if let Some(info) = error.get("info") {
|
||||||
|
if info == "The page you tried to create has been created already." {
|
||||||
|
verboseln!("The page you tried to create has been created already. Continuing...")
|
||||||
|
} else {
|
||||||
|
println!("There was an error while editing the wiki: {}", info.to_string().yellow())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if let Some(edit) = response.get("edit") {
|
||||||
verboseln!("pos3");
|
verboseln!("pos3");
|
||||||
if edit.get("result").and_then(|r| r.as_str()) == Some("Success") {
|
if edit.get("result").and_then(|r| r.as_str()) == Some("Success") {
|
||||||
verboseln!("Successfully created wiki page: {}", page_title);
|
verboseln!("Successfully created wiki page: {}", page_title);
|
||||||
|
@ -318,9 +326,9 @@ impl Mediawiki {
|
||||||
verboseln!("updating main page...");
|
verboseln!("updating main page...");
|
||||||
self.update_plenum_page(page_title)?
|
self.update_plenum_page(page_title)?
|
||||||
},
|
},
|
||||||
_ => ()
|
_ => (),
|
||||||
};
|
};
|
||||||
return Ok(page_title.to_string())
|
return Ok(page_title.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is responsible for updating the main plenum page:
|
/// This function is responsible for updating the main plenum page:
|
||||||
|
@ -339,7 +347,6 @@ impl Mediawiki {
|
||||||
|
|
||||||
// check first if the script has been run before and if the link already exists
|
// check first if the script has been run before and if the link already exists
|
||||||
if !page_content.contains(&new_page_title_to_link_to) {
|
if !page_content.contains(&new_page_title_to_link_to) {
|
||||||
|
|
||||||
// check if the current year heading pattern exists
|
// check if the current year heading pattern exists
|
||||||
if !page_content.contains(&year_heading_pattern) {
|
if !page_content.contains(&year_heading_pattern) {
|
||||||
// If not, add a new year heading pattern
|
// If not, add a new year heading pattern
|
||||||
|
@ -375,7 +382,11 @@ impl Mediawiki {
|
||||||
verboseln!("{}", "The bot appears to have been run before and a duplicate link to the new plenum pad was avoided.".yellow())
|
verboseln!("{}", "The bot appears to have been run before and a duplicate link to the new plenum pad was avoided.".yellow())
|
||||||
}
|
}
|
||||||
// refresh page
|
// refresh page
|
||||||
self.new_wiki_page(&self.plenum_main_page_name, &page_content, ValidPageEdits::WithPotentiallyOverriding)?;
|
self.new_wiki_page(
|
||||||
|
&self.plenum_main_page_name,
|
||||||
|
&page_content,
|
||||||
|
ValidPageEdits::WithPotentiallyOverriding,
|
||||||
|
)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
/// This function downloads and returns the contents of a wiki page when given the page's title (e.g. `page_title = Plenum/13._August_2024`)
|
/// This function downloads and returns the contents of a wiki page when given the page's title (e.g. `page_title = Plenum/13._August_2024`)
|
||||||
|
@ -512,7 +523,11 @@ pub fn pad_ins_wiki(
|
||||||
let page_title = create_page_title(date);
|
let page_title = create_page_title(date);
|
||||||
let full_page_title = format!("{}/{}", wiki.plenum_main_page_name, page_title);
|
let full_page_title = format!("{}/{}", wiki.plenum_main_page_name, page_title);
|
||||||
|
|
||||||
wiki.new_wiki_page(&full_page_title, &pad_converted, ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding)?;
|
wiki.new_wiki_page(
|
||||||
|
&full_page_title,
|
||||||
|
&pad_converted,
|
||||||
|
ValidPageEdits::ModifyPlenumPageAfterwards_WithoutOverriding,
|
||||||
|
)?;
|
||||||
|
|
||||||
verboseln!("Finished successfully with wiki");
|
verboseln!("Finished successfully with wiki");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Reference in a new issue