Worked on WSDC Rank parsing

This commit is contained in:
Lukas Wölfer
2025-07-21 22:38:48 +02:00
parent 8640aee958
commit 8bae1fdfbb
4 changed files with 464 additions and 37 deletions

View File

@@ -1,10 +1,13 @@
use mwbot::{
Bot,
generators::{Generator, SortDirection, categories::CategoryMemberSort},
generators::{
Generator, SortDirection, categories::CategoryMemberSort, querypage::QueryPage,
search::Search,
},
};
use std::{error::Error, path::Path};
use crate::old_style::get_description;
use reqwest::RequestBuilder;
use serde::{Deserialize, Deserializer};
use std::{collections::HashMap, error::Error, path::Path};
mod old_style;
@@ -19,20 +22,115 @@ fn list_teacher_pages(bot: &Bot) -> tokio::sync::mpsc::Receiver<Result<mwbot::Pa
pages.generate(bot)
}
async fn print_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(),
old_style::get_wsdc_id(bot, &p)
.await
.map(|x| x.to_string())
.unwrap_or("Unknown".to_string())
);
get_description(bot, &p).await;
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
enum DanceRole {
Leader,
Follower,
}
#[derive(serde::Deserialize, Debug, PartialEq, Eq)]
enum DanceRank {
Novice,
Intermediate,
Advanced,
#[serde(rename = "All-Stars")]
AllStars,
Champions,
}
#[derive(serde::Deserialize, Debug)]
enum OptionalDanceRank {
#[serde(rename = "N/A")]
NotAvailable,
#[serde(untagged)]
Rank(DanceRank),
}
#[derive(serde::Deserialize, Debug)]
enum OptionalDanceLevel {
#[serde(rename = "N/A")]
NotAvailable,
#[serde(untagged)]
Level(u16),
}
#[derive(serde::Deserialize, Debug)]
struct DanceInfo {
dancer_first: String,
dancer_last: String,
short_dominate_role: DanceRole,
short_non_dominate_role: DanceRole,
dominate_role_highest_level_points: u16,
dominate_role_highest_level: DanceRank,
non_dominate_role_highest_level_points: OptionalDanceLevel,
non_dominate_role_highest_level: OptionalDanceRank,
}
async fn fetch_wsdc_info(id: u32) {
let client = reqwest::ClientBuilder::new().build().unwrap();
let mut params = HashMap::new();
params.insert("q", id.to_string());
let response = client
.request(
reqwest::Method::POST,
"https://points.worldsdc.com/lookup2020/find",
)
.form(&params)
.send()
.await
.unwrap();
let x: DanceInfo = response.json().await.unwrap();
dbg!(x);
}
async fn wanted_ids(bot: &Bot) -> Vec<u32> {
let mut gene = QueryPage::new("Wantedpages").generate(bot);
let mut result = vec![];
while let Some(x) = gene.recv().await {
let p = match x {
Ok(p) => p,
Err(e) => {
eprintln!("Could not get search result: {e}");
continue;
}
};
if let Ok(n) = parse_wsdc_page_name(p.title()) {
result.push(n);
}
}
result
}
fn parse_wsdc_page_name(name: &str) -> Result<u32, ()> {
if !name.starts_with("WSDC/") {
eprintln!("{name} is a wrong match");
return Err(());
}
match name.trim_start_matches("WSDC/").parse::<u32>() {
Ok(n) => Ok(n),
Err(e) => {
eprintln!("Page {name} does not fit: {e}");
Err(())
}
}
}
async fn index_wsdc_ids(bot: &Bot) -> Vec<u32> {
let mut gene = Search::new("WSDC/").generate(bot);
let mut result = vec![];
while let Some(x) = gene.recv().await {
let p = match x {
Ok(p) => p,
Err(e) => {
eprintln!("Could not get search result: {e}");
continue;
}
};
if let Ok(n) = parse_wsdc_page_name(&p.title()) {
result.push(n);
}
}
result
}
#[tokio::main]
@@ -44,8 +142,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
return Ok(());
}
};
print_teachers(&bot).await;
// dbg!(index_wsdc_ids(&bot).await);
// dbg!(wanted_ids(&bot).await);
// fetch_wsdc_info(1234).await;
// fetch_wsdc_info(1010).await;
fetch_wsdc_info(18080).await;
Ok(())
// // Monitor changes on these pages

View File

@@ -1,4 +1,8 @@
use mwbot::{Bot, parsoid::WikinodeIterator as _};
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 '/'
@@ -17,7 +21,60 @@ fn extract_number_from_url(url: &str) -> Option<u32> {
None
}
pub async fn get_wsdc_id(bot: &Bot, page: &mwbot::Page) -> Option<u32> {
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()) {
@@ -27,21 +84,7 @@ pub async fn get_wsdc_id(bot: &Bot, page: &mwbot::Page) -> Option<u32> {
None
}
pub async fn get_description(bot: &Bot, page: &mwbot::Page) -> Option<String> {
pub async fn get_description(page: &mwbot::Page) -> Option<String> {
let x = page.html().await.unwrap().into_mutable();
for w in &x.iter_sections() {
dbg!(w);
let Some(h) = w.heading() else {
println!("No heading");
continue;
};
dbg!(&h);
let Some(t) = h.as_text() else {
println!("No text");
continue;
};
dbg!(t.borrow());
}
None
Some(x.iter_sections().first()?.first_child()?.text_contents())
}