Fetch name for wiki property by querying label
This commit is contained in:
Generated
+2297
File diff suppressed because it is too large
Load Diff
@@ -17,3 +17,6 @@ categories = ["command-line-utilities"]
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
ascii_table = "4.0.7"
|
ascii_table = "4.0.7"
|
||||||
|
mediawiki = "0.3.1"
|
||||||
|
serde_json = "1.0.64"
|
||||||
|
tokio = "1.47.1"
|
||||||
|
|||||||
+35
@@ -35,6 +35,35 @@ pub mod wikipedia_infobox_analyzer {
|
|||||||
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect();
|
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect();
|
||||||
return ascii_table.format(data);
|
return ascii_table.format(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fetches the wikidata label for the wiki property
|
||||||
|
pub async fn fetch_name_for_wiki_property(pid: u64) -> String {
|
||||||
|
let query = format!(
|
||||||
|
"
|
||||||
|
SELECT ?label
|
||||||
|
WHERE
|
||||||
|
{{
|
||||||
|
wd:P{} rdfs:label ?label.
|
||||||
|
FILTER(lang(?label) = \"en\")
|
||||||
|
SERVICE wikibase:label {{ bd:serviceParam wikibase:language 'en'.}}
|
||||||
|
}}
|
||||||
|
",
|
||||||
|
pid
|
||||||
|
);
|
||||||
|
|
||||||
|
let api = mediawiki::api::Api::new("https://www.wikidata.org/w/api.php")
|
||||||
|
.await
|
||||||
|
.expect("Wikidata should return the api endpoint");
|
||||||
|
let res = api.sparql_query(&query)
|
||||||
|
.await
|
||||||
|
.expect("Query should be able to retrieve label");
|
||||||
|
let res_s = serde_json::to_string_pretty(&res).unwrap();
|
||||||
|
|
||||||
|
let re = Regex::new("\"value\": \"(.+)\"").unwrap();
|
||||||
|
let Some(label) = re.captures(&res_s) else { return "<unknown>".to_string() };
|
||||||
|
|
||||||
|
return label[1].to_string();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@@ -86,4 +115,10 @@ mod tests {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_fetch_name_for_wiki_property() {
|
||||||
|
let name = fetch_name_for_wiki_property(31).await;
|
||||||
|
assert_eq!(name, "instance of")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user