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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/transfermarkt/player.rb', line 20

def initialize(options = {})
  super

  encoding_options = {
    :invalid           => :replace,  # Replace invalid byte sequences
    :undef             => :replace,  # Replace anything not defined in ASCII
    :replace           => '',        # Use a blank for those replacements
    :UNIVERSAL_NEWLINE_DECORATOR => true       # Always break lines with \n
  }
  self.age = self.age.to_i
  self.market_value = #self.market_value.to_s.gsub(",", "").to_i
  self.height = self.height.to_s.gsub(",", "").to_i
  self.nationality = self.nationality.to_s.encode(Encoding.find('ASCII'), encoding_options).split("\n").collect(&:strip)
  if self.date_of_birth.present?
    self.age = Date.today.year - Date.parse(self.date_of_birth).year
  end
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

#complete_nameObject

Returns the value of attribute complete_name.



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

def complete_name
  @complete_name
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

#player_agentObject

Returns the value of attribute player_agent.



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

def player_agent
  @player_agent
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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/transfermarkt/player.rb', line 59

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

    puts "**** Parsing player #{profile_uri}"

    options[:profile_uri] = profile_uri

    # //*[@id="main"]/div[7]/div/div/div[2]/div[2]/div[2]/table/tbody/tr[2]/td/a
    club = profile_html.css(".hauptpunkt > a:nth-child(1)").text

    unless club.empty?
      options[:club] = profile_html.css(".hauptpunkt > a:nth-child(1)").text

      # options[:position] = profile_html.xpath('//*[@id="main"]//div[7]//table[1]//tr[3]//td[1]')[1].text.strip
      options[:full_name] = profile_html.css(".dataName > h1:nth-child(2)").text

      options[:picture] = "https:" + profile_html.css(".dataBild > img:nth-child(1)").first["src"]
      #profile_html.xpath('//*[@id="main"]//div[7]//div//div//div[2]//div[1]//img')[0]["src"]

      # options[:name_in_native_country] = profile_html.xpath('//*[@id="main"]//div[9]//div[1]//div[2]//div[2]//div[1]//div//table//tr[1]//td[1]')[0].text

      options[:market_value] = profile_html.css(".dataMarktwert > a:nth-child(1)").text.split(" ").first

      agent = profile_html.css("div.dataDaten:nth-child(2) > p:nth-child(4) > span:nth-child(2) > a:nth-child(1)").text

      # if options[:market_value].include?("Mil")
      #    options[:market_value] = options[:market_value].to_f * 1_000_000
      # else
      #   options[:market_value] = options[:market_value].to_f * 100_000
      # end

      options[:name_in_native_country] = options[:full_name]
      options[:complete_name] = options[:full_name]

      player_info = profile_html.css("table.auflistung").search("tr")

      player_info.each do |info_row|
        header = info_row.search('th')[0].text.strip
        if header == "Name in home country:"
          options[:name_in_native_country] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Date of birth:"
          options[:date_of_birth] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Place of birth:"
          options[:place_of_birth] = info_row.search('td')[0].text.strip
          puts options
        # elsif header == "Age:"
        #   options[:age] = info_row.search('td')[0].text.strip
        #   puts options
        elsif header == "Height:"
          options[:height] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Nationality:"
          options[:nationality] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Position:"
          options[:position] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Foot:"
          options[:foot] = info_row.search('td')[0].text.strip
          puts options
        elsif header == "Complete name:"
          options[:complete_name] = info_row.search('td')[0].text.strip
          puts options
        end
      end

      # get player performance
      # options[:performance_data] = {}
      #
      # performance_uri = profile_uri.gsub("profil", "leistungsdaten") + "/saison/"
      #
      # years = (Time.now.year - 6..Time.now.year).to_a
      # years.each do |year|
      #   goalkeeper = options[:position] == "Goalkeeper"
      #   options[:performance_data][year.to_s] = self.fetch_performance_data(performance_uri + year.to_s, goalkeeper)
      # end
      #
      # # Get injury data
      #
      # injury_uri = profile_uri.gsub("profil", "verletzungen")
      #
      # options[:injuries_data] = self.fetch_injuries_data(injury_uri)
    end

    self.new(options)
  end
end

.search(player_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/transfermarkt/player.rb', line 46

def self.search(player_name)
  req = self.get("/schnellsuche/ergebnis/schnellsuche", query: {query: player_name, x: 0, y:0})
  if req.code != 200
    nil
  else
    profile_html = Nokogiri::HTML(req.parsed_response)
    results = {}
    
    link = profile_html.css(".spielprofil_tooltip")[0]["href"]
    self.fetch_by_profile_uri(link)
  end
end

Instance Method Details

#valid_player?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/transfermarkt/player.rb', line 38

def valid_player?
  if club.nil? or club.empty?
    false
  else
    true
  end
end