Last active
May 24, 2018 09:12
-
-
Save tmtmtmtm/f8e8466e009d0abb5a2d97cea038914a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'csv' | |
require 'rest-client' | |
WIKIDATA_SPARQL_URL = 'https://query.wikidata.org/sparql' | |
def sparql(query) | |
result = RestClient.get WIKIDATA_SPARQL_URL, accept: 'text/csv', params: { query: query } | |
CSV.parse(result.body, headers: true, header_converters: :symbol) | |
rescue RestClient::Exception => e | |
raise "Wikidata query #{query} failed: #{e.message}" | |
end | |
memberships_query = <<SPARQL | |
SELECT ?item ?ps WHERE { | |
?item p:P39 ?ps . | |
?ps ps:P39 wd:Q42712773 . | |
FILTER NOT EXISTS { ?ps pq:P2937 wd:Q29387197 } | |
} | |
SPARQL | |
sparql(memberships_query).map(&:to_h).each do |row| | |
puts [row[:item].split('/').last, 'P39', row[:ps].split('/').last, 'P2937', 'Q29387197'].join("\t") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment