Fetch name for wiki property by querying label

master
Raymon Zutekouw 2 days ago
parent 8e457d29a3
commit 9b2d592a48
Signed by: raymon
GPG Key ID: 0E62222846283925
  1. 2341
      Cargo.lock
  2. 3
      Cargo.toml
  3. 35
      src/lib.rs

2341
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -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,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")
}
}

Loading…
Cancel
Save