Class: SportsDb::TwitterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/sports_db/twitter_builder.rb

Class Method Summary collapse

Class Method Details

.add_hash_highlight(str) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/sports_db/twitter_builder.rb', line 119

def self.add_hash_highlight(str)
    formatted_text = []
    each_word = str.split(' ')
    each_word.each do |word|
        if word[0,7] == "http://"
            new_word = "<a href='#{word}'>#{word}</a>"
            formatted_text << new_word
        elsif word.include? "http://"
            phrase = word
            pieces = word.split("http://")

            phrase = "#{pieces[0]} <a href='http://#{pieces[1]}'>http://#{pieces[1]}</a>"
            formatted_text << phrase
        else
            formatted_text << word
        end
    end
    formatted_text.join(' ')
end

.adjust_titles(title) ⇒ Object

Adjust the title for a couple of feeds. Return the original title if it doesn’t need to be adjusted.



109
110
111
112
113
114
115
116
117
# File 'lib/sports_db/twitter_builder.rb', line 109

def self.adjust_titles(title)
    if title == 'Official NFL Twitter Feed'
        return 'NFL'
    elsif title == 'The Trenches Twitter'
        return 'The Trenches'
    else
        return title
    end
end

.parse_feed(new_tweets, url, feed_title, team_id = nil) ⇒ Object



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
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
# File 'lib/sports_db/twitter_builder.rb', line 54

def self.parse_feed(new_tweets, url, feed_title, team_id=nil)
        require 'open-uri'
        p "Twitter - #{feed_title} - #{url}"

        begin
            open(url) do |file|
                doc = Nokogiri::XML(file.read)
                source = doc.xpath('/rss/channel/title').text
                source = adjust_titles(source)
                # this has to come after adjust_titles or it gomers it all up
                source = source.gsub('Twitter', '').strip

                if !doc.nil? && !source.nil?
                    doc.xpath('//item').each do |node|
                        t = Twitter.new
                        t.source = source
                        t.title  = node.xpath('content:encoded').text.strip
                        t.title  = t.title.gsub('&gt;', '>')
                        t.author = node.xpath('dc:creator').text
                        t.thumb_image_url = node.xpath('media:thumbnail/@url').text
                        t.published_at = node.xpath('pubDate').text
                        t.link = node.xpath('link').text
                        t.guid = node.xpath('guid').text
                        t.contents = t.title

                        team_key = team_mapping(t.source)

                         if !team_key.blank?
                            if Team.column_names.include?("tsn_key")  && !Team.find_by_tsn_key(team_key).blank?
                                t.team_id = Team.find_by_tsn_key(team_key).id
                            elsif !Team.find_by_key(team_key).blank?
                                t.team_id = Team.find_by_key(team_key).id
                            end
                        end

                        new_tweets << t
                    end
                end
            end
        rescue OpenURI::HTTPError => http_e
            p 'unable to retrieve feed'
        rescue EOFError
            p 'connection error'
        rescue Errno::ECONNRESET
            p 'connection error'
        rescue Errno::ECONNREFUSED
            p 'connection error'
        end
rescue Exception => e
    Zumobi::ExceptionHandler.error e
end

.team_mapping(source) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sports_db/twitter_builder.rb', line 139

def self.team_mapping(source)

    links = {'New England Patriots'   => 'l.nfl.com-t.4',
             'Buffalo Bills'          => 'l.nfl.com-t.1',
             'Miami Dolphins'         => 'l.nfl.com-t.3',
             'New York Jets'          => 'l.nfl.com-t.5',
             'Denver Broncos'         => 'l.nfl.com-t.12',
             'Kansas City Chiefs'     => 'l.nfl.com-t.13',
             'Oakland Raiders'        => 'l.nfl.com-t.14',
             'San Diego Chargers'     => 'l.nfl.com-t.15',
             'Baltimore Ravens'       => 'l.nfl.com-t.6',
             'Cincinnati Bengals'     => 'l.nfl.com-t.7',
             'Cleveland Browns'       => 'l.nfl.com-t.8',
             'Pittsburgh Steelers'    => 'l.nfl.com-t.10',
             'Houston Texans'         => 'l.nfl.com-t.32',
             'Jacksonville Jaguars'   => 'l.nfl.com-t.9',
             'Tennessee Titans'       => 'l.nfl.com-t.11',
             'Dallas Cowboys'         => 'l.nfl.com-t.18',
             'New York Giants'        => 'l.nfl.com-t.19',
             'Washington Redskins'    => 'l.nfl.com-t.21',
             'Arizona Cardinals'      => 'l.nfl.com-t.17',
             'San Francisco 49ers'    => 'l.nfl.com-t.31',
             'Seattle Seahawks'       => 'l.nfl.com-t.16',
             'St. Louis Rams'         => 'l.nfl.com-t.28',
             'Chicago Bears'          => 'l.nfl.com-t.22',
             'Detroit Lions'          => 'l.nfl.com-t.23',
             'Green Bay Packers'      => 'l.nfl.com-t.24',
             'Minnesota Vikings'      => 'l.nfl.com-t.25',
             'Atlanta Falcons'        => 'l.nfl.com-t.27',
             'Carolina Panthers'      => 'l.nfl.com-t.29',
             'New Orleans Saints'     => 'l.nfl.com-t.30',
             'Tampa Bay Bucs'         => 'l.nfl.com-t.26',
             'Arizona Diamondbacks'   => 'l.mlb.com-t.26',
             'Atlanta Braves'         => 'l.mlb.com-t.15',
             'Baltimore Orioles'      => 'l.mlb.com-t.1',
             'Boston Red Sox'         => 'l.mlb.com-t.2',
             'Chicago Cubs'           => 'l.mlb.com-t.20',
             'Chicago White Sox'      => 'l.mlb.com-t.6',
             'Cincinnati Reds'        => 'l.mlb.com-t.21',
             'Cleveland Indians'      => 'l.mlb.com-t.7',
             'Colorado Rockies'       => 'l.mlb.com-t.27',
             'Detroit Tigers'         => 'l.mlb.com-t.8',
             'Miami Marlins'          => 'l.mlb.com-t.16',
             'Houston Astros'         => 'l.mlb.com-t.22',
             'Kansas City Royals'     => 'l.mlb.com-t.9',
             'Los Angeles Angels'     => 'l.mlb.com-t.11',
             'Los Angeles Dodgers'    => 'l.mlb.com-t.28',
             'Milwaukee Brewers'      => 'l.mlb.com-t.23',
             'Minnesota Twins'        => 'l.mlb.com-t.10',
             'New York Mets'          => 'l.mlb.com-t.18',
             'New York Yankees'       => 'l.mlb.com-t.3',
             'Oakland Athletics'      => 'l.mlb.com-t.12',
             'Philadelphia Phillies'  => 'l.mlb.com-t.19',
             'Pittsburgh Pirates'     => 'l.mlb.com-t.24',
             'San Diego Padres'       => 'l.mlb.com-t.29',
             'San Francisco Giants'   => 'l.mlb.com-t.30',
             'Seattle Mariners'       => 'l.mlb.com-t.13',
             'St. Louis Cardinals'    => 'l.mlb.com-t.25',
             'Tampa Bay Rays'         => 'l.mlb.com-t.4',
             'Texas Rangers'          => 'l.mlb.com-t.14',
             'Toronto Blue Jays'      => 'l.mlb.com-t.5',
             'Washington Nationals'   => 'l.mlb.com-t.17',
             'Boston Celtics'         => 'l.nba.com-t.1',
             'Brooklyn Nets'          => 'l.nba.com-t.3',
             'New York Knicks'        => 'l.nba.com-t.4',
             'Philadelphia 76ers'     => 'l.nba.com-t.6',
             'Toronto Raptors'        => 'l.nba.com-t.15',
             'Chicago Bulls'          => 'l.nba.com-t.10',
             'Cleveland Cavaliers'    => 'l.nba.com-t.11',
             'Detroit Pistons'        => 'l.nba.com-t.12',
             'Indiana Pacers'         => 'l.nba.com-t.13',
             'Milwaukee Bucks'        => 'l.nba.com-t.14',
             'Atlanta Hawks'          => 'l.nba.com-t.8',
             'Charlotte Bobcats'      => 'l.nba.com-t.32',
             'Miami Heat'             => 'l.nba.com-t.2',
             'Orlando Magic'          => 'l.nba.com-t.5',
             'Washington Wizards'     => 'l.nba.com-t.7',
             'Denver Nuggets'         => 'l.nba.com-t.17',
             'Minnesota Timberwolves' => 'l.nba.com-t.20',
             'Oklahoma City Thunder'  => 'l.nba.com-t.29',
             'Portland Trail Blazers' => 'l.nba.com-t.27',
             'Utah Jazz'              => 'l.nba.com-t.22',
             'Golden State Warriors'  => 'l.nba.com-t.23',
             'Los Angeles Clippers'   => 'l.nba.com-t.24',
             'Los Angeles Lakers'     => 'l.nba.com-t.25',
             'Phoenix Suns'           => 'l.nba.com-t.26',
             'Sacramento Kings'       => 'l.nba.com-t.28',
             'Dallas Mavericks'       => 'l.nba.com-t.16',
             'Houston Rockets'        => 'l.nba.com-t.18',
             'Memphis Grizzlies'      => 'l.nba.com-t.19',
             'New Orleans Hornets'    => 'l.nba.com-t.9',
             'San Antonio Spurs'      => 'l.nba.com-t.21'}

    return (links[source].nil?) ? "" : links[source]
end

.update_ncaa_twitterObject



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
48
49
50
51
# File 'lib/sports_db/twitter_builder.rb', line 19

def self.update_ncaa_twitter
    new_tweets = []

    config = SimpleConfig.for(:feeds)
    config.twitter_feeds.each do |feed_title, url|
        parse_feed(new_tweets, url, feed_title)
    end

    ExternalFeed.find(:all, :conditions => ["content_type = ? and provider = ?", "twitter", "Sporting News"]).each do |feed_obj|
        feed_url = feed_obj.woven_feed_url
        if SimpleConfig.for(:application).woven_feed_server != "woven.zumobi.net"
            feed_url = feed_url.gsub("woven.zumobi.net", SimpleConfig.for(:application).woven_feed_server)
        end
        
        begin
          team_obj  = Team.find_by_id!(feed_obj.team_id)
         feed_name = (team_obj.nil? ? "Unknown" : team_obj.city_name)
      parse_feed(new_tweets, feed_url, feed_name, feed_obj.team_id)
      
  rescue ActiveRecord::RecordNotFound
    p "--- Couldn't update feed, no matching team:  #{feed_url}"
  end
    end

    Twitter.transaction do
        Twitter.delete_all()
        new_tweets.each {|t| t.save}
        p "Tweets saved: #{new_tweets.length}"
    end

rescue Exception => e
    Zumobi::ExceptionHandler.error e
end

.update_twitterObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/sports_db/twitter_builder.rb', line 4

def self.update_twitter
    new_tweets = []

    config = SimpleConfig.for(:feeds)
    config.twitter_feeds.each do |feed_title, url|
        parse_feed(new_tweets, url, feed_title)
    end

    Twitter.transaction do
        Twitter.delete_all()
        new_tweets.each {|t| t.save}
        p "Tweets saved: #{new_tweets.length}"
    end
end