Class: SportsDb::SportingNewsFeedBuilder
- Inherits:
-
Object
- Object
- SportsDb::SportingNewsFeedBuilder
- Defined in:
- lib/sports_db/sporting_news_feed_builder.rb
Class Method Summary collapse
- .parse_webgen_feeds(rss, feed_key, url, list_type, feed_title) ⇒ Object
- .remove_by_news_filter(news_filter) ⇒ Object
- .remove_dup_and_old_articles ⇒ Object
- .remove_flash_elements(article) ⇒ Object
- .retrieve_feed(url) ⇒ Object
- .send_notification(article, team_key, notify_type) ⇒ Object
-
.send_push_notification(alert_text, tags, digest) ⇒ Object
Sends a notification for the specified app / notification class combo.
- .set_article_filter_association(article, feed_key) ⇒ Object
- .set_fantasy_article_filter(article, feed_key) ⇒ Object
- .update_news_feed ⇒ Object
- .update_webgen_feeds(feed_list, list_type) ⇒ Object
Class Method Details
.parse_webgen_feeds(rss, feed_key, url, list_type, feed_title) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 81 def self.parse_webgen_feeds(rss, feed_key, url, list_type, feed_title) p "News - #{feed_title} - #{url}" article_count = 0 doc = Nokogiri::XML(rss) source = "Sporting News" if !doc.nil? doc.xpath('//item').each do |node| article_count += 1 if feed_key == CONFIG.affiliation_key article_link = node.xpath('link').text if !article_link.match(CONFIG.sn_breaking_news_match) next end end guid = node.xpath('guid').text item_title = node.xpath('title').text if Article.find_by_digest(guid).nil? && Article.find_by_title(item_title).nil? article_obj = Article.new article_obj.title = node.xpath('title').text article_obj.published_at = node.xpath('pubDate').text article_obj.link = node.xpath('link').text article_obj.digest = guid article_obj.source = source if list_type == "team_news" || list_type == "team_news_no_filters" article_obj.category = "Team News" elsif list_type == "breaking" || list_type == "news" article_obj.category = "League News" elsif list_type == "fantasy" article_obj.category = "Fantasy News" else article_obj.category = "News" end if guid.blank? article_obj.set_digest end if list_type == "breaking" article_obj. = (!node.xpath('author').nil?) ? node.xpath('author').text : "" article_obj.contents = (!node.xpath('description').nil?) ? node.xpath('description').text.strip : "" else article_obj. = (!node.xpath('dc:creator').nil?) ? node.xpath('dc:creator').text : "" article_obj.contents = (!node.xpath('content:encoded').nil?) ? node.xpath('content:encoded').text.strip : "" thumb_image = (!node.xpath('media:thumbnail').nil?) ? node.xpath('media:thumbnail/@url').text : "" if !thumb_image.blank? if CONFIG.affiliation_key == 'l.nfl.com' article_obj.thumb_image_url = CONFIG.image_service + 'crop/w/110/url/' + CGI::escape(CGI::escape(thumb_image)) article_obj.article_image_url = CONFIG.image_service + 'transform/w/480/h/480/url/' + CGI::escape(CGI::escape(thumb_image)) else article_obj.thumb_image_url = CONFIG.image_service + 'crop/w/55/url/' + CGI::escape(CGI::escape(thumb_image)) article_obj.article_image_url = CONFIG.image_service + 'transform/w/280/h/280/url/' + CGI::escape(CGI::escape(thumb_image)) end end end if article_obj.title.blank? || article_obj.title == "." || article_obj.contents.blank? || article_obj.contents == "." next end if list_type == "fantasy" set_fantasy_article_filter(article_obj, feed_key) else set_article_filter_association(article_obj, feed_key) end article_obj.save else Article.transaction do article_obj = Article.find_by_digest(guid) if article_obj.nil? article_obj = Article.find_by_title(item_title) end article_obj.title = node.xpath('title').text article_obj.published_at = node.xpath('pubDate').text article_obj.link = node.xpath('link').text if list_type == "breaking" article_obj. = (!node.xpath('author').nil?) ? node.xpath('author').text : "" article_obj.contents = (!node.xpath('description').nil?) ? node.xpath('description').text.strip : "" else article_obj. = (!node.xpath('dc:creator').nil?) ? node.xpath('dc:creator').text : "" article_obj.contents = (!node.xpath('content:encoded').nil?) ? node.xpath('content:encoded').text.strip : "" thumb_image = (!node.xpath('media:thumbnail').nil?) ? node.xpath('media:thumbnail/@url').text : "" if !thumb_image.blank? if CONFIG.affiliation_key == 'l.nfl.com' article_obj.thumb_image_url = CONFIG.image_service + 'crop/w/110/url/' + CGI::escape(CGI::escape(thumb_image)) article_obj.article_image_url = CONFIG.image_service + 'transform/w/480/h/480/url/' + CGI::escape(CGI::escape(thumb_image)) else article_obj.thumb_image_url = CONFIG.image_service + 'crop/w/55/url/' + CGI::escape(CGI::escape(thumb_image)) article_obj.article_image_url = CONFIG.image_service + 'transform/w/280/h/280/url/' + CGI::escape(CGI::escape(thumb_image)) end end end if list_type == "fantasy" set_fantasy_article_filter(article_obj, feed_key) else set_article_filter_association(article_obj, feed_key) end article_obj.save end end p "Article - #{article_obj.title}" remove_flash_elements(article_obj) if (list_type == "team_news" || list_type == "breaking") && CONFIG.enable_notifications if list_type == "team_news" if Team.column_names.include?("tsn_key") team = Team.find_by_key(feed_key) notify_feed_key = team.tsn_key else notify_feed_key = feed_key end send_notification(article_obj, notify_feed_key, list_type) else send_notification(article_obj, feed_key, list_type) end end end p "#{article_count} articles processed" end end |
.remove_by_news_filter(news_filter) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 382 def self.remove_by_news_filter(news_filter) if !news_filter.nil? article_titles = {} team_articles = news_filter.articles.find(:all) team_articles.each do |a| article_digest = Digest::SHA1.hexdigest(a.sn_url) if article_titles[article_digest].blank? article_titles[article_digest] = a p "---- #{a.title}" else p "Dup! - #{a.title}" this_article = a saved_article = article_titles[article_digest] if this_article.published_at > saved_article.published_at article_titles[article_digest] = this_article saved_article.destroy else this_article.destroy end end end end end |
.remove_dup_and_old_articles ⇒ Object
368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 368 def self.remove_dup_and_old_articles() teams = Team.find(:all, :conditions => ["division is not null"]) teams.each do |t| p "#{t.city_name} #{t.team_name}" news_filter = NewsFilter.find(:first, :conditions => ["news_filter_key = ?", t.key]) remove_by_news_filter(news_filter) end p "All" news_filter = NewsFilter.find(:first, :conditions => ["news_filter_key = ?", "all"]) remove_by_news_filter(news_filter) end |
.remove_flash_elements(article) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 274 def self.remove_flash_elements(article) doc = Nokogiri::HTML(article.contents) objects = doc.search("embed") objects.each do |obj| obj.remove end contents = doc.children[1].to_html contents = contents.gsub("<html><body>\n", '') contents = contents.gsub('</body></html>', '') article.contents = contents article.save end |
.retrieve_feed(url) ⇒ Object
362 363 364 365 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 362 def self.retrieve_feed(url) body = make_request(url) body end |
.send_notification(article, team_key, notify_type) ⇒ Object
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 289 def self.send_notification(article, team_key, notify_type) #checks to see if the article is new enough #checks if notification has been sent yet. if there's a row in the notifications table, then it's been sent if !article.nil? && !article.digest.blank? n = Notification.find(:first, :conditions => ['title = ? and category = ?', article.title, team_key]) if (n.nil? && article.published_at > CONFIG.send_notifications_since.call()) n = Notification.new n.title = article.title n.article_digest = article.digest n.category = team_key n.article_id = article.id n.article_link = article.link n.notify_sent = 0 if team_key != CONFIG.affiliation_key team = Team.find_by_tsn_key(team_key) if !team.nil? n.team_id = team.id end end n.save elsif (!n.nil? && n.notify_sent == 0 && article.published_at > CONFIG.send_notifications_since.call()) #putting a delay in for sending out notify rather than sending right when we get the article. #i'm sure that there's a caching issue between the backend and front end. hopefully this will help work that out. alert_title = article.title[0,90] if article.title.length > 90 alert_title += "..." end n.notify_sent = 1 n.save if notify_type == "team_news" && !n.team_id.nil? send_push_notification(alert_title, [team_key], article.digest) elsif notify_type == "breaking" send_push_notification(alert_title, [team_key], article.digest) end else if (!n.nil?) #Rails.logger.info("Already sent (Notification_id: #{n.id})") else #Rails.logger.info("Article too old to notify (#{article.digest})") end end end end |
.send_push_notification(alert_text, tags, digest) ⇒ Object
Sends a notification for the specified app / notification class combo.
Parameters: application_name - The name of the application the notification is intended for. tag - data goes into “tags” field alert - The text to display in the notification. digest - An additional field added for NFL - this is the article id. tag_key - An additional field added for NFL - this is the tag that the notifs is subscribed to team or league. sound (optional) - Whether or not to play a sound when the notification is sent. A value of ÔøΩtrueÔøΩ, ÔøΩ1ÔøΩ, or ÔøΩyesÔøΩ means to play the sound. Any other values will not play a sound.
349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 349 def self.send_push_notification(alert_text, , digest) p "--Notified! '#{alert_text[0,50]}' at #{DateTime.now.to_s}: Tag: #{}" notifier = Zumobi::Notifier.new notifier.push({ :aps => {:alert => alert_text, :sound => "1"}, :android => {:alert => alert_text, :extra => "#{.join(',')}|#{digest}"}, :tags => , :tag_key => .join(','), :digest => digest }) end |
.set_article_filter_association(article, feed_key) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 217 def self.set_article_filter_association(article, feed_key) #all if !article.news_filters.find(:first, :conditions => "news_filter_key = 'all'") filter = NewsFilter.find(:first, :conditions => ["news_filter_key = 'all'"]) filter.articles << article filter.save end if feed_key != CONFIG.affiliation_key #team if Team.column_names.include?("tsn_key") && feed_key.to_s.match(CONFIG.affiliation_key) feed_key = tsn_keys_to_stats_key(feed_key) end team = Team.find_by_key(feed_key) if !team.nil? if !article.news_filters.find(:first, :conditions => ["news_filter_key = ?", team.key]) filter_team = NewsFilter.find(:first, :conditions => ["news_filter_key = ?", team.key]) if !filter_team.nil? filter_team.articles << article end end #conference if CONFIG.affiliation_key.match('l.ncaa.org') conference_key = team.conference.key else conference_key = team.conference end if !article.news_filters.find(:first, :conditions => ["news_filter_key = ?", conference_key]) filter_conference = NewsFilter.find(:first, :conditions => ["news_filter_key = ?", conference_key]) if !filter_conference.nil? filter_conference.articles << article end end end else #league if !article.news_filters.find(:first, :conditions => ["news_filter_key = ?", feed_key]) filter_conference = NewsFilter.find(:first, :conditions => ["news_filter_key = ?", feed_key]) if !filter_conference.nil? filter_conference.articles << article end end end end |
.set_fantasy_article_filter(article, feed_key) ⇒ Object
264 265 266 267 268 269 270 271 272 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 264 def self.set_fantasy_article_filter(article, feed_key) if !article.news_filters.find(:first, :conditions => "news_filter_key = 'fantasy'") filter = NewsFilter.find(:first, :conditions => ["news_filter_key = 'fantasy'"]) if !filter.nil? filter.articles << article filter.save end end end |
.update_news_feed ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 4 def self.update_news_feed config_feeds = SimpleConfig.for(:feeds) Article.transaction do # #delete existing articles older than 30 days # remove_date = TimeService.now - 30.day # sn_articles_old = Article.find(:all, :conditions => ["source = 'Sporting News' and published_at > ?", remove_date]) # sn_articles_old.each {|article| article.destroy } update_webgen_feeds(config_feeds.news_feeds, "news") update_webgen_feeds(config_feeds.breaking_news_feeds, "breaking") if CONFIG.affiliation_key == 'l.nfl.com' update_webgen_feeds(config_feeds.team_feeds, "pro_team_news") elsif CONFIG.affiliation_key == 'l.nba.com' update_webgen_feeds(config_feeds.team_feeds, "pro_team_news") elsif CONFIG.affiliation_key == 'l.mlb.com' update_webgen_feeds(config_feeds.team_feeds, "pro_team_news") update_webgen_feeds(config_feeds.fantasy_news_feeds, "fantasy") elsif CONFIG.affiliation_key.match('l.ncaa.org') update_webgen_feeds(ExternalFeed.find(:all, :conditions => ["content_type = ? and provider = ?", "news", "Sporting News"]), "ncaa_team_news") end end end |
.update_webgen_feeds(feed_list, list_type) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 72 73 74 75 76 77 78 79 |
# File 'lib/sports_db/sporting_news_feed_builder.rb', line 30 def self.update_webgen_feeds(feed_list, list_type) if list_type == "ncaa_team_news" feed_list.each do |feed_obj| feed_team = Team.find(feed_obj.team_id) if !feed_team.nil? feed_url = feed_obj.woven_feed_url if CONFIG.woven_feed_server != "woven.zumobi.net" feed_url = feed_url.gsub("woven.zumobi.net", CONFIG.woven_feed_server) end list_type = "team_news" rss = retrieve_feed(feed_url) parse_webgen_feeds(rss, feed_team.key, feed_url, list_type, feed_team.city_name) end end else feed_list.each do |feed_title, url| if list_type == "pro_team_news" feed_key = feed_title if Team.column_names.include?("tsn_key") && feed_key.to_s.match(CONFIG.affiliation_key) feed_key = tsn_keys_to_stats_key(feed_key) end team = Team.find_by_key(feed_key) feed_title = (!team.nil?) ? team.city_name : feed_title elsif list_type == "breaking" feed_key = CONFIG.affiliation_key elsif list_type == "news" feed_key = CONFIG.affiliation_key elsif list_type == "fantasy" feed_key = "fantasy" end if !feed_key.blank? rss = retrieve_feed(url) if list_type == "pro_team_news" parse_list_type = "team_news" else parse_list_type = list_type end parse_webgen_feeds(rss, feed_key, url, parse_list_type, feed_title) end end end rescue Exception => e Zumobi::ExceptionHandler.error e end |