espn-fantasy-news 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ \#*
2
+ \.#*
3
+ log/*
4
+ config/s3.yml
5
+ .autotest
6
+ vendor/bundler_gems
7
+ .bundle/*
8
+ db/development.sqlite3
9
+ /pkg
data/README ADDED
@@ -0,0 +1 @@
1
+ This is a library for loading in the espn fantasy football player universe, and parsing news updates for each player.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "espn-fantasy-news"
5
+ gemspec.summary = "Scrapes ESPN Fantasy football news updates and player data"
6
+ gemspec.description = "ESPN fantasy football has a list of players in their Universe, and it provides news updates on said players. This is a utility library used to scrape the data for use in other applications"
7
+ gemspec.email = "petkanics@gmail.com"
8
+ gemspec.homepage = "http://github.com/dob/espn_fantasy_news"
9
+ gemspec.authors = ["Doug Petkanics"]
10
+ end
11
+ Jeweler::GemcutterTasks.new
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: gem install jeweler"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{espn-fantasy-news}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Doug Petkanics"]
12
+ s.date = %q{2010-09-16}
13
+ s.description = %q{ESPN fantasy football has a list of players in their Universe, and it provides news updates on said players. This is a utility library used to scrape the data for use in other applications}
14
+ s.email = %q{petkanics@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "README",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "espn-fantasy-news.gemspec",
24
+ "lib/espn_fantasy_news.rb",
25
+ "lib/espn_fantasy_news/news.rb",
26
+ "lib/espn_fantasy_news/parser.rb",
27
+ "lib/espn_fantasy_news/player.rb",
28
+ "test/suite.rb",
29
+ "test/test_parser.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/dob/espn_fantasy_news}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Scrapes ESPN Fantasy football news updates and player data}
36
+ s.test_files = [
37
+ "test/suite.rb",
38
+ "test/test_parser.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
51
+
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'nokogiri'
4
+
5
+ module ESPNFantasyNews
6
+
7
+ PLAYER_LIST_ENDPOINT = "http://games.espn.go.com/ffl/tools/projections"
8
+ NEWS_ENDPOINT = "http://games.espn.go.com/ffl/resources/playernews"
9
+
10
+ end
11
+
12
+ directory = File.expand_path(File.dirname(__FILE__))
13
+
14
+ require File.join(directory, 'espn_fantasy_news', 'player')
15
+ require File.join(directory, 'espn_fantasy_news', 'news')
16
+ require File.join(directory, 'espn_fantasy_news', 'parser')
@@ -0,0 +1,11 @@
1
+ module ESPNFantasyNews
2
+ class News
3
+ attr_reader :espn_player_id, :text
4
+
5
+ def initialize(player_id, text)
6
+ @espn_player_id = player_id
7
+ @text = text
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,83 @@
1
+ require 'open-uri'
2
+
3
+ module ESPNFantasyNews
4
+
5
+ class Parser
6
+
7
+ def self.load_all_players
8
+ offset = 0
9
+ res = []
10
+ while offset <= 1080 do
11
+ url = ESPNFantasyNews::PLAYER_LIST_ENDPOINT + "?startIndex=#{offset.to_s}"
12
+ res += self.players_from_url(url)
13
+ offset += 40
14
+ end
15
+ res
16
+ end
17
+
18
+ def self.players_from_url(url)
19
+ doc = Nokogiri::HTML(open(url))
20
+ player_attributes = doc.css('.pncPlayerRow').collect do |x|
21
+ cell = x.css('td')[1]
22
+ cell_text = cell.text
23
+ name, teampos = cell_text.split(',')
24
+ team, pos = self.split_string_on_char_194(teampos.strip)
25
+ player_id = cell.css('a')[0].get_attribute('playerid').to_i
26
+
27
+ pos = self.parse_pos(pos)
28
+
29
+ # Remove the asterisk from players who are on the IR
30
+ name = name[0, name.length - 1] if name[-1, 1] == "*"
31
+
32
+ ESPNFantasyNews::Player.new(name, player_id, pos, team)
33
+ end
34
+ player_attributes
35
+ end
36
+
37
+ def self.load_all_news
38
+ self.news_from_url(ESPNFantasyNews::NEWS_ENDPOINT)
39
+ end
40
+
41
+ def self.news_from_url(url)
42
+ doc = Nokogiri::HTML(open(url))
43
+ news_attributes = doc.css('tr.tableBody').collect do |player_row|
44
+ player_id = player_row.css('a')[0].get_attribute('playerid').to_i
45
+ text = player_row.css('.pni-shorttext')[0].content
46
+
47
+ ESPNFantasyNews::News.new(player_id, text)
48
+ end
49
+ news_attributes
50
+ end
51
+
52
+ private
53
+
54
+ # It isn't a space that seperates the team and position, it's ascii char 194 for some reason?
55
+ def self.split_string_on_char_194(str)
56
+ return str if (str.nil? || str.length < 4)
57
+
58
+ if str[2].to_i == 194
59
+ return str[0,2], str[4, str.length - 4]
60
+ elsif str[3].to_i == 194
61
+ return str[0,3], str[5, str.length - 4]
62
+ end
63
+ end
64
+
65
+ def self.parse_pos(pos)
66
+ self.positions[pos[0,1]]
67
+ end
68
+
69
+ # Garbage characters get included if there's an injury, so just use the first letter
70
+ def self.positions
71
+ {
72
+ "Q" => "QB",
73
+ "R" => "RB",
74
+ "W" => "WR",
75
+ "T" => "TE",
76
+ "K" => "K",
77
+ "D" => "D/ST"
78
+ }
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,17 @@
1
+ module ESPNFantasyNews
2
+ class Player
3
+ attr_reader :name, :espn_player_id, :position, :team
4
+
5
+ def initialize(name, player_id, position, team)
6
+ @name = name
7
+ @espn_player_id = player_id
8
+ @position = position
9
+ @team = team
10
+ end
11
+
12
+ def to_s
13
+ "#{self.name} - #{self.position} #{self.team}, id #{self.espn_player_id.to_s}"
14
+ end
15
+
16
+ end
17
+ end
data/test/suite.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+
3
+ tests = Dir["#{File.dirname(__FILE__)}/test_*.rb"]
4
+ tests.each do |file|
5
+ require file
6
+ end
@@ -0,0 +1,23 @@
1
+ require 'test/unit'
2
+ require 'espn_fantasy_news'
3
+
4
+ class ParserTest < Test::Unit::TestCase
5
+
6
+ def test_parse_players
7
+ players = ESPNFantasyNews::Parser.players_from_url(ESPNFantasyNews::PLAYER_LIST_ENDPOINT)
8
+ assert players != nil && players != []
9
+ assert players.length == 40
10
+ end
11
+
12
+ def test_parse_news
13
+ news = ESPNFantasyNews::Parser.news_from_url(ESPNFantasyNews::NEWS_ENDPOINT)
14
+ assert news != nil && news != []
15
+ end
16
+
17
+ def test_load_all_players
18
+ players = ESPNFantasyNews::Parser.load_all_players
19
+ assert players != [] && players != nil
20
+ assert_equal players.length, 1087
21
+ end
22
+
23
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: espn-fantasy-news
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Doug Petkanics
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-09-16 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ESPN fantasy football has a list of players in their Universe, and it provides news updates on said players. This is a utility library used to scrape the data for use in other applications
17
+ email: petkanics@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - .gitignore
26
+ - README
27
+ - Rakefile
28
+ - VERSION
29
+ - espn-fantasy-news.gemspec
30
+ - lib/espn_fantasy_news.rb
31
+ - lib/espn_fantasy_news/news.rb
32
+ - lib/espn_fantasy_news/parser.rb
33
+ - lib/espn_fantasy_news/player.rb
34
+ - test/suite.rb
35
+ - test/test_parser.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/dob/espn_fantasy_news
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --charset=UTF-8
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Scrapes ESPN Fantasy football news updates and player data
64
+ test_files:
65
+ - test/suite.rb
66
+ - test/test_parser.rb