Cleanup the code made so far

master
Raymon Zutekouw 2 days ago
parent cd8d5f14d1
commit 0097b105a1
Signed by: raymon
GPG Key ID: 0E62222846283925
  1. 29
      src/lib.rs

@ -6,22 +6,18 @@ pub mod wikipedia_infobox_analyzer {
/// Looks for a used template that does lists wikidata properties listing
pub fn extract_used_properties_from_template(template: String) -> Vec<String> {
let lines = template.lines();
let mut lines = template.lines();
let used = lines
.filter(|line| line.starts_with("{{Gebruikt Wikidata"))
.nth(0)
.find(|line| line.starts_with("{{Gebruikt Wikidata"))
.expect("Template should have a line declaring which properties are used");
// The properties are listed inside the template with their number: P1|P2|P...
let re = Regex::new(r"P\d+").unwrap();
// Find all matches and collect into vector
let properties = re
.find_iter(used)
.map(|m| m.as_str().to_owned())
.collect::<Vec<String>>();
return properties;
// Find all matches and collect the properties into vector
re.find_iter(used)
.map(|m| m.as_str().to_owned())
.collect()
}
/// Creates an ascii infobox out of the properties
@ -34,23 +30,20 @@ pub mod wikipedia_infobox_analyzer {
.set_align(Align::Left);
let data: Vec<Vec<&String>> = properties.iter().map(|v| vec![v]).collect();
return ascii_table.format(data);
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!(
"
let query = format!("
SELECT ?label
WHERE
{{
wd:P{} rdfs:label ?label.
wd:P{pid} 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
@ -63,7 +56,7 @@ pub mod wikipedia_infobox_analyzer {
let re = Regex::new("\"value\": \"(.+)\"").unwrap();
let Some(label) = re.captures(&res_s) else { return "<unknown>".to_string() };
return label[1].to_string();
label[1].to_string()
}
/// Fetches the properties which have claims for the given wiki item inside of wikidata

Loading…
Cancel
Save