Skip to content

Commit

Permalink
Initial scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
tmtmtmtm committed Apr 7, 2017
1 parent a21e034 commit 0c5824d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/politician.rb
@@ -0,0 +1,38 @@
require 'wikisnakker'

module Wikisnakker
class Politician < Item
P39_QUALIFIERS = {
P102: :party,
P155: :follows,
P156: :followed_by,
P194: :body,
P580: :start_date,
P582: :end_date,
P642: :of,
P768: :constituency,
P1365: :replaces,
P1366: :replaced_by,
P2715: :election,
P2937: :term,
}.freeze

def positions
return [] if self.P39s.empty?
self.P39s.map do |posn|
quals = posn.qualifiers
qdata = quals.properties.partition { |p| P39_QUALIFIERS[p] }
warn "#{id}: #{posn.value.id} + unknown #{qdata.last.join(', ')}" unless qdata.last.empty?

qgood = qdata.first.map { |p| [P39_QUALIFIERS[p], quals[p].value.to_s] }.to_h
{
id: id,
position: posn.value.id,
label: posn.value.to_s,
description: posn.value.description(:en).to_s,
start_date: '' # need _something_ here so we can key on it
}.merge(qgood) rescue {}
end
end
end
end
16 changes: 16 additions & 0 deletions scraper.rb
@@ -0,0 +1,16 @@
#!/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true

require 'everypolitician'
require 'pry'
require 'scraperwiki'

require_relative 'lib/politician'

ScraperWiki.sqliteexecute('DELETE FROM data') rescue nil
house = EveryPolitician::Index.new.country('Belarus').lower_house
house.popolo.persons.map(&:wikidata).compact.each_slice(100) do |wanted|
data = Wikisnakker::Politician.find(wanted).flat_map(&:positions).compact
ScraperWiki.save_sqlite(%i(id position start_date), data)
end

0 comments on commit 0c5824d

Please sign in to comment.