add mediawiki struct & move stuff onto it
This commit is contained in:
parent
a454914b1f
commit
3be7898cdb
12
src/main.rs
12
src/main.rs
|
@ -35,7 +35,6 @@ use chrono::{Datelike, Local, NaiveDate, Weekday};
|
|||
use clap::{Arg, Command};
|
||||
use colored::Colorize;
|
||||
use regex::Regex;
|
||||
use reqwest::blocking::Client;
|
||||
use std::borrow::Cow;
|
||||
use std::env;
|
||||
use std::error::Error;
|
||||
|
@ -52,6 +51,7 @@ use email::{Email, SimpleEmail};
|
|||
mod hedgedoc;
|
||||
use hedgedoc::HedgeDoc;
|
||||
mod mediawiki;
|
||||
use mediawiki::Mediawiki;
|
||||
|
||||
const FALLBACK_TEMPLATE: &str = variables_and_settings::FALLBACK_TEMPLATE;
|
||||
|
||||
|
@ -163,8 +163,12 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
&config["email-to"],
|
||||
config.get("email-in-reply-to").ok(),
|
||||
);
|
||||
println!("[START]\n{}", "Aktueller Zustand der DB:".bold());
|
||||
config.dump_redacting(&["email-password", "wiki-http-password", "wiki-api-secret", "matrix-password"]).ok();
|
||||
let wiki = Mediawiki::new(
|
||||
&config["wiki-server-url"],
|
||||
&config["wiki-http-user"],
|
||||
&config["wiki-http-password"],
|
||||
is_dry_run(),
|
||||
);
|
||||
|
||||
// Dienstage diesen Monat
|
||||
let all_tuesdays: Vec<NaiveDate> = get_all_weekdays(0, Weekday::Tue);
|
||||
|
@ -226,7 +230,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
let top_anzahl: i32 = 0; // Muss noch gecodet werden
|
||||
let yesterday_was_plenum = true; // Das ist nur zu Testzwecken, kommt noch weg
|
||||
|
||||
let auth_result = mediawiki::get_login_token(&Client::new(), &config.get("wiki-http-user").expect("HTTP User not found DB!"), &config.get("wiki-http-password").expect("HTTP Password not found DB!"))?;
|
||||
let auth_result = wiki.get_login_token()?;
|
||||
println!("---AUTH RESULT:---\n{}\n-----------", auth_result);
|
||||
// TEMPORÄR ENDE
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup {
|
|||
fields: &[
|
||||
CfgField::Default {
|
||||
key: "server-url",
|
||||
default: "https://wiki.berlin.ccc.de/",
|
||||
default: "https://wiki.berlin.ccc.de",
|
||||
description: "Server running the wiki.",
|
||||
},
|
||||
CfgField::Default {
|
||||
|
@ -35,6 +35,42 @@ pub const CONFIG: CfgGroup<'static> = CfgGroup {
|
|||
],
|
||||
};
|
||||
|
||||
pub struct Mediawiki {
|
||||
server_url: String,
|
||||
http_user: String,
|
||||
http_password: String,
|
||||
is_dry_run: bool,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl Mediawiki {
|
||||
pub fn new(
|
||||
server_url: &str, http_auth_user: &str, http_auth_password: &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(),
|
||||
is_dry_run,
|
||||
client: Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Authenticate with username and password on http basic auth
|
||||
pub fn get_login_token(&self) -> Result<String, Box<dyn Error>> {
|
||||
let url =
|
||||
format!("{}/api.php?action=query&meta=tokens&type=login&format=json", self.server_url);
|
||||
let resp = self
|
||||
.client
|
||||
.get(url)
|
||||
.basic_auth(&self.http_user, Some(&self.http_password))
|
||||
.send()?
|
||||
.text()?;
|
||||
let response_deserialized: QueryResponse = serde_json::from_str(&resp)?;
|
||||
Ok(response_deserialized.query.tokens.logintoken)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct QueryResponse {
|
||||
batchcomplete: String,
|
||||
|
@ -55,7 +91,6 @@ pub fn pad_ins_wiki(old_pad_content: String) {
|
|||
convert_md_to_mediawiki(old_pad_content);
|
||||
// Textdatei wieder einlesen
|
||||
|
||||
|
||||
// Passwörter aus Datenbank lesen (ToBeDone)
|
||||
/*
|
||||
let plenum_bot_user = String::from("PlenumBot@PlenumBot-PW1");
|
||||
|
@ -80,28 +115,20 @@ fn pandoc_convert(old_pad_content: String, output_filepath: &str, input_format:
|
|||
}
|
||||
|
||||
/// 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).expect(&*format!("Fehler beim öffnen der Textdatei mit Pfad {filepath}!"));
|
||||
fn read_txt_file(filepath: &str) -> String {
|
||||
let mut file = File::open(filepath)
|
||||
.expect(&*format!("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!").to_string()
|
||||
file.read_to_string(&mut contents)
|
||||
.expect("Fehler beim auslesen der MediaWiki-Textdatei!")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
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");
|
||||
read_txt_file(output_filepath)
|
||||
}
|
||||
|
||||
/// Authenticate with username and password on http basic auth
|
||||
pub fn get_login_token(client: &Client, http_user: &str, http_pass: &String) -> Result<String, Box<dyn Error>> {
|
||||
let resp = client
|
||||
.get("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json")
|
||||
.basic_auth(http_user, Some(http_pass))
|
||||
.send()?
|
||||
.text()?;
|
||||
let response_deserialized: QueryResponse = serde_json::from_str(&resp)?;
|
||||
Ok(response_deserialized.query.tokens.logintoken)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue