Class: Transfermarkt::Player

Inherits:
EntityBase show all
Defined in:
lib/transfermarkt/player.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Player

Returns a new instance of Player.



18
19
20
21
22
# File 'lib/transfermarkt/player.rb', line 18

def initialize(options = {})
  super
  self.market_value = self.market_value.to_s.gsub(".", "").to_i
  self.height = self.height.to_s.gsub(",", "").to_i
end

Instance Attribute Details

#ageObject

Returns the value of attribute age.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def age
  @age
end

#clubObject

Returns the value of attribute club.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def club
  @club
end

#date_of_birthObject

Returns the value of attribute date_of_birth.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def date_of_birth
  @date_of_birth
end

#footObject

Returns the value of attribute foot.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def foot
  @foot
end

#full_nameObject

Returns the value of attribute full_name.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def full_name
  @full_name
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def height
  @height
end

#injuries_dataObject

Returns the value of attribute injuries_data.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def injuries_data
  @injuries_data
end

#market_valueObject

Returns the value of attribute market_value.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def market_value
  @market_value
end

#name_in_native_countryObject

Returns the value of attribute name_in_native_country.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def name_in_native_country
  @name_in_native_country
end

#nationalityObject

Returns the value of attribute nationality.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def nationality
  @nationality
end

#performance_dataObject

Returns the value of attribute performance_data.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def performance_data
  @performance_data
end

#pictureObject

Returns the value of attribute picture.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def picture
  @picture
end

#positionObject

Returns the value of attribute position.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def position
  @position
end

#profile_uriObject

Returns the value of attribute profile_uri.



3
4
5
# File 'lib/transfermarkt/player.rb', line 3

def profile_uri
  @profile_uri
end

Class Method Details

.fetch_by_profile_uri(profile_uri = "") ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/transfermarkt/player.rb', line 24

def self.fetch_by_profile_uri(profile_uri = "")
  puts "fetching player profile #{profile_uri}"

  req = self.get("/#{profile_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
  if req.code != 200
    nil
  else
    profile_html = Nokogiri::HTML(req.parsed_response)
    options = {}

    options[:profile_uri] = profile_uri
    options[:club] = profile_html.xpath('//*[@id="centerbig"]//div[1]//div//table//tr[2]//td//a[1]').text
    options[:full_name] = profile_html.xpath('//*[@id="centerbig"]//div[1]//div//table//tr[1]//td[2]//h1').text.gsub(/[\d]/, "").strip
    options[:picture] = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[1]//img')[1]["src"]

    headers = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[2]//table//tr//td[1]').collect(&:text)
    headers = headers.collect {|header| header.downcase.gsub(":", "").gsub(" ", "_").gsub("'s", "").to_sym}

    values = profile_html.xpath('//*[@id="centerbig"]//div[1]//table//tr//td[2]//table//tr//td[2]').collect(&:text)
    values = values.collect {|value| value.strip.match(/[A-Za-z0-9,. -]*/)[0] }

    # get player performance
    options[:performance_data] = {}

    performance_uri = profile_uri.gsub("profil", "leistungsdaten")

    options = options.merge(Hash[headers.zip(values)])

    # If there is a performance data blcok
    if profile_html.xpath('//*[@id="centerbig"]/div[4]/p[3]/a').any?

      perforamnce_types = []
      10.times do |i|
        perforamnce_types << (Time.now.year - i).to_s
      end

      perforamnce_types.each do |type|
        performance_with_type_uri = ""
        if type == "All"
          performance_with_type_uri = performance_uri.gsub(".html", "_gesamt.html")
        else
          performance_with_type_uri = performance_uri.gsub(".html", "_#{type}.html")
        end

        goalkeeper = options[:position] == "Goalkeeper"
        options[:performance_data][type] = self.fetch_performance_data(performance_with_type_uri, goalkeeper) 
      end
    end        
    
    options[:injuries_data] = self.fetch_injuries_data(profile_html)

    puts "fetched player #{options[:full_name]}"

    self.new(options)
  end
end

.fetch_injuries_data(player_html) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/transfermarkt/player.rb', line 107

def self.fetch_injuries_data(player_html)
  injury_data = []
  injuries_headers = [:season, :from, :to, :injury]

  player_html.xpath('//*[@id="centerbig"]/div[4]/table[3]//tr[position()>1]').each do |injury_row|
    values = Nokogiri::HTML::DocumentFragment.parse(injury_row.to_html).search("*//td").collect(&:text)
    injury_data << Hash[injuries_headers.zip(values)]
  end
  injury_data
end

.fetch_performance_data(performance_uri, is_goalkeeper = false) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/transfermarkt/player.rb', line 81

def self.fetch_performance_data(performance_uri, is_goalkeeper = false)
  req = self.get("/#{performance_uri}", headers: {"User-Agent" => Transfermarkt::USER_AGENT})
  if req.code != 200
    nil
  else
    performance_data = []
    performance_html = Nokogiri::HTML(req.parsed_response)
    performance_headers = if is_goalkeeper
      [:competition, :matches, :goals, :own_goals, :assists, :yellow_cards, :second_yellows, :red_cards, :substituted_in, :substituted_out , :goals_conceded, :saves, :minutes]
    else
      [:competition, :matches, :goals, :own_goals, :assists, :yellow_cards, :second_yellows, :red_cards, :substituted_in, :substituted_out, :minutes_per_goal, :minutes]
    end

    performance_html.xpath('//table[@class="standard_tabelle"][1]//tr[position()>1]').each do |competition|
      values = Nokogiri::HTML::DocumentFragment.parse(competition.to_html).search("*//td").collect(&:text)
      if values.first == ""
        values.delete_at 0
      end
      performance_data << Hash[performance_headers.zip(values)]
    end
    performance_data
  end

  return performance_data
end