diff --git a/src/main.rs b/src/main.rs index c36fd19..1d50e94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -112,12 +112,12 @@ async fn main() -> Result<(), Box> { config.dump_redacting(&["email-password","wiki-password","matrix-password"]).ok(); // Dienstage diesen Monat - let all_tuesdays: Vec = get_tuesdays(0); + let all_tuesdays: Vec = get_all_weekdays(0, Weekday::Tue); let zweiter_dienstag: String = all_tuesdays[1].to_string(); // z.B. 2024-07-09 let vierter_dienstag: String = all_tuesdays[3].to_string(); // z.B. 2024-07-23 //Dienstage des nächsten Monats definieren - let all_tuesdays_next_month: Vec = get_tuesdays(1); + let all_tuesdays_next_month: Vec = get_all_weekdays(1, Weekday::Tue); let zweiter_dienstag_nächster_monat: String = all_tuesdays_next_month[1].to_string(); let vierter_dienstag_nächster_monat: String = all_tuesdays_next_month[3].to_string(); @@ -269,7 +269,7 @@ Und hier ist das Protokoll des letzten Plenums: if config.has_errors() { Err("There were errors.".into()) } else { Ok(()) } } -fn get_tuesdays(month_offset: i32) -> Vec{ +fn get_all_weekdays(month_offset: i32, week_day: Weekday) -> Vec{ let date = Local::now().date_naive(); let date = match month_offset.signum() { 0 => Some(date), @@ -281,7 +281,7 @@ fn get_tuesdays(month_offset: i32) -> Vec{ let mut current_date = NaiveDate::from_ymd_opt(date.year(), date.month(), 1); let mut dates = Vec::new(); while let Some(date) = current_date { - if date.month() == month && date.weekday() == Weekday::Tue { + if date.month() == month && date.weekday() == week_day { dates.push(date); } current_date = date.succ_opt(); @@ -407,57 +407,4 @@ fn try_to_remove_top_instructions (pad_content: String) -> String { let re_top_instructions: Regex = Regex::new(r"()").unwrap(); let result: Cow = re_top_instructions.replace_all(&pad_content, "---"); result.to_string() // Wenn es nicht geklappt hat, wird einfach das Pad mit dem Kommentar zurückgegeben -} - -fn pad_ins_wiki(old_pad_content: String) { - convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content); - - // 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) - 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}") -} - -fn convert_markdown_to_mediawiki_and_save_as_txt (old_pad_content: String) { - //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(pandoc::InputFormat::Markdown, vec![]); - p.set_output(pandoc::OutputKind::File("./pandoc-output.txt".parse().unwrap())); - p.set_output_format(pandoc::OutputFormat::MediaWiki, vec![]); - p.execute().expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); -} - -fn login_to_mediawiki (plenum_bot_user: String, plenum_bot_pw: String) -> Result> { - //let mut map = HashMap::new(); - //map.insert("logintoken", "result"); - let username = "cccb-wiki"; - 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); - - */ - Ok(String::from("unimplemented")) } \ No newline at end of file diff --git a/src/mediawiki.rs b/src/mediawiki.rs index 754d791..17ba2d5 100644 --- a/src/mediawiki.rs +++ b/src/mediawiki.rs @@ -1,17 +1,9 @@ -use pandoc::{MarkdownExtension, Pandoc, PandocError, PandocOutput}; -use std::io::prelude::*; +use std::error::Error; use std::fs::File; +use std::io::Read; pub fn pad_ins_wiki(old_pad_content: String) { - //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(pandoc::InputFormat::Markdown, vec![]); - // p.set_output_format(Pandoc::OutputFormat::mediawiki, vec![MarkdownExtension::Smart]); - p.set_output(pandoc::OutputKind::File("./pandoc-output.txt".parse().unwrap())); - p.set_output_format(pandoc::OutputFormat::MediaWiki, vec![]); - p.execute().expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); + convert_markdown_to_mediawiki_and_save_as_txt(old_pad_content); // Textdatei wieder einlesen let mut file = File::open("pandoc-output.txt").expect("Fehler beim öffnen der MediaWiki-Textdatei!"); @@ -21,5 +13,44 @@ pub fn pad_ins_wiki(old_pad_content: String) { // 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}") +} +fn convert_markdown_to_mediawiki_and_save_as_txt (old_pad_content: String) { + //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(pandoc::InputFormat::Markdown, vec![]); + p.set_output(pandoc::OutputKind::File("./pandoc-output.txt".parse().unwrap())); + p.set_output_format(pandoc::OutputFormat::MediaWiki, vec![]); + p.execute().expect("Fehler beim Umwandeln des und speichern des Pads in eine mediawiki-Textdatei"); +} + +fn login_to_mediawiki (plenum_bot_user: String, plenum_bot_pw: String) -> Result> { + //let mut map = HashMap::new(); + //map.insert("logintoken", "result"); + let username = "cccb-wiki"; + 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); + + */ + Ok(String::from("unimplemented")) } \ No newline at end of file