Class: Transfermarkt::League

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from EntityBase

#initialize

Constructor Details

This class inherits a constructor from Transfermarkt::EntityBase

Instance Attribute Details

#club_urisObject

Returns the value of attribute club_uris.



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

def club_uris
  @club_uris
end

#clubsObject

Returns the value of attribute clubs.



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

def clubs
  @clubs
end

#clubs_indexObject

Returns the value of attribute clubs_index.



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

def clubs_index
  @clubs_index
end

#countryObject

Returns the value of attribute country.



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

def country
  @country
end

#league_uriObject

Returns the value of attribute league_uri.



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

def league_uri
  @league_uri
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.fetch_by_league_uri(league_uri, fetch_clubs = false) ⇒ Object



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
# File 'lib/transfermarkt/league.rb', line 49

def self.fetch_by_league_uri(league_uri, fetch_clubs = false)
  puts "fetching league #{league_uri}"

  req = self.get("/#{league_uri}", headers: {"User-Agent" => Useragents.rand()})
  if req.code != 200
    nil
  else
    league_html = Nokogiri::HTML(req.parsed_response)
    options = {}

    options[:league_uri] = league_uri
    options[:name] = league_html.xpath('//select[@id="wettbewerb_select_breadcrumb"]//option[@selected="selected"]')[0].text
    options[:country] = league_html.xpath('//select[@id="land_select_breadcrumb"]//option[@selected="selected"]').text

    options[:club_uris] = league_html.xpath('//*[@id="yw1"]//table//tr//td[2]//a[1]').collect{|player_html| player_html["href"]}

    puts "Found #{options[:club_uris].count} clubs"
    options[:clubs] = []

    if fetch_clubs
      options[:club_uris].each do |club_uri|
        options[:clubs] << Transfermarkt::Club.fetch_by_club_uri(club_uri, fetch_clubs)
      end
    end

    puts "fetched league clubs for #{options[:name]}"

    self.new(options)
  end
end

.fetch_clubs_and_uris_by_league_uri(league_uri) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/transfermarkt/league.rb', line 18

def self.fetch_clubs_and_uris_by_league_uri(league_uri)
  req = self.get("/#{league_uri}", headers: {"User-Agent" => ::UserAgents.rand()})
  if req.code != 200
    raise req.code.to_s
  else
    league_html = Nokogiri::HTML(req.parsed_response)
    options = {}
    puts "**** Parsing league #{league_uri}"
    options[:league_uri] = league_uri
    unless league_html.xpath('//table[@class="profilheader"]//tr[1]//th[1]')[0].text == "Type of cup:"
      league_name = league_html.xpath('//select[@id="wettbewerb_select_breadcrumb"]//option[@selected="selected"]')
      if league_name.empty?
        options[:name] = league_html.xpath('//div[@class="spielername-profil"]').text.strip
        options[:country] = league_html.xpath('//table[@class="profilheader"]//img/@title').first.value
      else
        options[:name] = league_name[0].text
        options[:country] = league_html.xpath('//select[@id="land_select_breadcrumb"]//option[@selected="selected"]').text
      end
      club_uris = league_html.xpath('//*[@id="yw1"]//table//tr//td[2]//a[1]').collect{|player_html| player_html["href"]}
      club_names = league_html.xpath('//*[@id="yw1"]//table//tr//td[2]//a[1]').collect{|player_html| player_html.text }

      clubs = Hash[club_names.zip(club_uris)]

      options[:clubs_index] = clubs

      puts "**** Finish parsing #{league_uri}"
    end
    self.new(options)
  end
end

.fetch_competition_leagues(competition_uri) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/transfermarkt/league.rb', line 94

def self.fetch_competition_leagues(competition_uri)
  puts "Fetching #{competition_uri}"
  req = self.get(competition_uri, headers: {"User-Agent" => UserAgents.rand()})
  league_uris = []
  if req.code != 200
    []
  else
    competition_html = Nokogiri::HTML(req.parsed_response)
    league_uris << competition_html.xpath('//*[@id="yw1"]//table[@class="items"]//tr//td[2]//a').collect {|league| league["href"] }

    next_page_link = competition_html.xpath('//*[@id="yw2"]//li[@class="naechste-seite"]//a')[0]
    if next_page_link
      link = next_page_link["href"].split("?").first

      page = next_page_link["href"].scan(/page=(\d)/).flatten.first
      league_uris << Transfermarkt::League.fetch_competition_leagues(link + "?page=#{page}")
    else
      league_uris.flatten
    end

    league_uris.flatten
  end
end

.fetch_league_urisObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/transfermarkt/league.rb', line 80

def self.fetch_league_uris
  competition_uris = ["/wettbewerbe/europa",
                      "/wettbewerbe/asien",
                      "/wettbewerbe/amerika",
                      "/wettbewerbe/afrika"]

  all_leagues = []
  competition_uris.each do |competition_uri|
    all_leagues << Transfermarkt::League.fetch_competition_leagues(competition_uri)
  end

  all_leagues.flatten
end

Instance Method Details

#valid_league?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/transfermarkt/league.rb', line 10

def valid_league?
  if name.nil? or name.empty?
    false
  else
    true
  end
end