17 releases

✓ Uses Rust 2018 edition

0.1.16 Jul 3, 2019
0.1.15 Jun 27, 2019
0.1.11 May 30, 2019
0.1.10 Apr 18, 2019
0.1.5 Mar 26, 2019

#12 in Authentication

Download history 22/week @ 2019-04-20 8/week @ 2019-04-27 13/week @ 2019-05-04 14/week @ 2019-05-11 33/week @ 2019-05-18 37/week @ 2019-05-25 45/week @ 2019-06-01 31/week @ 2019-06-08 41/week @ 2019-06-15 127/week @ 2019-06-22 65/week @ 2019-06-29 168/week @ 2019-07-06 5/week @ 2019-07-13 35/week @ 2019-07-20 19/week @ 2019-07-27

167 downloads per month
Used in 2 crates

MIT/Apache

57KB
1K SLoC

Build Status

A MediaWiki client library in Rust

Examples

Get all categories of "Albert Einstein" on English Wikipedia:

let mut api = mediawiki::api::Api::new("https://en.wikipedia.org/w/api.php").unwrap();

// Query parameters
let params: HashMap<_, _> = vec![
    ("action".to_string(), "query".to_string()),
    ("prop".to_string(), "categories".to_string()),
    ("titles".to_string(), "Albert Einstein".to_string()),
    ("cllimit".to_string(), "500".to_string()),
]
.into_iter()
.collect();

// Run query; this will automatically continue if more results are available, and merge all results into one
let res = api.get_query_api_json_all(&params).unwrap();

// Parse result
let categories: Vec<&str> = res["query"]["pages"]
    .as_object()
    .unwrap()
    .iter()
    .flat_map(|(_page_id, page)| {
        page["categories"]
            .as_array()
            .unwrap()
            .iter()
            .map(|c| c["title"].as_str().unwrap())
    })
    .collect();

dbg!(&categories);

Edit the Wikidata Sandbox Item (as a bot):

let mut api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php");
api.login("MY BOT USER NAME", "MY BOT PASSWORD").unwrap();

let token = api.get_edit_token().unwrap();
let params: HashMap<_, _> = vec![
    ("action", "wbeditentity"),
    ("id", "Q4115189"),
    ("data",r#"{"claims":[{"mainsnak":{"snaktype":"value","property":"P1810","datavalue":{"value":"ExampleString","type":"string"}},"type":"statement","rank":"normal"}]}"#),
    ("token", &token),
]
.into_iter()
.collect();
let res = api.post_query_api_json(&params).unwrap();

Query Wikidata using SPARQL:

let mut api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php"); // Will determine the SPARQL API URL via site info data
let res = api.sparql_query ( "SELECT ?q ?qLabel ?fellow_id { ?q wdt:P31 wd:Q5 ; wdt:P6594 ?fellow_id . SERVICE wikibase:label { bd:serviceParam wikibase:language '[AUTO_LANGUAGE],en'. } }" ).unwrap() ;
println!("{}", ::serde_json::to_string_pretty(&res).unwrap());

Dependencies

~20MB
~438K SLoC