Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
147 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,8 @@ | ||
Style/AlignHash: | ||
EnforcedHashRocketStyle: table | ||
EnforcedColonStyle: table | ||
|
||
Style/ClassAndModuleCamelCase: | ||
Enabled: false | ||
|
||
Style/CollectionMethods: | ||
Enabled: true | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Style/FormatString: | ||
EnforcedStyle: percent | ||
|
||
Style/HashSyntax: | ||
EnforcedStyle: ruby19_no_mixed_keys | ||
|
||
Style/RescueModifier: | ||
Enabled: false | ||
|
||
Style/SignalException: | ||
Enabled: false | ||
|
||
Style/SymbolArray: | ||
Enabled: true | ||
|
||
Style/TrailingCommaInLiteral: | ||
EnforcedStyleForMultiline: consistent_comma | ||
|
||
|
||
Metrics/ClassLength: | ||
Max: 250 | ||
|
||
Metrics/MethodLength: | ||
Max: 20 | ||
|
||
Metrics/LineLength: | ||
Max: 150 | ||
|
||
Metrics/AbcSize: | ||
Max: 50 | ||
|
||
Metrics/CyclomaticComplexity: | ||
Max: 7 | ||
|
||
Metrics/PerceivedComplexity: | ||
Max: 8 | ||
|
||
# Get rid of these ones over time | ||
inherit_from: .rubocop_todo.yml | ||
AllCops: | ||
Exclude: | ||
- 'Vagrantfile' | ||
- 'vendor/**/*' | ||
TargetRubyVersion: 2.4 | ||
|
||
inherit_from: | ||
- https://raw.githubusercontent.com/everypolitician/everypolitician-data/master/.rubocop_base.yml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# It's easy to add more libraries or choose different versions. Any libraries | ||
# specified here will be installed and made available to your morph.io scraper. | ||
# Find out more: https://morph.io/documentation/ruby | ||
|
||
ruby '2.4.4' | ||
|
||
source 'https://rubygems.org' | ||
git_source(:github) { |repo_name| "https://github.com/#{repo_name}.git" } | ||
|
||
ruby '2.3.3' | ||
|
||
gem 'everypolitician', github: 'everypolitician/everypolitician-ruby' | ||
gem 'pry' | ||
gem 'rubocop' | ||
gem 'rest-client' | ||
gem 'scraped', github: 'everypolitician/scraped', branch: 'scraper-class' | ||
gem 'scraperwiki', github: 'openaustralia/scraperwiki-ruby', branch: 'morph_defaults' | ||
gem 'wikisnakker', github: 'everypolitician/wikisnakker' | ||
gem 'sqlite_magic', github: 'openc/sqlite_magic' | ||
|
||
group :quality do | ||
gem 'rubocop' | ||
end | ||
|
||
group :development do | ||
gem 'pry' | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# TODO: extending Scraped::Scraper with ability to add Strategies | ||
class Scraped::Request::Strategy::LiveRequest | ||
require 'rest-client' | ||
|
||
def url | ||
SPARQL_URL % CGI.escape(QUERY % @url) | ||
end | ||
|
||
private | ||
|
||
def sparql(query) | ||
result = RestClient.get WIKIDATA_SPARQL_URL, accept: 'text/csv', params: { query: query } | ||
CSV.parse(result, headers: true, header_converters: :symbol) | ||
rescue RestClient::Exception => e | ||
raise "Wikidata query #{query} failed: #{e.message}" | ||
end | ||
|
||
SPARQL_URL = 'https://query.wikidata.org/sparql?format=json&query=%s' | ||
|
||
QUERY = <<~SPARQL | ||
SELECT DISTINCT ?ps ?item ?itemLabel ?minister ?ministerLabel ?ordinal ?start ?end ?cabinet ?cabinetLabel | ||
WHERE { | ||
?item p:P39/ps:P39 wd:%s . | ||
?item p:P39 ?ps . | ||
?ps ps:P39 ?minister . | ||
?minister wdt:P279* wd:Q83307 . | ||
OPTIONAL { ?ps pq:P1545 ?ordinal } | ||
OPTIONAL { ?ps pq:P580 ?start } | ||
OPTIONAL { ?ps pq:P582 ?end } | ||
OPTIONAL { ?ps pq:P5054 ?cabinet } | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } | ||
} | ||
SPARQL | ||
end | ||
|
||
class CabinetScraper < Scraped::JSON | ||
field :memberships do | ||
json[:results][:bindings].map { |result| fragment(result => Membership).to_h } | ||
end | ||
|
||
class Membership < Scraped::JSON | ||
field :id do | ||
json.dig(:item, :value).to_s.split('/').last | ||
end | ||
|
||
field :name do | ||
json.dig(:itemLabel, :value) | ||
end | ||
|
||
field :position_id do | ||
json.dig(:ps, :value).to_s.split('/').last | ||
end | ||
|
||
field :position do | ||
json.dig(:minister, :value).to_s.split('/').last | ||
end | ||
|
||
field :label do | ||
json.dig(:ministerLabel, :value) | ||
end | ||
|
||
field :start_date do | ||
json.dig(:start, :value).to_s[0..9] | ||
end | ||
|
||
field :end_date do | ||
json.dig(:end, :value).to_s[0..9] | ||
end | ||
|
||
field :ordinal do | ||
json.dig(:ordinal, :value).to_i | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,8 @@ | ||
#!/bin/env ruby | ||
# encoding: utf-8 | ||
# frozen_string_literal: true | ||
|
||
require 'everypolitician' | ||
require 'pry' | ||
require 'scraped' | ||
require 'scraperwiki' | ||
require_relative 'lib/cabinet' | ||
|
||
require_relative 'lib/politician' | ||
|
||
ScraperWiki.sqliteexecute('DROP TABLE data') rescue nil | ||
house = EveryPolitician::Index.new.country('Ukraine').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 | ||
Scraped::Scraper.new('Q12132454' => CabinetScraper).store(:memberships, index: %i[position_id]) |