Class: Pluto::Updater
- Inherits:
-
Object
- Object
- Pluto::Updater
- Includes:
- LogUtils::Logging, Models
- Defined in:
- lib/pluto/updater.rb
Instance Attribute Summary collapse
-
#worker ⇒ Object
readonly
Returns the value of attribute worker.
Instance Method Summary collapse
- #debug=(value) ⇒ Object
- #debug? ⇒ Boolean
- #fetch_feed(url) ⇒ Object
- #handle_feed_item_atom(item) ⇒ Object
- #handle_feed_item_rss(item) ⇒ Object
-
#initialize ⇒ Updater
constructor
A new instance of Updater.
- #parse_feed(xml) ⇒ Object
- #update_feeds(opts = {}) ⇒ Object
- #update_subscriptions(config, opts = {}) ⇒ Object
Constructor Details
#initialize ⇒ Updater
Returns a new instance of Updater.
9 10 11 |
# File 'lib/pluto/updater.rb', line 9 def initialize @worker = ::Fetcher::Worker.new end |
Instance Attribute Details
#worker ⇒ Object (readonly)
Returns the value of attribute worker.
13 14 15 |
# File 'lib/pluto/updater.rb', line 13 def worker @worker end |
Instance Method Details
#debug=(value) ⇒ Object
15 16 17 18 |
# File 'lib/pluto/updater.rb', line 15 def debug=(value) @debug = value ### logger.debug "[Updater] setting debug flag - debug? #{debug?}" end |
#debug? ⇒ Boolean
20 21 22 |
# File 'lib/pluto/updater.rb', line 20 def debug? @debug || false end |
#fetch_feed(url) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pluto/updater.rb', line 24 def fetch_feed( url ) xml = worker.read( url ) ### # NB: Net::HTTP will NOT set encoding UTF-8 etc. # will mostly be ASCII # - try to change encoding to UTF-8 ourselves logger.debug "xml.encoding.name (before): #{xml.encoding.name}" ##### # NB: ASCII-8BIT == BINARY == Encoding Unknown; Raw Bytes Here ## NB: # for now "hardcoded" to utf8 - what else can we do? # - note: force_encoding will NOT change the chars only change the assumed encoding w/o translation xml = xml.force_encoding( Encoding::UTF_8 ) logger.debug "xml.encoding.name (after): #{xml.encoding.name}" xml end |
#handle_feed_item_atom(item) ⇒ Object
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 |
# File 'lib/pluto/updater.rb', line 154 def handle_feed_item_atom( item ) ## todo: if content.content empty use summary for example item_attribs = { title: item.title.content, url: item.link.href, published_at: item.updated.content.utc.strftime( "%Y-%m-%d %H:%M" ), # content: item.content.content, } item_attribs[ :guid ] = item.id.content if item.summary item_attribs[ :content ] = item.summary.content else if item.content text = item.content.content.dup ## strip all html tags text = text.gsub( /<[^>]+>/, '' ) text = text[ 0..400 ] # get first 400 chars ## todo: check for length if > 400 add ... at the end??? item_attribs[ :content ] = text end end puts "- #{item.title.content}" puts " link >#{item.link.href}<" puts " id (~guid) >#{item.id.content}<" ### todo: use/try published first? why? why not? puts " updated (~pubDate) >#{item.updated.content}< >#{item.updated.content.utc.strftime( "%Y-%m-%d %H:%M" )}< : #{item.updated.content.class.name}" puts # puts "*** dump item:" # pp item item_attribs end |
#handle_feed_item_rss(item) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/pluto/updater.rb', line 193 def handle_feed_item_rss( item ) item_attribs = { title: item.title, url: item.link, published_at: item.pubDate.utc.strftime( "%Y-%m-%d %H:%M" ), # content: item.content_encoded, } # if item.content_encoded.nil? # puts " using description for content" item_attribs[ :content ] = item.description # end item_attribs[ :guid ] = item.guid.content puts "- #{item.title}" puts " link (#{item.link})" puts " guid (#{item.guid.content})" puts " pubDate >#{item.pubDate}< >#{item.pubDate.utc.strftime( "%Y-%m-%d %H:%M" )}< : #{item.pubDate.class.name}" puts # puts "*** dump item:" # pp item item_attribs end |
#parse_feed(xml) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pluto/updater.rb', line 45 def parse_feed( xml ) parser = RSS::Parser.new( xml ) parser.do_validate = false parser.ignore_unknown_element = true puts "Parsing feed..." feed = parser.parse puts " feed.class=#{feed.class.name}" feed end |
#update_feeds(opts = {}) ⇒ Object
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 |
# File 'lib/pluto/updater.rb', line 96 def update_feeds( opts={} ) logger.debug "using stdlib RSS::VERSION #{RSS::VERSION}" Feed.all.each do |feed_rec| feed_key = feed_rec.key feed_url = feed_rec.feed_url feed_xml = fetch_feed( feed_url ) logger.debug "feed_xml:" logger.debug feed_xml[ 0..300 ] # get first 300 chars # if opts.verbose? # also write a copy to disk if debug? logger.debug "saving feed to >./#{feed_key}.xml<..." File.open( "./#{feed_key}.xml", 'w' ) do |f| f.write( feed_xml ) end end puts "Before parsing feed >#{feed_key}<..." feed = parse_feed( feed_xml ) if feed.class == RSS::Atom::Feed puts "== #{feed.title.content} ==" else ## assume RSS::Rss::Feed puts "== #{feed.channel.title} ==" end feed.items.each do |item| if feed.class == RSS::Atom::Feed item_attribs = handle_feed_item_atom( item ) else ## assume RSS::Rss::Feed item_attribs = handle_feed_item_rss( item ) end # add feed_id fk_ref item_attribs[ :feed_id ] = feed_rec.id rec = Item.find_by_guid( item_attribs[ :guid ] ) if rec.nil? rec = Item.new puts "** NEW" else puts "UPDATE" end rec.update_attributes!( item_attribs ) end # each item end # each feed end |
#update_subscriptions(config, opts = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pluto/updater.rb', line 58 def update_subscriptions( config, opts={} ) ## for now - use single site w/ key planet -- fix!! allow multiple sites (planets) site_key = 'planet' site_rec = Site.find_by_key( site_key ) if site_rec.nil? site_rec = Site.new site_rec.key = site_key end site_rec.title = config[ 'title' ] site_rec.save! config[ 'feeds' ].each do |feed_key| feed_hash = config[ feed_key ] feed_url = feed_hash[ 'feed_url' ] puts "Updating feed subscription >#{feed_key}< - >#{feed_url}<..." feed_rec = Feed.find_by_key( feed_key ) if feed_rec.nil? feed_rec = Feed.new feed_rec.key = feed_key end feed_rec.feed_url = feed_url feed_rec.url = feed_hash[ 'url' ] feed_rec.title = feed_hash[ 'title' ] # todo: use title from feed? feed_rec.save! ## todo: # add subscription records (feed,site) - how? end end |