Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Created May 21, 2018 08:13
Show Gist options
  • Save tmtmtmtm/1fe4a83e0c175245824654becaab89a2 to your computer and use it in GitHub Desktop.
Save tmtmtmtm/1fe4a83e0c175245824654becaab89a2 to your computer and use it in GitHub Desktop.
#!/bin/env ruby
require 'csv'
require 'rest-client'
#-----------------------------------------------------------------------
# Find all Memberships of the Romanian Chamber of Deputies with a start
# time after December 2016 and add a 2016-20 term qualifier if missing.
#
# The output of this can then be passed to PositionStatements
# https://github.com/everypolitician/position_statements
#-----------------------------------------------------------------------
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
{
?item p:P39 ?ps .
?ps ps:P39/wdt:P279* wd:Q17556530 ; pq:P580 ?start .
FILTER (?start >= "2016-12-19T00:00:00Z"^^xsd:dateTime)
FILTER NOT EXISTS { ?ps pq:P2937 wd:Q28726607 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
SPARQL
sparql(memberships_query).map(&:to_h).each do |row|
puts [row[:item].split('/').last, "P39", row[:ps].split('/').last, 'P2937', 'Q28726607'].join("\t")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment