Skip to content

Instantly share code, notes, and snippets.

View tmtmtmtm's full-sized avatar

Tony Bowden tmtmtmtm

  • Tallinn, Estonia
  • X @tmtm
View GitHub Profile
@tmtmtmtm
tmtmtmtm / stances_json_to_csv
Created December 2, 2014 10:53
Convert a Stance file into simple CSV
require 'json'
require 'csv'
file = ARGV.first or raise "Need a filename"
json = JSON.parse(File.read(file))
puts json.map { |i| i['stances'].map { |bloc, s| [ i['id'], bloc, s['score'] ].to_csv } }
@tmtmtmtm
tmtmtmtm / philippine-elections.rb
Created February 4, 2017 08:26
generate QuickStatements for ensuring Philippine elections have correct "instance of"
#!/usr/bin/ruby
require 'pry'
require 'scraped'
require 'wikidata/fetcher'
INSTANCE_OF = 'P31'
PPE = 'Q7185370'
PSE = 'Q28333164'
PHE = 'Q28649370'
POSITION_HELD = 'P39'
START_DATE = 'P580'
CONSTITUENCY = 'P768'
PARTY = 'P4100'
ELECTED_IN = 'P2715'
SOURCE = 'S143'
TWFY = 'Q7783633'
TERM_MEMBERSHIP = 'Q30524710' # Member of the 57th Parliament of the UK
ELECTION = 'Q25052149' # GE 2017
name wikidata
Aadu Must Q12358062
Aet Maatee Q16407380
Ain Lutsepp Q12358429
Aivar Kokk Q13608089
Aivar Sõerd Q12358481
Alar Nääme Q28845703
Andre Sepp Q16404500
Andrei Novikov Q16405149
Andres Ammas Q20528366
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
require 'pry'
require 'json'
require 'rest-client'
WIKIDATA_SPARQL_URL = 'https://query.wikidata.org/sparql'
#!/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
#!/bin/env ruby
require 'csv'
require 'rest-client'
#---------------------------------------------------------------------------
# Find all Memberships of the 2016-20 with no P4100 qualifier and add it
# from data on the Wikipedia members list page:
# https://ro.wikipedia.org/wiki/Legislatura_2016-2020_(Camera_Deputaților)
# as scraped at
#!/bin/env ruby
require 'csv'
require 'rest-client'
#---------------------------------------------------------------------------
# Find all P4100s from the ALDE disambiguation item to the proper one
#---------------------------------------------------------------------------
WIKIDATA_SPARQL_URL = 'https://query.wikidata.org/sparql'
#!/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)
#!/bin/env ruby
# frozen_string_literal: true
require 'csv'
require 'pry'
require 'rest-client'
#---------------------------------------------------------------------------
# Find all members of the 37th Eduskunta with no P4100 (parliamentary group)
# and add one from