Fetch name for wiki property by querying label

This commit is contained in:
2025-08-03 19:37:46 +02:00
parent 8e457d29a3
commit 9b2d592a48
3 changed files with 2335 additions and 0 deletions
Generated
+2297
View File
File diff suppressed because it is too large Load Diff
+3
View File
@@ -17,3 +17,6 @@ categories = ["command-line-utilities"]
[dependencies]
regex = "1.11.1"
ascii_table = "4.0.7"
mediawiki = "0.3.1"
serde_json = "1.0.64"
tokio = "1.47.1"
+35
View File
@@ -35,6 +35,35 @@ pub mod wikipedia_infobox_analyzer {
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect();
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)]
@@ -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")
}
}