Class: SportsDb::MediaBuilder

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

Class Method Summary collapse

Class Method Details

.update_sn_videoObject



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
29
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
# File 'lib/sports_db/media_builder.rb', line 4

def self.update_sn_video
    require 'open-uri'
    config = SimpleConfig.for(:feeds)
    
    config.sn_video_feeds.each do |feed_title, url|
        p "Video - #{url}"	
        open( url ) do |file|
            doc = Nokogiri::XML(file.read)
            
            nodes = doc.xpath('//item')
            media_nodes = doc.xpath('/rss/channel/item/media:content', 'media' => 'http://search.yahoo.com/mrss/')
                                             
            if media_nodes.length > 0
                Media.delete_all( "media_type = 'sportingnews_video'")
    
                nodes.each do |entry|
                    high_bandwidth_node = entry.at('.//media:content[@medium="video"]', 'media' => 'http://search.yahoo.com/mrss/')
                    high_bandwidth_url  = high_bandwidth_node.nil? ? '' : high_bandwidth_node['url']
                    low_bandwidth_url   = high_bandwidth_url
                    
                    image_url_node = entry.at('./media:thumbnail', 'media' => 'http://search.yahoo.com/mrss/')
                    if image_url_node.nil?
                        image_url = ""
                        thumb_url = ""
                    else
                        image_url = image_url_node['url']
                        thumb_url = CONFIG.image_service + "crop/w/55/h/55/url/#{CGI::escape(CGI::escape(image_url))}"
                    end
                              
                    p "Video added - #{entry.xpath('title').text}"

                    Media.create(
                        :feed_title => feed_title,
                        :title => entry.xpath('title').text,
                        :description => entry.xpath('content:encoded', 'content' => 'http://purl.org/rss/1.0/modules/content/').text,
                        :contents => entry.xpath('content:encoded', 'content' => 'http://purl.org/rss/1.0/modules/content/').text,
                        :high_bandwidth_url => high_bandwidth_url,
                        :low_bandwidth_url => low_bandwidth_url,
                        :media_type => 'sportingnews_video',
                        :thumb_image_url => thumb_url,
                        :article_image_url => image_url,
                        :published_at => DateTime.parse(entry.xpath('pubDate').text)
                    )
                end
            else
                Rails.logger.info("No URLs available - Did not update videos")
            end
        end
    end
rescue Exception => e
    Zumobi::ExceptionHandler.error e
end