Fetch list of properties for wiki items
This commit is contained in:
+44
@@ -2,6 +2,7 @@
|
||||
pub mod wikipedia_infobox_analyzer {
|
||||
use ascii_table::{Align, AsciiTable};
|
||||
use regex::Regex;
|
||||
use itertools::Itertools;
|
||||
|
||||
/// Looks for a used template that does lists wikidata properties listing
|
||||
pub fn extract_used_properties_from_template(template: String) -> Vec<String> {
|
||||
@@ -64,6 +65,33 @@ pub mod wikipedia_infobox_analyzer {
|
||||
|
||||
return label[1].to_string();
|
||||
}
|
||||
|
||||
/// Fetches the properties which have claims for the given wiki item inside of wikidata
|
||||
pub fn fetch_properties_for_wiki_item(qid: u64) -> Vec<u64> {
|
||||
let mut ids: Vec<u64> = vec![];
|
||||
let uri = format!("https://www.wikidata.org/wiki/Special:EntityData/Q{qid}.json");
|
||||
let res = reqwest::blocking::get(uri).unwrap();
|
||||
let text = res.text().unwrap();
|
||||
if text.contains("<h1>Not Found</h1><p>No entity with ID ") {
|
||||
return vec![];
|
||||
}
|
||||
let ent = wikidata::Entity::from_json(serde_json::from_str(&text).unwrap()).unwrap();
|
||||
if ent.claims.is_empty() {
|
||||
return vec![];
|
||||
}
|
||||
|
||||
for i in 0..ent.claims.len() {
|
||||
let (wikidata::Pid(pid), _) = &ent.claims[i];
|
||||
ids.push(*pid);
|
||||
}
|
||||
|
||||
ids.sort();
|
||||
|
||||
ids.iter()
|
||||
.map(|n| n.to_owned())
|
||||
.unique()
|
||||
.collect::<Vec<u64>>()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -121,4 +149,20 @@ mod tests {
|
||||
let name = fetch_name_for_wiki_property(31).await;
|
||||
assert_eq!(name, "instance of")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fetch_properties_for_wiki_item() {
|
||||
let qid = 16639197; // identifier for GitLab on wikidata
|
||||
let properties = fetch_properties_for_wiki_item(qid);
|
||||
|
||||
// Properties which should be present:
|
||||
assert!(properties.contains(&10)); // property: video
|
||||
assert!(properties.contains(&18)); // property: image
|
||||
assert!(properties.contains(&154)); // property: logo image
|
||||
assert!(properties.contains(&178)); // property: developer
|
||||
|
||||
// Properties which should not be present:
|
||||
assert!(!properties.contains(&19)); // property: place of birth
|
||||
assert!(!properties.contains(&30)); // property: continent
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user