Skip to content

Instantly share code, notes, and snippets.

@tmtmtmtm
Created November 22, 2018 10:19
Show Gist options
  • Save tmtmtmtm/4ab667a5e805028ff8181c364f3252ba to your computer and use it in GitHub Desktop.
Save tmtmtmtm/4ab667a5e805028ff8181c364f3252ba to your computer and use it in GitHub Desktop.
Solomon Islands constituency QuickStatements
#!/bin/env ruby
# frozen_string_literal: true
# Produce QuickStatements for creating missing constituency items,
# and adding 'instance of' to existing ones (most of which are missing it)
require 'pry'
require 'scraped'
require 'wikidata_ids_decorator'
require 'open-uri/cached'
OpenURI::Cache.cache_path = '.cache'
class String
def quoted
'"%s"' % self
end
end
class Page < Scraped::HTML
decorator WikidataIdsDecorator::Links
field :constituencies do
relevant_table.xpath('.//tr[td]').map { |tr| fragment(tr => Row) }
end
private
def relevant_table
noko.xpath('//table[.//tr[th[contains(.,"Electorate")]]]')
end
class Row < Scraped::HTML
field :name do
constituency_field.text.tidy
end
field :id do
constituency_field.css('a/@wikidata').text
end
private
def constituency_field
noko.css('td').first
end
end
end
url = 'https://en.wikipedia.org/wiki/List_of_constituencies_of_the_National_Parliament_of_Solomon_Islands'
Page.new(response: Scraped::Request.new(url: url).response).constituencies.each do |c|
if c.id.to_s.empty?
puts 'CREATE'
puts ['LAST', 'Len', c.name.quoted ].join("\t")
puts ['LAST', 'Den', '"Constituency of the Solomon Islands Parliament"' ].join("\t")
puts ['LAST', 'P31', 'Q59081206' ].join("\t")
else
puts [c.id, 'P31', 'Q59081206' ].join("\t")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment