murmeldin
e4444b8118
- mediawiki.rs: - new make_request function for easier post- and get-requests - new new_wiki_page function that creates new wiki pages - working csrf-Token-function - fixed read_txt_file function: Now properly working - pad_ins_wiki-function is now the main trigger for all the actions that should be done on day after plenum - hedgedoc.rs + mediawiki.rs: Better error handling for common offline edge case - main: ansi art moved in function, Timer and eta for time prediction during development - still to do in mediawiki.rs: Date logic for pad generation and function for modifying the main Plenum Page
294 lines
11 KiB
Rust
294 lines
11 KiB
Rust
use std::error::Error;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
|
|
use colored::Colorize;
|
|
use pandoc::{PandocError, PandocOutput};
|
|
use reqwest::blocking::Client;
|
|
use serde::Deserialize;
|
|
|
|
use crate::config_spec::{CfgField, CfgGroup};
|
|
|
|
pub const CONFIG: CfgGroup<'static> = CfgGroup {
|
|
name: "wiki",
|
|
description: "API Settings for Mediawiki",
|
|
fields: &[
|
|
CfgField::Default {
|
|
key: "server-url",
|
|
default: "https://wiki.berlin.ccc.de",
|
|
description: "Server running the wiki.",
|
|
},
|
|
CfgField::Default {
|
|
key: "http-user",
|
|
default: "cccb-wiki",
|
|
description: "HTTP basic auth user name.",
|
|
},
|
|
CfgField::Password {
|
|
key: "http-password",
|
|
description: "HTTP basic auth password."
|
|
},
|
|
CfgField::Default {
|
|
key: "api-user",
|
|
default: "PlenumBot@PlenumBot-PW2",
|
|
description: "API Username associated with the bot account used for edits.",
|
|
},
|
|
CfgField::Password {
|
|
key: "api-secret",
|
|
description: "API secret / \"password\" used for authenticating as the bot.",
|
|
},
|
|
CfgField::Default {
|
|
key: "eta",
|
|
default: "no ETA, program never ran",
|
|
description: "ETA message for estimating time the program takes"
|
|
}
|
|
],
|
|
};
|
|
|
|
pub struct Mediawiki {
|
|
server_url: String,
|
|
http_user: String,
|
|
http_password: String,
|
|
api_user: String,
|
|
api_secret: String,
|
|
is_dry_run: bool,
|
|
login_token: String,
|
|
csrf_token: String,
|
|
client: Client,
|
|
}
|
|
|
|
impl std::fmt::Debug for Mediawiki {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("Mediawiki")
|
|
.field("server_url", &self.server_url)
|
|
.field("http_user", &self.http_user)
|
|
.field("http_password", &"*****")
|
|
.field("is_dry_run", &self.is_dry_run)
|
|
.field("client", &self.client)
|
|
.finish()
|
|
}
|
|
}
|
|
|
|
pub enum RequestType {
|
|
Get,
|
|
Post,
|
|
PostForEditing
|
|
}
|
|
|
|
impl Mediawiki {
|
|
pub fn new(
|
|
server_url: &str, http_auth_user: &str, http_auth_password: &str, api_user: &str, api_secret: &str, is_dry_run: bool,
|
|
) -> Self {
|
|
Self {
|
|
server_url: server_url.to_string(),
|
|
http_user: http_auth_user.to_string(),
|
|
http_password: http_auth_password.to_string(),
|
|
api_user: api_user.to_string(),
|
|
api_secret: api_secret.to_string(),
|
|
is_dry_run,
|
|
login_token: String::new(),
|
|
csrf_token: String::new(),
|
|
client: Client::builder().cookie_store(true).build().unwrap(),
|
|
}
|
|
}
|
|
pub fn get_login_token(&self) -> Result<String, Box<dyn Error>> {
|
|
let url =
|
|
format!("{}/api.php?", self.server_url);
|
|
let params: Box<[(&str, &str)]> = Box::from( [
|
|
("format", "json"),
|
|
("meta", "tokens"),
|
|
("type", "login"),
|
|
("action", "query")
|
|
]);
|
|
let resp = self.make_request(url, params, RequestType::Get).unwrap();
|
|
let response_deserialized: QueryResponseLogin = serde_json::from_str(&resp)?;
|
|
Ok(response_deserialized.query.tokens.logintoken)
|
|
}
|
|
pub fn login (&self) -> Result<String, Box<dyn Error>> {
|
|
let url = format!("{}/api.php?", self.server_url);
|
|
let params: Box<[(&str, &str)]> = Box::from([
|
|
("lgname", self.api_user.as_str()),
|
|
("lgpassword", self.api_secret.as_str()),
|
|
("lgtoken", &self.login_token),
|
|
("action", "login")
|
|
]);
|
|
let resp: Result<String, Box<dyn Error>> = self.make_request(url, params, RequestType::Post);
|
|
Ok(resp.unwrap())
|
|
}
|
|
pub fn get_csrf_token(&self) -> Result<String, Box<dyn Error>> { // HAS TO BE FIXED
|
|
let url =
|
|
format!("{}/api.php?", self.server_url);
|
|
let params: Box<[(&str, &str)]> = Box::from([
|
|
("format", "json"),
|
|
("meta", "tokens"),
|
|
("formatversion", "2"),
|
|
("action", "query")
|
|
]);
|
|
let resp: Result<String, Box<dyn Error>> = self.make_request(url, params, RequestType::Get);
|
|
let resp = resp.unwrap();
|
|
let response_deserialized: QueryResponseCsrf = serde_json::from_str(&resp)?;
|
|
Ok(response_deserialized.query.tokens.csrftoken)
|
|
}
|
|
|
|
pub fn make_request(&self, url: String, params: Box<[(&str, &str)]>, request_type: RequestType) -> Result<String, Box<dyn Error>> {
|
|
let resp: Result<String, Box<dyn Error>> = match
|
|
match request_type {
|
|
RequestType::Get => {
|
|
self
|
|
.client
|
|
.get(url)
|
|
//.basic_auth(&self.http_user, Some(&self.http_password)) ZU TESTZWECKEN ENTFERNT
|
|
.query(¶ms)
|
|
.send()
|
|
}
|
|
RequestType::Post | RequestType::PostForEditing => {
|
|
self
|
|
.client
|
|
.post(url)
|
|
//.basic_auth(&self.http_user, Some(&self.http_password)) ZU TESTZWECKEN ENTFERNT
|
|
.form(¶ms)
|
|
.send()
|
|
}
|
|
}
|
|
{
|
|
Ok(response) => {
|
|
if response.status().is_success() {
|
|
match request_type {
|
|
RequestType::PostForEditing => Ok(response.text().unwrap()),
|
|
_ => Ok(response.text().unwrap())
|
|
}
|
|
}
|
|
else {
|
|
Err(format!("Failed to connect to wiki server: HTTP status code {}", response.status()).into())
|
|
}
|
|
}
|
|
Err(e) => {
|
|
if e.is_connect() {
|
|
Err(format!("Failed to connect to wiki server. Please check your internet connection or the server URL.\n(Error: {})", e).into())
|
|
} else {
|
|
Err(format!("An error occurred while sending the request to the wiki server: {}", e).into())
|
|
}
|
|
}
|
|
};
|
|
resp
|
|
}
|
|
/// Creates a completely new wiki page with page_content and page_title as inputs
|
|
pub fn new_wiki_page (&self, page_title: &str, page_content: &str) -> Result<String, Box<dyn Error>> {
|
|
// action=edit&format=json&title=Wikipedia:Sandbox&appendtext=Hello&token=sampleCsrfToken123+\
|
|
let url =
|
|
format!("{}/api.php?", self.server_url);
|
|
let params: Box<[(&str, &str)]> = Box::from([
|
|
("action", "edit"), // Create and edit pages.
|
|
("format", "json"),
|
|
("title", page_title), // Title of the page to edit. Cannot be used together with pageid.
|
|
("appendtext", page_content), // Add this text to the end of the page or section. Overrides text.
|
|
("token", self.csrf_token.as_str()), // A "csrf" token retrieved from action=query&meta=tokens
|
|
("bot", "true"), // Mark this edit as a bot edit.
|
|
]);
|
|
self.make_request(url, params, RequestType::Post)
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct QueryResponseLogin {
|
|
batchcomplete: String,
|
|
query: QueryTokensLogin,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct QueryTokensLogin {
|
|
tokens: TokensLogin,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct TokensLogin {
|
|
logintoken: String,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct QueryResponseCsrf {
|
|
batchcomplete: bool,
|
|
query: crate::mediawiki::QueryTokensCsrf,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct QueryTokensCsrf {
|
|
tokens: crate::mediawiki::TokensCsrf,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
struct TokensCsrf {
|
|
csrftoken: String,
|
|
}
|
|
|
|
pub fn pad_ins_wiki(old_pad_content: String, wiki: &mut Mediawiki) -> Result<(), Box<dyn Error>> {
|
|
// Login to Wiki and get required tokens for logging in and writing
|
|
let auth_result = wiki.get_login_token()?;
|
|
wiki.login_token.clone_from(&auth_result);
|
|
println!("AUTH Success");
|
|
let login_result = wiki.login()?;
|
|
println!("LOGIN Success");
|
|
let csrf_token = wiki.get_csrf_token();
|
|
let csrf_token = csrf_token.unwrap_or_else(|e| {
|
|
println!("Error while trying to get csrf: {:?}", e);
|
|
String::new()
|
|
});
|
|
println!("CSRF Success");
|
|
wiki.csrf_token.clone_from(&csrf_token);
|
|
println!("---AUTH RESULT:---\n{}\n---LOGIN RESULT:---\n{:?}\n---CSRF RESULT:---\n{}\n-----------", auth_result, login_result, csrf_token);
|
|
|
|
// Convert to mediawiki and make new page
|
|
let pad_converted = convert_md_to_mediawiki(old_pad_content);
|
|
println!("Das kommt ins Wiki: {}", pad_converted);
|
|
//wiki.new_wiki_page("Page Test 5", &pad_converted)?; JUST AN EXAMPLE
|
|
|
|
// Textdatei wieder einlesen
|
|
|
|
// Passwörter aus Datenbank lesen (ToBeDone)
|
|
/*
|
|
let plenum_bot_user = String::from("PlenumBot@PlenumBot-PW1");
|
|
let plenum_bot_pw = String::from("**OLD_API_PW_REMOVED**");
|
|
let login_token = login_to_mediawiki(plenum_bot_user.clone(), plenum_bot_pw.clone())
|
|
.expect("Fehler beim Einloggen!");
|
|
println!("plenum_bot_user: {plenum_bot_user}, plenum_bot_pw: {plenum_bot_pw}, login_token: {login_token}")
|
|
|
|
*/
|
|
Ok(())
|
|
}
|
|
|
|
/// Converts one file type into another using pandoc and saves the result as a txt file
|
|
fn pandoc_convert(
|
|
old_pad_content: String, output_filepath: &str, input_format: pandoc::InputFormat,
|
|
output_format: pandoc::OutputFormat,
|
|
) -> Result<PandocOutput, PandocError> {
|
|
//Convert Markdown into Mediawiki
|
|
// Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight
|
|
let mut p = pandoc::new();
|
|
p.set_input(pandoc::InputKind::Pipe(old_pad_content));
|
|
p.set_input_format(input_format, vec![]);
|
|
p.set_output(pandoc::OutputKind::File(output_filepath.parse().unwrap()));
|
|
p.set_output_format(output_format, vec![]);
|
|
p.execute()
|
|
}
|
|
|
|
|
|
/// Reads a text file from a specified path and returns it as a String
|
|
fn read_txt_file(filepath: &str) -> String {
|
|
let mut file = File::open(filepath)
|
|
.unwrap_or_else(|_| panic!("Fehler beim öffnen der Textdatei mit Pfad {filepath}!"));
|
|
let mut contents = String::new();
|
|
file.read_to_string(&mut contents)
|
|
.expect("Fehler beim auslesen der MediaWiki-Textdatei!");
|
|
contents
|
|
}
|
|
|
|
/// Takes a Sting in the Markdown format and returns a String in the mediawiki Format
|
|
fn convert_md_to_mediawiki(old_pad_content: String) -> String {
|
|
// TODO: use tempfile="3.3", make it a NamedTempFile::new()?;
|
|
// or alternatively use piped stdout to avoid files entirely
|
|
let output_filepath: &str = "./pandoc_mediawiki.txt";
|
|
pandoc_convert(old_pad_content, output_filepath, pandoc::InputFormat::Markdown, pandoc::OutputFormat::MediaWiki).expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei");
|
|
let temp = read_txt_file(output_filepath);
|
|
println!("TEMP: {}", temp.purple());
|
|
temp
|
|
}
|