Skip to content

Commit

Permalink
Merge pull request #58 from sunsheeppoplar/ay/scrape
Browse files Browse the repository at this point in the history
fixed merge conflict and changed google maps response index issue
  • Loading branch information
sunsheeppoplar authored Mar 11, 2017
2 parents 7fd750c + 7d6f02e commit d960eb2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions app/models/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ class Player < ApplicationRecord

scope :missing_pob, -> { where(place_of_birth: nil) }

def only_have_nationality
self.place_of_birth.nil? && self.bp_latitude.nil? && bp_longitude.nil?
self.place_of_birth = ""
end

def strip_nationality
deprecated_nations = ["Jugoslawien (SFR)","CSSR","UDSSR","Yugoslavia (Republic)"]
# we can update nationality from another source
Expand Down
16 changes: 8 additions & 8 deletions lib/tasks/get_gps_coords.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ namespace :app do

require 'httparty'
require 'json'
require 'resolv'
require 'resolv-replace'

url_base = "https://maps.googleapis.com/maps/api/geocode/json?"

# club_players = []
# p =Player.find 373
# club_players << p
club_players = Player.all

start = Time.now
Expand All @@ -15,11 +20,6 @@ namespace :app do
player.strip_nationality # replaces specified nationalities with ""
nat = player.nationality

# make sure that we skip over players that TransferMarkt might not have info on / we didn't grab properly for whatever reason
if pob.nil? || nat.nil?
next
end

if (pob.include? " ")
pob = player.place_of_birth.gsub(" ","+")
end
Expand All @@ -29,21 +29,21 @@ namespace :app do
end

query = url_base + "address=" + pob + ",+" + nat + "&" + "AIzaSyCtNLoBlmw1b1zUJa10PSXqHu-QII9NpYU"

# takes unicode and turns it into ascii, strip unneeded accents to ensure Google response
response = HTTParty.get(URI.encode(I18n.transliterate(query)))

if response['status'] == 'OK'

maps_response = JSON.parse(response.body)

location = maps_response['results'][0]['geometry']['location'] #testing
location = maps_response['results'][0]['geometry']['location']

player.write_attribute(:bp_latitude,location['lat'])
player.write_attribute(:bp_longitude,location['lng'])

if nat == "" # overwrites empty string nationalities with proper nation
nation = maps_response['results'][0]['address_components'][2]['long_name'] # holds nation for eastern bloc players hit with Player.strip_nationality
last_index = maps_response['results'][0]['address_components'].length - 1
nation = maps_response['results'][0]['address_components'][last_index]['long_name'] # holds nation for eastern bloc players hit with Player.strip_nationality
player.write_attribute(:nationality, nation)
end

Expand Down
5 changes: 3 additions & 2 deletions lib/tasks/scrape_player_missing_pob.rake
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ namespace :app do

parent_span = doc.at_css('span[itemprop=nationality]')
nationality = parent_span.content
player.write_attribute(:nationality, nationality )

player.write_attribute(:nationality, nationality)
# sets pob to empty string
player.only_have_nationality
player.save!

print "\rNumber of players birthplaces updated: #{i + 1}"
Expand Down

0 comments on commit d960eb2

Please sign in to comment.