mediawiki function cleanup, login token working and to-do list in main

This commit is contained in:
murmeldin 2024-08-03 17:13:31 +02:00 committed by murmeldin
parent fbe8e4b85d
commit 427bd27968
4 changed files with 74 additions and 46 deletions

View file

@ -9,7 +9,7 @@ chrono = "0.4.38"
regex = "1.10.5" regex = "1.10.5"
futures = "0.3.30" futures = "0.3.30"
headers = "0.4.0" headers = "0.4.0"
reqwest = {version = "0.12.5", features = ["blocking"]} reqwest = { version = "0.12.5", default-features = false, features = ["blocking", "rustls-tls"] }
lettre = "0.11.7" lettre = "0.11.7"
rand = "0.9.0-alpha.1" rand = "0.9.0-alpha.1"
rusqlite = "0.31.0" rusqlite = "0.31.0"
@ -17,3 +17,5 @@ uuid = { version = "1.10.0", features = ["v4"] }
log = "0.4.22" log = "0.4.22"
clap = "4.5.11" clap = "4.5.11"
rpassword = "7.3.1" rpassword = "7.3.1"
serde = {version = "1.0.204", features = ["derive"]}
serde_json = "1.0.122"

View file

@ -18,18 +18,26 @@ Pad-ins-Wiki-und-versenden-Skript
Pad in MediaWiki-Format umwandeln Pad in MediaWiki-Format umwandeln
Neue Wiki-Seite erstellen und dort das umgewandelte Pad hochladen Neue Wiki-Seite erstellen und dort das umgewandelte Pad hochladen
*/ */
/* *TO-DO LIST*
main.rs
- [ ] Add logic for top_anzahl in main.rs:172
MediaWiki
- [ ] Add "get_csrf_token-function" for getting a write token to allow write operations in the wiki (murmeldin)
- [ ] Add "create_page" function for creating new pages in the wiki that is called on every day after plenum (murmeldin)
- [ ] Add "modify_plenum_main_page" function for creating new Links on the Plenum main page whenever the create_page function is being called (murmeldin)
// future improvements: future improvements:
// - search ADJ_TIMEYWIMEY to find places that need adjusting if the bot might run late - search ADJ_TIMEYWIMEY to find places that need adjusting if the bot might run late
// (that's an incomplete list, but tag things as you notice them…) (that's an incomplete list, but tag things as you notice them)
*/
// Import other .rs files as modules // Import other .rs files as modules
mod key_value; mod key_value;
use key_value::KeyValueStore as KV; use key_value::KeyValueStore as KV;
mod hedgedoc; mod hedgedoc;
use hedgedoc::HedgeDoc; use hedgedoc::HedgeDoc;
mod mediawiki; mod mediawiki;
mod config_check; mod config_check;
pub mod variables_and_settings; pub mod variables_and_settings;
@ -49,6 +57,7 @@ use uuid::Uuid;
use lettre::message::{header, SinglePart}; use lettre::message::{header, SinglePart};
use lettre::transport::smtp::authentication::Credentials; use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport}; use lettre::{Message, SmtpTransport, Transport};
use reqwest::blocking::Client;
const FALLBACK_TEMPLATE: &str = variables_and_settings::FALLBACK_TEMPLATE; const FALLBACK_TEMPLATE: &str = variables_and_settings::FALLBACK_TEMPLATE;
@ -165,9 +174,14 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut message_id: Option<String> = None; let mut message_id: Option<String> = None;
// let in_3_days_is_plenum = true; // let in_3_days_is_plenum = true;
//TEMPORÄR ANFANG: BEI PRODUCTION MUSS DAS HIER RAUS
let top_anzahl: i32 = 0; // Muss noch gecodet werden let top_anzahl: i32 = 0; // Muss noch gecodet werden
let yesterday_was_plenum = true; // Das ist nur zu Testzwecken, kommt noch weg 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("HTML User not found DB!"), &config.get("wiki-http-pass").expect("HTML Password not found DB!"))?;
println!("---AUTH RESULT:---\n{}\n-----------", auth_result);
// TEMPORÄR ENDE
let pad_content = hedgedoc.download(&current_pad_id).expect("Fehler beim Download des Pads!"); let pad_content = hedgedoc.download(&current_pad_id).expect("Fehler beim Download des Pads!");
let pad_content_without_top_instructions = try_to_remove_top_instructions(pad_content); let pad_content_without_top_instructions = try_to_remove_top_instructions(pad_content);
println!("Pad-content geladen!"); println!("Pad-content geladen!");

View file

@ -1,67 +1,79 @@
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use clap::builder::Str;
use pandoc::{PandocError, PandocOutput};
use reqwest; use reqwest;
use reqwest::blocking::Client;
use reqwest::tls;
use serde::Deserialize;
#[derive(Deserialize)]
struct QueryResponse {
batchcomplete: String,
query: QueryTokens,
}
#[derive(Deserialize)]
struct QueryTokens {
tokens: Tokens,
}
#[derive(Deserialize)]
struct Tokens {
logintoken: String,
}
pub fn pad_ins_wiki(old_pad_content: String) { pub fn pad_ins_wiki(old_pad_content: String) {
convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content); convert_md_to_mediawiki(old_pad_content);
// Textdatei wieder einlesen // Textdatei wieder einlesen
let mut file =
File::open("pandoc-output.txt").expect("Fehler beim öffnen der MediaWiki-Textdatei!");
let mut contents = String::new();
file.read_to_string(&mut contents).expect("Fehler beim auslesen der MediaWiki-Textdatei!");
// Passwörter aus Datenbank lesen (ToBeDone) // Passwörter aus Datenbank lesen (ToBeDone)
/*
let plenum_bot_user = String::from("PlenumBot@PlenumBot-PW1"); let plenum_bot_user = String::from("PlenumBot@PlenumBot-PW1");
let plenum_bot_pw = String::from("**OLD_API_PW_REMOVED**"); let plenum_bot_pw = String::from("**OLD_API_PW_REMOVED**");
let login_token = login_to_mediawiki(plenum_bot_user.clone(), plenum_bot_pw.clone()) let login_token = login_to_mediawiki(plenum_bot_user.clone(), plenum_bot_pw.clone())
.expect("Fehler beim Einloggen!"); .expect("Fehler beim Einloggen!");
println!("plenum_bot_user: {plenum_bot_user}, plenum_bot_pw: {plenum_bot_pw}, login_token: {login_token}") println!("plenum_bot_user: {plenum_bot_user}, plenum_bot_pw: {plenum_bot_pw}, login_token: {login_token}")
*/
} }
fn convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content: String) { /// 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 //Convert Markdown into Mediawiki
// Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight // Vanilla pandoc Befehl: pandoc --from markdown --to mediawiki --no-highlight
let mut p = pandoc::new(); let mut p = pandoc::new();
p.set_input(pandoc::InputKind::Pipe(old_pad_content)); p.set_input(pandoc::InputKind::Pipe(old_pad_content));
p.set_input_format(pandoc::InputFormat::Markdown, vec![]); p.set_input_format(input_format, vec![]);
p.set_output(pandoc::OutputKind::File("./pandoc-output.txt".parse().unwrap())); p.set_output(pandoc::OutputKind::File(output_filepath.parse().unwrap()));
p.set_output_format(pandoc::OutputFormat::MediaWiki, vec![]); p.set_output_format(output_format, vec![]);
p.execute() p.execute()
.expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei");
} }
fn login_to_mediawiki( /// Reads a text file from a specified path and returns it as a String
plenum_bot_user: String, plenum_bot_pw: String, fn read_txt_file (filepath: &str) -> String {
) -> Result<String, Box<dyn Error>> { let mut file =
//let mut map = HashMap::new(); File::open(filepath).expect(&*format!("Fehler beim öffnen der Textdatei mit Pfad {filepath}!"));
//map.insert("logintoken", "result"); let mut contents = String::new();
let username = "cccb-wiki"; file.read_to_string(&mut contents).expect("Fehler beim auslesen der MediaWiki-Textdatei!").to_string()
let password = "**OLD_PW_REMOVED**"; }
let auth_header_value = format!("{}:{}", username, password);
// let auth_header_value = format!("Basic {}", Engine::encode(&auth_value, ()));
/* NOCH NICHT GEFIXT
let client = reqwest::blocking::Client::new();
let resp = client
.get("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json")
.send()?
.text()?;
//let response = client
// .post("https://wiki.berlin.ccc.de/api.php?action=query&meta=tokens&type=login&format=json")
// .form(&[("Username", "cccb-wiki"), ("Password", "**OLD_PW_REMOVED**")])
// .send()
// .unwrap();
//.json(&map);
let html_source = resp.text()?;
//let login_token: String = map.get("logintoken").unwrap().to_string().clone();
println!("---HTML:---\n{}\n-----------", html_source);
*/ /// Takes a Sting in the Markdown format and returns a String in the mediawiki Format
Ok(String::from("unimplemented")) fn convert_md_to_mediawiki (old_pad_content: String) -> String {
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 /// Authenticate with username and password on http basic auth
fn 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)
} }