Skip to content

Commit

Permalink
Routine update
Browse files Browse the repository at this point in the history
  • Loading branch information
retep998 committed Sep 23, 2023
1 parent 19a0000 commit a0838d8
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ name = "mediawiki"
version = "0.0.1"
authors = ["Peter Atashian <[email protected]>"]
license = "MIT/Apache-2.0"
edition = "2018"
edition = "2021"

[dependencies]
cookie = "0.15"
cookie = "0.17"
regex = "1.0"
reqwest = { version = "0.11", features = ["blocking", "multipart"] }
serde = { version = "1.0", features = ["derive"] }
Expand Down
38 changes: 38 additions & 0 deletions src/bin/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use mediawiki::{oredict::Oredict, tilesheet::Tilesheet, Mediawiki};
use serde_json::Value;
use std::fs;

fn export() {
let mw = Mediawiki::login_path("ftb.json").unwrap();
let tiles = mw
.query_tiles(None)
.into_iter()
.map(|tile| tile.unwrap())
.collect::<Vec<_>>();
fs::write("tiles.json", Value::Array(tiles.clone()).to_string()).unwrap();
let sheets = mw
.query_sheets()
.into_iter()
.map(|sheet| sheet.unwrap())
.collect::<Vec<_>>();
fs::write("sheets.json", Value::Array(sheets).to_string()).unwrap();
let ores = mw
.query_ores(None)
.into_iter()
.map(|ore| ore.unwrap())
.collect::<Vec<_>>();
fs::write("ores.json", Value::Array(ores).to_string()).unwrap();
let translations = tiles
.into_iter()
.flat_map(|tile| {
let id = tile["id"].as_i64().unwrap();
mw.query_tile_translations(id)
.into_iter()
.map(|trans| trans.unwrap())
})
.collect::<Vec<_>>();
fs::write("translations.json", Value::Array(translations).to_string()).unwrap();
}
fn main() {
export();
}
2 changes: 1 addition & 1 deletion src/bin/fixgt5ore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn fix_gt5() {
assert!(mod_name == "GT");
let pair = (tag_name.into(), item_name.into());
if !gt5_ores.contains(&pair) {
println!("{:?}", pair);
println!("{pair:?}");
mw.edit_ore(&token, id, Some("GT5"), None, None, None)
.unwrap();
}
Expand Down
27 changes: 27 additions & 0 deletions src/bin/purgesheet.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use mediawiki::{tilesheet::Tilesheet, Mediawiki};
use std::env::args;

fn purge(mod_name: &str) {
let mw = Mediawiki::login_path("ftb.json").unwrap();
let todelete: Vec<String> = mw
.query_tiles(Some(mod_name))
.into_iter()
.map(|tile| {
let tile = tile.unwrap();
let tile = tile.as_object().unwrap();
tile["id"].as_i64().unwrap().to_string()
})
.collect();
let token = mw.get_token().unwrap();
for chunk in todelete.chunks(100) {
let blob = chunk.join("|");
mw.delete_tiles(&token, &blob, Some("Purging tiles"))
.unwrap();
}
mw.delete_sheet(&token, mod_name, Some("Purging tilesheet"))
.unwrap();
}
fn main() {
let name = args().nth(1).unwrap();
purge(&name);
}
4 changes: 2 additions & 2 deletions src/bin/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ fn main() {
let mut file = files
.entry(name.clone())
.or_insert_with(|| BufWriter::new(File::create(name).unwrap()));
writeln!(&mut file, "{}", change)?;
writeln!(&mut file, "{change}")?;
Ok(())
})() {
println!("Error: {:?}", e);
println!("Error: {e:?}");
}
}
}
10 changes: 5 additions & 5 deletions src/bin/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ fn upload_test(filename: &str, file: Upload, comment: Option<&str>) {
let result = mw
.upload(filename, &token, file, None, comment, false)
.unwrap();
println!("{:?}", result);
println!("{result:?}");
match result["upload"]["result"].as_str().unwrap() {
"Warning" => (),
"Success" => return,
other => panic!("Unknown result: {}", other),
other => panic!("Unknown result: {other}"),
}
for (warning, _value) in result["upload"]["warnings"].as_object().unwrap() {
match &**warning {
"was-deleted" => (),
"duplicate" => (),
"exists" => (),
other => panic!("Unknown warning: {}", other),
other => panic!("Unknown warning: {other}"),
}
}
let filekey = result["upload"]["filekey"].as_str().unwrap();
Expand All @@ -30,11 +30,11 @@ fn upload_test(filename: &str, file: Upload, comment: Option<&str>) {
true,
)
.unwrap();
println!("{:?}", result);
println!("{result:?}");
match result["upload"]["result"].as_str().unwrap() {
"Warning" => (),
"Success" => (),
other => panic!("Unknown result: {}", other),
other => panic!("Unknown result: {other}"),
}
}
fn main() {
Expand Down
7 changes: 1 addition & 6 deletions src/bin/verifyoretile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ fn import() {
// if mod_name == "GT6-I" {
// todelete.push(id.to_string());
// }
writeln!(
&mut file,
"{} {} = {} ({})",
id, tag_name, item_name, mod_name
)
.unwrap();
writeln!(&mut file, "{id} {tag_name} = {item_name} ({mod_name})",).unwrap();
}
}
/*
Expand Down
29 changes: 17 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ impl Mediawiki {
config,
client: Client::new(),
};
mw.login()?;
if !mw.config.username.is_empty() {
mw.login()?;
}
Ok(mw)
}
pub fn login_path<P: AsRef<Path>>(path: P) -> Result<Mediawiki, Error> {
Expand Down Expand Up @@ -148,7 +150,7 @@ impl Mediawiki {
let mut request = self.request();
request.arg("action", "query");
request.arg("prop", "imageinfo");
request.arg("titles", format!("File:{}", name));
request.arg("titles", format!("File:{name}"));
request.arg("iiprop", "url");
let json = request.get()?;
let images = json["query"]["pages"]
Expand All @@ -171,7 +173,7 @@ impl Mediawiki {
if response.status() == StatusCode::OK {
break response;
}
println!("{:?}", response);
println!("{response:?}");
sleep(Duration::from_secs(5))
};
let mut buf = Vec::new();
Expand Down Expand Up @@ -222,6 +224,7 @@ impl<'a> RequestBuilder<'a> {
args: HashMap::new(),
};
request.arg("format", "json");
request.arg("formatversion", "2");
request
}
pub fn arg<T, U>(&mut self, key: T, val: U) -> &mut Self
Expand Down Expand Up @@ -277,25 +280,29 @@ impl<'a> RequestBuilder<'a> {
let text = response.text()?;
if status.is_success() {
let json: Json = serde_json::from_str(&text)?;
Ok(json)
if json["error"].is_object() {
Err(Error::Json(json))
} else {
Ok(json)
}
} else {
println!("{:?}", text);
println!("{text:?}");
Err(status.to_string().into())
}
}
pub fn post(&self) -> Result<Json, Error> {
loop {
match self.request(Method::POST, None) {
Ok(json) => return Ok(json),
Err(status) => println!("{:?}", status),
Err(status) => println!("{status:?}"),
}
}
}
pub fn get(&self) -> Result<Json, Error> {
loop {
match self.request(Method::GET, None) {
Ok(json) => return Ok(json),
Err(status) => println!("{:?}", status),
Err(status) => println!("{status:?}"),
}
}
}
Expand Down Expand Up @@ -359,10 +366,8 @@ impl<'a> Query<'a> {
self.buf.reverse();
if let Json::Object(cont) = &json["continue"] {
for (key, val) in cont {
self.req.arg(
&*key,
val.as_str().ok_or_else(|| Error::Json(json.clone()))?,
);
self.req
.arg(key, val.as_str().ok_or_else(|| Error::Json(json.clone()))?);
}
Ok(true)
} else {
Expand Down Expand Up @@ -397,7 +402,7 @@ impl<T> Token<T> {
Token(token.to_owned(), PhantomData)
}
fn value(&self) -> &str {
&*self.0
&self.0
}
}
#[derive(Debug)]
Expand Down
7 changes: 7 additions & 0 deletions src/tilesheet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{Csrf, Error, Json, Mediawiki, QueryBuilder, Token};
pub trait Tilesheet {
fn query_tiles(&self, tsmod: Option<&str>) -> QueryBuilder;
fn query_tile_translations(&self, tsid: i64) -> QueryBuilder;
fn add_tiles(
&self,
token: &Token<Csrf>,
Expand Down Expand Up @@ -49,6 +50,12 @@ impl Tilesheet for Mediawiki {
query.arg("tslimit", "5000");
query
}
fn query_tile_translations(&self, tsid: i64) -> QueryBuilder {
let mut query = self.query("tiles");
query.arg("list", "tiletranslations");
query.arg("tsid", tsid.to_string());
query
}
fn delete_sheet(
&self,
token: &Token<Csrf>,
Expand Down

0 comments on commit a0838d8

Please sign in to comment.