Compare commits
5 Commits
html_scrap
...
v0.1.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7baff3a50c | ||
|
|
31293d1807 | ||
|
|
5414a1bb26 | ||
|
|
c45001cb6d | ||
|
|
5fae51248a |
2113
Cargo.lock
generated
2113
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "teachertracker-rs"
|
name = "teachertracker-rs"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors = ["Lukas Wölfer <coding@thasky.one>"]
|
authors = ["Lukas Wölfer <coding@thasky.one>"]
|
||||||
description = "A MediaWiki bot that updates score information of teachers"
|
description = "A MediaWiki bot that updates score information of teachers"
|
||||||
@@ -12,12 +12,15 @@ categories = ["web-programming", "api-bindings", "automation"]
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "0.4.41"
|
chrono = "0.4.41"
|
||||||
|
clap = { version = "4.5.54", features = ["derive"] }
|
||||||
futures = "0.3.31"
|
futures = "0.3.31"
|
||||||
# mwbot = { git = "https://gitlab.wikimedia.org/repos/mwbot-rs/mwbot.git", rev = "05cbb12188f18e2da710de158d89a9a4f1b42689", default-features = false, features = ["generators", "mwbot_derive"] }
|
# mwbot = { git = "https://gitlab.wikimedia.org/repos/mwbot-rs/mwbot.git", rev = "05cbb12188f18e2da710de158d89a9a4f1b42689", default-features = false, features = ["generators", "mwbot_derive"] }
|
||||||
mwbot = { version = "0.7.0", default-features = false, features = ["generators", "mwbot_derive"] }
|
mwbot = { version = "0.7.0", default-features = false, features = ["generators", "mwbot_derive"] }
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
reqwest = "0.12.22"
|
reqwest = "0.12.22"
|
||||||
scraper = "0.24.0"
|
scraper = "0.24.0"
|
||||||
|
sentry = { version = "0.45.0", features = ["tracing"] }
|
||||||
|
sentry-tracing = { version = "0.45.0", features = ["backtrace", "logs"] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_plain = "1.0.2"
|
serde_plain = "1.0.2"
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
|
|||||||
1
robert-2026-01-07.html
Normal file
1
robert-2026-01-07.html
Normal file
File diff suppressed because one or more lines are too long
@@ -24,7 +24,7 @@ pub enum DanceRank {
|
|||||||
Newcomer,
|
Newcomer,
|
||||||
Novice,
|
Novice,
|
||||||
Intermediate,
|
Intermediate,
|
||||||
#[serde(rename = "Advance")]
|
#[serde(rename = "Advance", alias = "Advanced")]
|
||||||
Advanced,
|
Advanced,
|
||||||
#[serde(rename = "All Star", alias = "All-Stars")]
|
#[serde(rename = "All Star", alias = "All-Stars")]
|
||||||
AllStars,
|
AllStars,
|
||||||
|
|||||||
109
src/main.rs
109
src/main.rs
@@ -16,7 +16,6 @@
|
|||||||
clippy::cast_possible_wrap,
|
clippy::cast_possible_wrap,
|
||||||
reason = "Disable this for most of the time, enable this for cleanup later"
|
reason = "Disable this for most of the time, enable this for cleanup later"
|
||||||
)]
|
)]
|
||||||
#![feature(hash_map_macro)]
|
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
|
|
||||||
use mwbot::{
|
use mwbot::{
|
||||||
@@ -24,8 +23,10 @@ use mwbot::{
|
|||||||
generators::{Generator, SortDirection, categories::CategoryMemberSort},
|
generators::{Generator, SortDirection, categories::CategoryMemberSort},
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use tracing::level_filters::LevelFilter;
|
||||||
|
use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
use crate::watchdog::watch_wanted;
|
use crate::watchdog::{update_wanted_ids, watch_wanted};
|
||||||
|
|
||||||
mod dance_info;
|
mod dance_info;
|
||||||
mod updater;
|
mod updater;
|
||||||
@@ -60,24 +61,104 @@ pub enum AppError {
|
|||||||
BotError(#[from] ConfigError),
|
BotError(#[from] ConfigError),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), AppError> {
|
fn init_sentry() -> Option<sentry::ClientInitGuard> {
|
||||||
tracing_subscriber::fmt()
|
let fmt_filter = tracing_subscriber::fmt::layer().with_filter(
|
||||||
.with_level(true)
|
tracing_subscriber::EnvFilter::builder()
|
||||||
.with_max_level(tracing::Level::INFO)
|
.with_default_directive(LevelFilter::INFO.into())
|
||||||
.init();
|
.from_env_lossy(),
|
||||||
tracing::info!("Starting {}", app_signature());
|
);
|
||||||
|
let guard: Option<sentry::ClientInitGuard> = match std::fs::read_to_string("sentry_dsn.txt") {
|
||||||
|
Ok(dsn) => {
|
||||||
|
let guard = sentry::init((
|
||||||
|
dsn,
|
||||||
|
sentry::ClientOptions {
|
||||||
|
release: sentry::release_name!(),
|
||||||
|
traces_sample_rate: 1.0,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
let sentry_layer = sentry::integrations::tracing::layer()
|
||||||
|
.event_filter(|md| match *md.level() {
|
||||||
|
tracing::Level::ERROR => sentry_tracing::EventFilter::Event,
|
||||||
|
_ => sentry_tracing::EventFilter::Ignore,
|
||||||
|
})
|
||||||
|
.span_filter(|md| {
|
||||||
|
matches!(*md.level(), tracing::Level::ERROR | tracing::Level::WARN)
|
||||||
|
});
|
||||||
|
|
||||||
|
tracing_subscriber::registry()
|
||||||
|
.with(fmt_filter)
|
||||||
|
.with(sentry_layer)
|
||||||
|
.init();
|
||||||
|
tracing::info!("Starting {} with sentry", app_signature());
|
||||||
|
|
||||||
|
Some(guard)
|
||||||
|
}
|
||||||
|
Err(error) => {
|
||||||
|
tracing_subscriber::registry().with(fmt_filter).init();
|
||||||
|
tracing::warn!("Could not load 'sentry_dsn.txt': {}", error);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
};
|
||||||
|
guard
|
||||||
|
}
|
||||||
|
|
||||||
|
use clap::{Parser, Subcommand};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "myapp")]
|
||||||
|
#[command(about = "A simple CLI app with subcommands", long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Option<Commands>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Commands {
|
||||||
|
/// Build pages for all missing teachers
|
||||||
|
Missing,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), AppError> {
|
||||||
|
let _guard = init_sentry();
|
||||||
|
|
||||||
|
// Register the Sentry tracing layer to capture breadcrumbs, events, and spans:
|
||||||
let rt = tokio::runtime::Builder::new_current_thread()
|
let rt = tokio::runtime::Builder::new_current_thread()
|
||||||
.enable_all()
|
.enable_all()
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
|
let cli = Cli::parse();
|
||||||
|
|
||||||
let bot = rt.block_on(Bot::from_path(Path::new("./mwbot.toml")))?;
|
let bot = rt.block_on(Bot::from_path(Path::new("./mwbot.toml")))?;
|
||||||
|
|
||||||
#[allow(
|
match &cli.command {
|
||||||
unreachable_code,
|
Some(Commands::Missing) => {
|
||||||
reason = "This is a false positive I think, I just want to loop infinitely on two futures"
|
rt.block_on(async {
|
||||||
)]
|
let wanted = wikiinfo::wanted_ids(bot.clone()).await;
|
||||||
rt.block_on(async { futures::join!(watch_wanted(bot.clone()), updater::update_wsdc(bot)) });
|
tracing::info!(
|
||||||
|
"Missing ids: {}",
|
||||||
|
wanted
|
||||||
|
.iter()
|
||||||
|
.map(|(v, _)| v)
|
||||||
|
.map(u32::to_string)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n")
|
||||||
|
);
|
||||||
|
update_wanted_ids(&wanted, &[]).await;
|
||||||
|
});
|
||||||
|
|
||||||
Ok(())
|
return Ok(());
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
#[allow(
|
||||||
|
unreachable_code,
|
||||||
|
reason = "This is a false positive I think, I just want to loop infinitely on two futures"
|
||||||
|
)]
|
||||||
|
rt.block_on(async {
|
||||||
|
futures::join!(watch_wanted(bot.clone()), updater::update_wsdc(bot))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unreachable!();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,10 +45,10 @@ pub async fn watch_wanted(bot: Bot) -> ! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_wanted_ids(wanted: &[(u32, Page)], ignored_ids: &[u32]) -> Vec<u32> {
|
pub async fn update_wanted_ids(wanted: &[(u32, Page)], ignored_ids: &[u32]) -> Vec<u32> {
|
||||||
let mut new_ignored = vec![];
|
let mut new_ignored = vec![];
|
||||||
|
|
||||||
for (id, page) in wanted.iter().filter(|(x, _)| ignored_ids.contains(x)) {
|
for (id, page) in wanted.iter().filter(|(x, _)| !ignored_ids.contains(x)) {
|
||||||
let span = tracing::info_span!("update", id);
|
let span = tracing::info_span!("update", id);
|
||||||
let _enter = span.enter();
|
let _enter = span.enter();
|
||||||
if let Err(e) = generate_page(*id, page.clone()).await {
|
if let Err(e) = generate_page(*id, page.clone()).await {
|
||||||
|
|||||||
@@ -10,7 +10,15 @@ pub async fn wanted_ids(bot: Bot) -> Vec<(u32, Page)> {
|
|||||||
let p = match x {
|
let p = match x {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!("Could not get search result: {e}");
|
match e {
|
||||||
|
mwbot::Error::ApiError(a) if &a.code == "assertuserfailed" => {
|
||||||
|
tracing::error!("Bot is logged out: {a}");
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
tracing::error!("Could not get search result: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ pub async fn fetch_wsdc_info_wsdc(id: u32) -> Result<DanceInfo, DanceInfoError>
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
pub async fn fetch_wsdc_info(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
||||||
fetch_wsdc_info_scoring_dance(id).await
|
// fetch_wsdc_info_scoring_dance(id).await
|
||||||
|
fetch_wsdc_info_wsdc(id).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -71,7 +72,7 @@ pub enum DanceInfoError {
|
|||||||
ClientBuild(reqwest::Error),
|
ClientBuild(reqwest::Error),
|
||||||
#[error("Failed to build request: {0}")]
|
#[error("Failed to build request: {0}")]
|
||||||
RequestBuild(reqwest::Error),
|
RequestBuild(reqwest::Error),
|
||||||
#[error("Request error: {0}")]
|
#[error("Request error: {0:#?}")]
|
||||||
Request(reqwest::Error),
|
Request(reqwest::Error),
|
||||||
#[error("Failed to parse response: {0}")]
|
#[error("Failed to parse response: {0}")]
|
||||||
JsonParse(reqwest::Error),
|
JsonParse(reqwest::Error),
|
||||||
|
|||||||
@@ -140,14 +140,21 @@ fn parse_stats(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn extract_tables(html: &str) -> Result<Vec<(String, Vec<Vec<String>>)>, ScoringParseError> {
|
fn extract_tables(html: &str) -> Result<Vec<(String, Vec<Vec<String>>)>, ScoringParseError> {
|
||||||
|
dbg!(&html);
|
||||||
let document = Html::parse_document(html);
|
let document = Html::parse_document(html);
|
||||||
let card_selector = Selector::parse("div:has( > div.card-header)").unwrap();
|
let card_selector = Selector::parse("div:has( > div.card-header)").unwrap();
|
||||||
|
document
|
||||||
document.select(&card_selector).map(parse_card).collect()
|
.select(&card_selector)
|
||||||
|
.inspect(|v| {
|
||||||
|
dbg!(&v);
|
||||||
|
})
|
||||||
|
.map(parse_card)
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_info(html: &str) -> Result<DanceInfo, ScoringParseError> {
|
fn parse_info(html: &str) -> Result<DanceInfo, ScoringParseError> {
|
||||||
let tables = extract_tables(html)?;
|
let tables = extract_tables(html)?;
|
||||||
|
dbg!(&tables);
|
||||||
let details = &tables
|
let details = &tables
|
||||||
.iter()
|
.iter()
|
||||||
.find(|(v, _)| v.to_lowercase().contains("detail"))
|
.find(|(v, _)| v.to_lowercase().contains("detail"))
|
||||||
@@ -173,7 +180,7 @@ fn parse_info(html: &str) -> Result<DanceInfo, ScoringParseError> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_table() {
|
fn test_parse_table() {
|
||||||
dbg!(parse_info(include_str!("../../polina.html")));
|
dbg!(parse_info(include_str!("../../robert-2026-01-07.html")));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_wsdc_info_scoring_dance(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
pub async fn fetch_wsdc_info_scoring_dance(id: u32) -> Result<DanceInfo, DanceInfoError> {
|
||||||
|
|||||||
Reference in New Issue
Block a user