module SportsDb class TwitterBuilder def self.update_twitter config = SimpleConfig.for(:feeds) config.twitter_feeds.each do |feed_title, url| parse_feed(url, feed_title) end end def self.update_ncaa_twitter config = SimpleConfig.for(:feeds) config.twitter_feeds.each do |feed_title, url| parse_feed(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 team_obj = Team.find(feed_obj.team_id) feed_name = (team_obj.nil? ? "Unknown" : team_obj.city_name) parse_feed(feed_url, feed_name, feed_obj.team_id) end rescue Exception => e Zumobi::ExceptionHandler.error e end def self.parse_feed(url, feed_title, team_id=nil) require 'open-uri' p "Twitter - #{feed_title} - #{url}" new_tweets = [] begin open(url) do |file| doc = Nokogiri::XML(file.read) source = doc.xpath('/rss/channel/title').text source = source.gsub('Twitter', '').strip source = title_changes(source) 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('>', '>') 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 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 def self.title_changes(source) titles = {'Official NFL Twitter Feed' => 'NFL', 'The Trenches Twitter' => 'The Trenches'} return (titles[source].nil?) ? source : titles[source] end def self.add_hash_highlight(str) formatted_text = [] each_word = str.split(' ') each_word.each do |word| if word[0,7] == "http://" new_word = "#{word}" formatted_text << new_word elsif word.include? "http://" phrase = word pieces = word.split("http://") phrase = "#{pieces[0]} http://#{pieces[1]}" formatted_text << phrase else formatted_text << word end end formatted_text.join(' ') end 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 end end