Class: Feed
- Inherits:
-
Object
- Object
- Feed
- Defined in:
- lib/kindle-feeds.rb
Class Method Summary collapse
- .auto_discover_and_subscribe(url) ⇒ Object
- .create_feed(xml, feed_url) ⇒ Object
-
.fetch(url) ⇒ Object
a simple wrapper over open-uri call.
-
.subscribe(feed_url) ⇒ Object
try to repair the URL if possible.
Class Method Details
.auto_discover_and_subscribe(url) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/kindle-feeds.rb', line 166 def self.auto_discover_and_subscribe(url) uri = URI.parse(url) feed_url = Autodiscovery.new(fetch(url)).discover if feed_url feed_url = uri.merge(feed_url).to_s puts "Found feed: #{feed_url}" return feed_url else puts "Can't find feed for #{url}" return nil end end |
.create_feed(xml, feed_url) ⇒ Object
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 |
# File 'lib/kindle-feeds.rb', line 93 def self.create_feed(xml, feed_url) begin feed = FeedNormalizer::FeedNormalizer.parse(xml) return nil unless feed.is_a?(FeedNormalizer::Feed) rescue puts "Error trying to parse feed: #{feed_url}" puts $! puts return nil end # clean up entries: ic = Iconv.new('ISO-8859-1//TRANSLIT', 'utf-8') ic2 = Iconv.new('ISO-8859-1//IGNORE', 'utf-8') #ic = Iconv.new('ASCII//TRANSLIT', 'utf-8') coder = HTMLEntities.new puts "#{feed.entries.size} entries downloaded." puts feed.entries.each do |e| e.title = coder.decode(e.title) e.content = coder.decode(e.content) begin e.title = ic.iconv(e.title) rescue e.title = ic2.iconv(e.title) end begin e.content = ic.iconv(e.content) rescue e.content = ic2.iconv(e.content) end doc = Hpricot(e.content) doc.search('h1, h2, h3') do |h| h.swap("<h4>#{h.inner_text}</h4>") end doc.search('//font') do |font| font.swap(font.inner_text) end doc.search('//img').remove doc.search('svg, object, embed').remove doc.search('script').remove e.content = doc.to_s end return feed end |
.fetch(url) ⇒ Object
a simple wrapper over open-uri call. Easier to mock in testing.
180 181 182 183 184 185 186 187 188 |
# File 'lib/kindle-feeds.rb', line 180 def self.fetch(url) begin open(url).read rescue Timeout::Error puts "-> attempt to fetch #{url} timed out" rescue Exception => e puts "-> error trying to fetch #{url}: #{$!}" end end |
.subscribe(feed_url) ⇒ Object
try to repair the URL if possible
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 |
# File 'lib/kindle-feeds.rb', line 139 def self.subscribe(feed_url) # try to repair the URL if possible unless feed_url =~ /^http:\/\// feed_url = "http://" + feed_url end puts "Downloading #{feed_url}" begin xml = fetch(feed_url) rescue SocketError puts "Error trying to load page at #{feed_url}" return end if xml.nil? puts "Can't find any resource at #{feed_url}" return end feed = Feed.create_feed( xml, feed_url.strip ) if feed.nil? puts "#{feed_url}: Attempting autodiscovery..." feed_url = auto_discover_and_subscribe(feed_url) if feed_url xml = fetch(feed_url) feed = Feed.create_feed( xml, feed_url.strip ) end end feed end |