Class: FootballNow::Scraper

Inherits:
Object
  • Object
show all
Extended by:
Capybara::DSL
Defined in:
lib/scraper.rb

Constant Summary collapse

BASE_URL =
"http://www.soccer24.com"
LEAGUES =
[
  "Premier League",     "Primera Division",
  "Bundesliga",         "Serie A"
]

Class Method Summary collapse

Class Method Details

.scrape_leaguesObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/scraper.rb', line 11

def self.scrape_leagues
  doc         = FootballNow::DB.get_html(BASE_URL)
  league_list = Nokogiri::HTML(doc).css('.left-menu').first.css('ul li')

  league_list.map do |row|
    href      = row.css('a').attribute('href').value
    league    = row.css('a').text
    team_hash = {name: league, league_url: "#{BASE_URL}#{href}"}

    LEAGUES.include?(league) ? team_hash : nil
  end.compact
end

.scrape_matches(league_url) ⇒ Object



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
# File 'lib/scraper.rb', line 46

def self.scrape_matches(league_url)
  doc          = FootballNow::DB.get_html(get_matches_page_url(league_url))
  matches_page = Nokogiri::HTML(doc)
  rows         = matches_page.css('tbody tr')

  rows.map do |row|
    if row.css('td').first.text[/Round/]
      @@round = row.css('td').first.text.gsub(/Round /, "").to_i
      nil
    elsif row.css('td.time').text.empty?
      nil
    else
      score       = row.css('td.score').text.split(':')
      home_team   = row.css('td.team-home').text.gsub(/[[:space:]]/, ' ').strip
      away_team   = row.css('td.team-away').text.gsub(/[[:space:]]/, ' ').strip
      match_hash  = {
        round:        @@round,
        date:         row.css('td.time').text.strip,
        home_team:    FootballNow::Team.find_by_name(home_team),
        away_team:    FootballNow::Team.find_by_name(away_team),
        home_score:   score[0].gsub(/[[:space:]]/, ' ').strip,
        away_score:   score[1].gsub(/[[:space:]]/, ' ').strip
      }
    end
  end.compact
end

.scrape_teams(league_url) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scraper.rb', line 24

def self.scrape_teams(league_url)
  doc             = FootballNow::DB.get_html(get_standings_page_url(league_url))
  standings_page  = Nokogiri::HTML(doc)
  standings       = standings_page.css('table#table-type-1 tbody tr')
  league          = standings_page.css('.tournament-name').text

  standings.map do |row|
    goals_for_against     = row.css('.goals').first.text.split(':')
    team_hash = {
      name:               row.css('.participant_name .team_name_span').text,
      league:             FootballNow::League.find_by_name(league),
      wins:               row.css('.wins').text,
      draws:              row.css('.draws').text,
      losses:             row.css('.losses').text,
      standing:           row.css('.rank').text.chomp('.'),
      goals_for:          goals_for_against[0],
      goals_against:      goals_for_against[1],
      points:             row.css('.goals')[1].text
    }
  end
end