elisehuard-media_feed 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/media_feed.rb +14 -6
- data/spec/media_feed_spec.rb +2 -2
- metadata +1 -1
data/lib/media_feed.rb
CHANGED
@@ -44,6 +44,8 @@ end
|
|
44
44
|
class Feed
|
45
45
|
include(LibXML)
|
46
46
|
|
47
|
+
MEDIA_RSS_NAMESPACE = 'media:http://search.yahoo.com/mrss'
|
48
|
+
|
47
49
|
attr_reader :title, :description, :thumbnail, :pubDate, :items
|
48
50
|
|
49
51
|
# Feed is initialized with a valid url for the feed itself.
|
@@ -99,7 +101,7 @@ private
|
|
99
101
|
channel = all_nodes[0]
|
100
102
|
@title = content(channel,'title')
|
101
103
|
@description = content(channel,'description')
|
102
|
-
@thumbnail = content(channel,'image/url')
|
104
|
+
@thumbnail = content(channel,'image/url') || content(channel,'media:thumbnail')
|
103
105
|
@pubDate = content(channel,'pubDate')
|
104
106
|
if @pubDate && @last_date
|
105
107
|
feed_pub_date = parse_date(@pubDate)
|
@@ -107,6 +109,7 @@ private
|
|
107
109
|
end
|
108
110
|
items = channel.find('//item')
|
109
111
|
|
112
|
+
item_pubDate = nil # latest item date
|
110
113
|
@items = items.inject([]) do |result,node|
|
111
114
|
item = parse_item(node)
|
112
115
|
if !item.valid? # invalid items shouldn't be added to feed but shouldn't stop the rest of the feed
|
@@ -114,9 +117,11 @@ private
|
|
114
117
|
puts item.to_s
|
115
118
|
else
|
116
119
|
result << item if item.is_after?(@last_date)
|
120
|
+
item_pubDate = item.pubDate if !item.pubDate.nil? && (item_pubDate.nil? || item.pubDate > item_pubDate)
|
117
121
|
end
|
118
122
|
result
|
119
123
|
end
|
124
|
+
@pubDate = item_pubDate if @pubDate.nil?
|
120
125
|
end
|
121
126
|
|
122
127
|
def parse_document(doc)
|
@@ -137,7 +142,9 @@ private
|
|
137
142
|
item.enclosure = url(node,'enclosure') || url(node,'media:content') || content(node,'guid')
|
138
143
|
date = content(node,'pubDate')
|
139
144
|
item.pubDate = parse_date(date) if date && !date.empty?
|
140
|
-
item.thumbnail = content(node,'image/url')
|
145
|
+
item.thumbnail = content(node,'image/url') || content(node,'media:thumbnail')
|
146
|
+
keywords = content(node,'media:keywords')
|
147
|
+
item.keywords = keywords.gsub(/ */,'').split(',') if keywords
|
141
148
|
|
142
149
|
# pubDate if not filled in by feed -> first (valid) one is probably most recent
|
143
150
|
@pubDate = item.pubDate.rfc2822 if @pubDate.nil? && item.pubDate
|
@@ -145,12 +152,13 @@ private
|
|
145
152
|
end
|
146
153
|
|
147
154
|
def content(parent_node,xpath)
|
148
|
-
|
155
|
+
result_xpath = parent_node.find(xpath,MEDIA_RSS_NAMESPACE)
|
156
|
+
result_xpath[0].content if result_xpath && result_xpath[0]
|
149
157
|
end
|
150
158
|
|
151
159
|
def url(parent_node,xpath)
|
152
|
-
|
153
|
-
|
160
|
+
result_xpath = parent_node.find(xpath,MEDIA_RSS_NAMESPACE)
|
161
|
+
result_xpath[0]['url'] if result_xpath && result_xpath[0]
|
154
162
|
end
|
155
163
|
|
156
164
|
# date string should be in rfc2822 format ex. Sat, 06 Sep 2008 11:59:15 +0000
|
@@ -163,7 +171,7 @@ private
|
|
163
171
|
end
|
164
172
|
|
165
173
|
class Item
|
166
|
-
attr_accessor :title, :description, :link, :pubDate, :enclosure, :thumbnail
|
174
|
+
attr_accessor :title, :description, :link, :pubDate, :enclosure, :thumbnail, :keywords
|
167
175
|
|
168
176
|
def valid?
|
169
177
|
if title.nil? || link.nil? || enclosure.nil?
|
data/spec/media_feed_spec.rb
CHANGED
@@ -83,9 +83,9 @@ describe MediaFeed::Feed, '.fetch_since' do
|
|
83
83
|
feed.fetch
|
84
84
|
date = feed.pubDate
|
85
85
|
feed2 = MediaFeed::Feed.new('http://feeds.feedburner.com/tedtalks_video')
|
86
|
-
|
86
|
+
feed2.stubs(:get_feed).returns(stub_response('tedtalks_video'))
|
87
87
|
feed2.fetch_since(date)
|
88
|
-
feed2.items.should
|
88
|
+
feed2.items.should have(0).items
|
89
89
|
end
|
90
90
|
|
91
91
|
it 'should only fetch the items that are new since last fetch if last date is given' do
|