<commit_before>require 'rubygems'
require 'scraperwiki'
require 'httparty'
require 'open-uri'
require 'json'
require 'i18n'

# --------------------
# scrapable_classes.rb
# --------------------

module RestfulApiMethods

  def format info
    info
  end

  def put record
  end

  def post record
  end
end

class PeopleStorage
  include RestfulApiMethods

  def save record
    post record
  end

  def post record
    #######################
    # for use with morph.io
    #######################

    if ((ScraperWiki.select("* from data where `uid`='#{record['uid']}'").empty?) rescue true)
      # Convert the array record['organizations'] to a string (by converting to json)
      if record['organizations'].is_a? Array
        record['organizations'] = JSON.dump(record['organizations'])
      end
      ScraperWiki.save_sqlite(['uid'], record)
      puts "Adds new record " + record['uid']
    else
      puts "Skipping already saved record " + record['uid']
    end
  end
end

class CongressmenProfiles < PeopleStorage
  def initialize()
    super()
    @location = 'http://pmocl.popit.mysociety.org/api/v0.1/persons/?per_page=200'
    @location_organizations = 'http://pmocl.popit.mysociety.org/api/v0.1/organizations/'
  end

  def process
    response = HTTParty.get(@location, :content_type => :json)
    response = JSON.parse(response.body)
    popit_congressmen = response['result']

    popit_congressmen.each do |congressman|
      record = get_info congressman
      post record
    end
  end

  def get_info congressman
    organizations = String.new
    if !congressman['memberships'].empty?
      congressman_organization_id = congressman['memberships'].first['organization_id']
      organizations = get_memberships congressman_organization_id
    end
    record = {
      'uid' => congressman['id'],
      'name' => I18n.transliterate(congressman['name']),
      'chamber' => congressman['title'],
      'district' => I18n.transliterate(congressman['represent'].first['district']).gsub('?','ta.'),
      'commune' => I18n.transliterate(congressman['represent'].first['comunas']),
      'region' => I18n.transliterate(congressman['represent'].first['region']),
      'profile_image' => '',
      'organization_id' => '',
      'organizations' => organizations,
      'date_scraped' => Date.today.to_s
    }
    if !congressman['images'].nil? then record['profile_image'] = congressman['images'].first['url'] end
    if !organizations.empty? then record['organization_id'] = congressman_organization_id end
    return record
  end

  def get_memberships organization_id
    response = HTTParty.get(@location_organizations + organization_id, :content_type => :json)
    response = JSON.parse(response.body)
    popit_membership = response['result']

    organizations = Array.new
    organizations[0] = I18n.transliterate(popit_membership['name'])
    i = 1
    popit_membership['other_names'].each do |organization|
      organizations[i] = I18n.transliterate(organization['name'])
      i = i + 1
    end
    return organizations
  end
end


# ---------------------
# congressmen_runner.rb
# ---------------------

if !(defined? Test::Unit::TestCase)
  CongressmenProfiles.new.process<commit_msg>removes the use of I18n.transliterate<commit_after>require 'rubygems'
require 'scraperwiki'
require 'httparty'
require 'open-uri'
require 'json'
require 'i18n'

# Scrapable classes
module RestfulApiMethods

  def format info
    info
  end

  def put record
  end

  def post record
  end
end

class PeopleStorage
  include RestfulApiMethods

  def save record
    post record
  end

  def post record
    if ((ScraperWiki.select("* from data where `uid`='#{record['uid']}'").empty?) rescue true)
      # Convert the array record['organizations'] to a string (by converting to json)
      if record['organizations'].is_a? Array
        record['organizations'] = JSON.dump(record['organizations'])
      end
      ScraperWiki.save_sqlite(['uid'], record)
      puts "Adds new record " + record['uid']
    else
      puts "Skipping already saved record " + record['uid']
    end
  end
end

# The real thing
class CongressmenProfiles < PeopleStorage
  def initialize()
    super()
    @location = 'http://pmocl.popit.mysociety.org/api/v0.1/persons/?per_page=200'
    @location_organizations = 'http://pmocl.popit.mysociety.org/api/v0.1/organizations/'
  end

  def process
    response = HTTParty.get(@location, :content_type => :json)
    response = JSON.parse(response.body)
    popit_congressmen = response['result']

    popit_congressmen.each do |congressman|
      record = get_info congressman
      post record
    end
  end

  def get_info congressman
    organizations = String.new
    if !congressman['memberships'].empty?
      congressman_organization_id = congressman['memberships'].first['organization_id']
      organizations = get_memberships congressman_organization_id
    end
    record = {
      'uid' => congressman['id'],
      'name' => congressman['name'],
      'chamber' => congressman['title'],
      'district' => congressman['represent'].first['district'].gsub('?','ta.'),
      'commune' => congressman['represent'].first['comunas'],
      'region' => congressman['represent'].first['region'],
      'profile_image' => '',
      'organization_id' => '',
      'organizations' => organizations,
      'date_scraped' => Date.today.to_s
    }
    if !congressman['images'].nil? then record['profile_image'] = congressman['images'].first['url'] end
    if !organizations.empty? then record['organization_id'] = congressman_organization_id end
    return record
  end

  def get_memberships organization_id
    response = HTTParty.get(@location_organizations + organization_id, :content_type => :json)
    response = JSON.parse(response.body)
    popit_membership = response['result']

    organizations = Array.new
    organizations[0] = popit_membership['name']
    i = 1
    popit_membership['other_names'].each do |organization|
      organizations[i] = organization['name']
      i = i + 1
    end
    return organizations
  end
end

# Runner
if !(defined? Test::Unit::TestCase)
  CongressmenProfiles.new.process
{'fraction_non_alphanumeric': 0.14320506575742814, 'fraction_numerical': 0.0061698327650592626, 'mean_word_length': 3.5663454410674573, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1291387', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1863, 'n_words': 407}