1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use log::{debug, info};
pub use mwapi::Client as ApiClient;
use mwapi::ErrorFormat;
pub use mwapi_errors::Error;
use serde::Deserialize;
use std::path::Path;
mod config;
mod page;
mod utils;
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub mod parsoid {
pub use parsoid::prelude::*;
}
pub use config::ConfigError;
pub use page::Page;
use crate::parsoid::*;
#[derive(Deserialize)]
struct Config {
api_url: String,
rest_url: String,
auth: Option<Auth>,
}
#[derive(Deserialize)]
struct Auth {
username: String,
password: String,
}
pub struct Bot {
api: ApiClient,
parsoid: ParsoidClient,
username: Option<String>,
}
impl Bot {
pub async fn from_default_config() -> Result<Self, ConfigError> {
let path = {
let first = std::path::Path::new("mwbot.toml");
if first.exists() {
first.to_path_buf()
} else {
dirs::config_dir()
.expect("Cannot find config directory")
.join("mwbot.toml")
}
};
Self::from_path(&path).await
}
pub async fn from_path(path: &Path) -> Result<Self, ConfigError> {
debug!("Reading config from {:?}", path);
let config: Config = toml::from_str(&std::fs::read_to_string(path)?)?;
Self::from_config(config).await
}
async fn from_config(config: Config) -> Result<Self, ConfigError> {
let mut api = ApiClient::builder(&config.api_url)
.set_maxlag(5)
.set_errorformat(ErrorFormat::Wikitext);
let mut user_agent = vec![];
let mut username = None;
if let Some(auth) = config.auth {
info!("Logging in as {}", &auth.username);
api = api.set_botpassword(&auth.username, &auth.password);
user_agent.push(format!("User:{}", &auth.username));
username = Some(auth.username);
}
user_agent.push(format!("mwbot-rs/{}", env!("CARGO_PKG_VERSION")));
let user_agent = user_agent.join(" ");
Ok(Self {
api: api.set_user_agent(&user_agent).build().await?,
parsoid: ParsoidClient::new(&config.rest_url, &user_agent)?,
username,
})
}
pub fn get_api(&self) -> &ApiClient {
&self.api
}
pub fn get_parsoid(&self) -> &ParsoidClient {
&self.parsoid
}
pub fn get_page(&self, title: &str) -> Page {
Page::new(self, title)
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) async fn testwp() -> Bot {
Bot::from_config(Config {
api_url: "https://test.wikipedia.org/w/api.php".to_string(),
rest_url: "https://test.wikipedia.org/api/rest_v1".to_string(),
auth: None,
})
.await
.unwrap()
}
#[tokio::test]
async fn test_get_api() {
let bot = testwp().await;
bot.get_api().get(&[("action", "query")]).await.unwrap();
}
#[tokio::test]
async fn test_get_page() {
let bot = testwp().await;
let page = bot.get_page("Example");
assert_eq!(page.title(), "Example");
}
}