Commit 579d0f13 authored by legoktm's avatar legoktm
Browse files

[WIP] Proof-of-concept of what page generators look like

parent a1445d32
Pipeline #398673058 failed with stage
in 7 minutes and 56 seconds
......@@ -15,6 +15,7 @@ dirs = "4"
log = "0.4"
mwapi = {path = "../mwapi", version = "^0.3.0-alpha.1"}
mwapi_errors = {version = "0.1"}
mwapi_responses = {path = "../mwapi_responses", version = "0.1.0"}
parsoid = "=0.6.0-alpha.3"
reqwest = {version = "0.11", default-features = false}
serde = {version = "1", features = ["derive"]}
......
/*
Copyright (C) 2021 Kunal Mehta <legoktm@debian.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::{Bot, Page, Result};
use mwapi_responses::prelude::*;
use std::collections::HashMap;
#[query(list = "categorymembers", cmprop = "title")]
struct CategoryResponse;
struct CategoryMembers {
bot: Bot,
title: String,
continue_: HashMap<String, String>,
}
impl CategoryMembers {
fn finished(&self) -> bool {
self.continue_.is_empty()
}
async fn get_batch(&mut self) -> Result<Vec<Page>> {
let mut params = CategoryResponse::params().to_vec();
params.push(("cmtitle", self.title.as_str()));
for (param, value) in &self.continue_ {
params.push((param.as_str(), value.as_str()));
}
dbg!(&params);
let resp: CategoryResponse = self.bot.api.get(&params).await?;
self.continue_ = resp.continue_.clone();
Ok(resp
.items()
.iter()
.map(|item| self.bot.get_page(&item.title))
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::testwp;
#[tokio::test]
async fn category_members() {
let bot = testwp().await;
let mut members = CategoryMembers {
bot,
title: "Category:!Requests".to_string(),
continue_: Default::default(),
};
loop {
let pages = members.get_batch().await.unwrap();
for page in pages {
dbg!(&page.title());
// This page is explicitly in the second continue request
if page.title() == "Wikipedia:Requests/Bot status/Your Bot Name"
{
assert!(true);
return;
}
}
if members.finished() {
break;
}
}
panic!("Unable to find the page");
}
}
......@@ -47,6 +47,7 @@ use tokio::{sync::Mutex, time};
mod config;
mod error;
mod generators;
mod page;
mod utils;
......@@ -54,6 +55,9 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
pub mod parsoid {
pub use parsoid::prelude::*;
}
pub mod responses {
pub use mwapi_responses::prelude::*;
}
pub use error::ConfigError;
pub use page::Page;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment