76 lines
2.1 KiB
Rust
76 lines
2.1 KiB
Rust
use std::time::Duration;
|
|
|
|
use crate::app_signature;
|
|
use crate::{wikiinfo::wanted_ids, wikipage::page_from_info, worldsdc::fetch_wsdc_info};
|
|
use mwbot::Bot;
|
|
use mwbot::SaveOptions;
|
|
use tracing::Level;
|
|
|
|
pub async fn watch_wanted(bot: Bot) -> ! {
|
|
let span = tracing::span!(Level::INFO, "wanted_watchdog");
|
|
let _enter = span.enter();
|
|
|
|
let mut ignored_ids = vec![];
|
|
let mut watch_count = 0;
|
|
loop {
|
|
watch_count += 1;
|
|
if watch_count >= 120 {
|
|
watch_count = 0;
|
|
let failed_id_info = if ignored_ids.is_empty() {
|
|
String::new()
|
|
} else {
|
|
format!("[{} failed ids]", ignored_ids.len())
|
|
};
|
|
tracing::info!("Watchdog check{failed_id_info}...");
|
|
}
|
|
let wanted = wanted_ids(bot.clone()).await;
|
|
let mut new_ignored = vec![];
|
|
for (id, page) in wanted.into_iter().filter(|(x, _)| ignored_ids.contains(x)) {
|
|
if !generate_page(id, page).await {
|
|
new_ignored.push(id);
|
|
}
|
|
}
|
|
if !new_ignored.is_empty() {
|
|
ignored_ids.extend(new_ignored);
|
|
}
|
|
tokio::time::sleep(Duration::from_secs(30)).await;
|
|
}
|
|
}
|
|
|
|
pub async fn generate_page(id: u32, page: mwbot::Page) -> bool {
|
|
tracing::info!("Generating page for {id}");
|
|
let info = match fetch_wsdc_info(id).await {
|
|
Ok(o) => o,
|
|
Err(e) => {
|
|
tracing::error!("Error fetching wsdc info for {id}: {e}");
|
|
return false;
|
|
}
|
|
};
|
|
let code = match page_from_info(info) {
|
|
Ok(o) => o,
|
|
Err(e) => {
|
|
tracing::error!("Creating wikicode for {id}: {e}");
|
|
return false;
|
|
}
|
|
};
|
|
|
|
match page
|
|
.save(
|
|
code,
|
|
&SaveOptions::summary(&format!(
|
|
"Created WSDC info from worldsdc.com -- {}",
|
|
app_signature()
|
|
))
|
|
.mark_as_bot(true)
|
|
.mark_as_minor(false),
|
|
)
|
|
.await
|
|
{
|
|
Ok(_) => true,
|
|
Err(e) => {
|
|
tracing::error!("Could not save page for {id}: {e}");
|
|
false
|
|
}
|
|
}
|
|
}
|