Files
dancing-teacher-info/src/old_style.rs
2025-07-21 22:38:48 +02:00

91 lines
2.7 KiB
Rust

use mwbot::{
parsoid::{self, map::IndexMap, Wikicode, WikinodeIterator}, Bot, SaveOptions
};
use crate::list_teacher_pages;
fn extract_number_from_url(url: &str) -> Option<u32> {
// Split the URL into parts using '/'
let parts: Vec<&str> = url.split('/').collect();
// Iterate over the parts to find the one that ends with ".html"
for part in parts {
if part.ends_with(".html") {
// Remove the ".html" suffix
let number_str = part.trim_end_matches(".html");
// Parse the remaining string into an integer
return number_str.parse().ok();
}
}
None
}
async fn convert_teachers(bot: &Bot) {
let mut v = list_teacher_pages(bot);
while let Some(page) = v.recv().await {
let p = page.unwrap();
println!(
"- {} [{}]",
p.as_title().dbkey(),
crate::old_style::get_wsdc_id(&p)
.await
.map(|x| x.to_string())
.unwrap_or("Unknown".to_string())
);
let Some(os) = convert_old_style(bot, &p).await else {
continue;
};
p.save(
os,
&SaveOptions::summary("Converted teacher profile to template usage")
.mark_as_bot(true)
.mark_as_minor(false),
)
.await
.unwrap();
}
}
pub async fn convert_old_style(bot: &Bot, page: &mwbot::Page) -> Option<Wikicode> {
if !is_old_style(page).await {
eprintln!("Not old style: {}", page.title());
return None;
}
let mut params = IndexMap::new();
if let Some(id) = get_wsdc_id(page).await {
params.insert("wsdc_id".to_owned(), id.to_string());
}
if let Some(description) = get_description(page).await {
params.insert("description".to_owned(), description);
}
let t = parsoid::Template::new("Template:Teacher", &params).unwrap();
let result = Wikicode::new("");
result.append(&t);
Some(result)
}
pub async fn is_old_style(page: &mwbot::Page) -> bool {
let x = page.html().await.unwrap().into_mutable();
!x.filter_templates()
.unwrap()
.iter()
.any(|x| x.name() == "Template:Teacher")
}
pub async fn get_wsdc_id(page: &mwbot::Page) -> Option<u32> {
let x = page.html().await.unwrap().into_mutable();
for w in &x.filter_external_links() {
if let Some(id) = extract_number_from_url(&w.target()) {
return Some(id);
}
}
None
}
pub async fn get_description(page: &mwbot::Page) -> Option<String> {
let x = page.html().await.unwrap().into_mutable();
Some(x.iter_sections().first()?.first_child()?.text_contents())
}