Class: Transfermarkt::League
- Inherits:
-
EntityBase
- Object
- EntityBase
- Transfermarkt::League
- Defined in:
- lib/transfermarkt/league.rb
Instance Attribute Summary collapse
-
#club_uris ⇒ Object
Returns the value of attribute club_uris.
-
#clubs ⇒ Object
Returns the value of attribute clubs.
-
#clubs_index ⇒ Object
Returns the value of attribute clubs_index.
-
#country ⇒ Object
Returns the value of attribute country.
-
#league_uri ⇒ Object
Returns the value of attribute league_uri.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
- .fetch_by_league_uri(league_uri, fetch_clubs = false) ⇒ Object
- .fetch_clubs_and_uris_by_league_uri(league_uri) ⇒ Object
- .fetch_competition_leagues(competition_uri) ⇒ Object
- .fetch_league_uris ⇒ Object
Instance Method Summary collapse
Methods inherited from EntityBase
Constructor Details
This class inherits a constructor from Transfermarkt::EntityBase
Instance Attribute Details
#club_uris ⇒ Object
Returns the value of attribute club_uris.
3 4 5 |
# File 'lib/transfermarkt/league.rb', line 3 def club_uris @club_uris end |
#clubs ⇒ Object
Returns the value of attribute clubs.
3 4 5 |
# File 'lib/transfermarkt/league.rb', line 3 def clubs @clubs end |
#clubs_index ⇒ Object
Returns the value of attribute clubs_index.
3 4 5 |
# File 'lib/transfermarkt/league.rb', line 3 def clubs_index @clubs_index end |
#country ⇒ Object
Returns the value of attribute country.
3 4 5 |
# File 'lib/transfermarkt/league.rb', line 3 def country @country end |
#league_uri ⇒ Object
Returns the value of attribute league_uri.
3 4 5 |
# File 'lib/transfermarkt/league.rb', line 3 def league_uri @league_uri end |
#name ⇒ Object
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) = {} [:league_uri] = league_uri [:name] = league_html.xpath('//select[@id="wettbewerb_select_breadcrumb"]//option[@selected="selected"]')[0].text [:country] = league_html.xpath('//select[@id="land_select_breadcrumb"]//option[@selected="selected"]').text [:club_uris] = league_html.xpath('//*[@id="yw1"]//table//tr//td[2]//a[1]').collect{|player_html| player_html["href"]} puts "Found #{[:club_uris].count} clubs" [:clubs] = [] if fetch_clubs [:club_uris].each do |club_uri| [:clubs] << Transfermarkt::Club.fetch_by_club_uri(club_uri, fetch_clubs) end end puts "fetched league clubs for #{[:name]}" self.new() 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) = {} puts "**** Parsing league #{league_uri}" [: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? [:name] = league_html.xpath('//div[@class="spielername-profil"]').text.strip [:country] = league_html.xpath('//table[@class="profilheader"]//img/@title').first.value else [:name] = league_name[0].text [: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)] [:clubs_index] = clubs puts "**** Finish parsing #{league_uri}" end self.new() 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_uris ⇒ Object
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
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 |