Guest User

worker.rb

a guest
Dec 25th, 2014
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.43 KB | None | 0 0
  1. require 'nokogiri'
  2. require 'open-uri'
  3. require 'mongo'
  4. require 'date'
  5. include Mongo
  6.  
  7. DBHOST = ''
  8. DBPORT = 27017
  9. DBNAME = ''
  10. DBADMIN = ''
  11. DBPASSWORD = ''
  12.  
  13.  
  14.  
  15. class Worker
  16.  
  17.   def initialize
  18.     mongo_client = MongoClient.new(DBHOST, DBPORT)
  19.     @db = mongo_client.db(DBNAME)
  20.     auth = @db.authenticate(DBADMIN, DBPASSWORD)
  21.   end
  22.  
  23.   def doWork(collection, feeds)
  24.     puts "SUNT IN METODA doWORK --------->"
  25.     coll = @db[collection]
  26.     feeds.each do |feed|
  27.         puts feed
  28.         if feed.include? 'feedburner'
  29.           doc = Nokogiri::XML(open(feed))
  30.           items = doc.xpath('rss/channel/item')
  31.           source = doc.xpath('rss/channel/generator').text
  32.           existentTitles = coll.distinct('titlu')
  33.  
  34.           i = 0
  35.           items.each do |item|
  36.             titlu = item.xpath("title").text
  37.             if not existentTitles.include? titlu
  38.               i += 1
  39.               link = item.xpath("feedburner:origLink").text
  40.               publishDate = DateTime.parse(item.xpath("pubDate").text).to_datetime.to_s
  41.               publishDate["T"] = " "
  42.               imagine = item.xpath("enclosure/@url").text
  43.               stire = {"titlu" => titlu, "link" => link, "publishDate" => publishDate, "imagine" => imagine, "sursa" => source}
  44.               coll.insert(stire)
  45.             end # end pentru verificare unicitate
  46.           end # end pentru iterare printre iteme
  47.         else #daca nu am feedburner
  48.           doc = Nokogiri::XML(open(feed))
  49.           items = doc.xpath('rss/channel/item')
  50.           source = doc.xpath('rss/channel/title').text
  51.           source = source.split(' - ')
  52.           source = source[1]
  53.           existentTitles = coll.distinct('titlu')
  54.  
  55.           i = 0
  56.           items.each do |item|
  57.             titlu = item.xpath("title").text
  58.             if not existentTitles.include? titlu
  59.               i += 1
  60.               link = item.xpath("link").text
  61.               publishDate = DateTime.parse(item.xpath("pubDate").text).to_datetime.to_s
  62.               publishDate["T"] = " "
  63.               imagine = item.xpath("enclosure/@url").text
  64.               stire = {"titlu" => titlu, "link" => link, "publishDate" => publishDate, "imagine" => imagine, "sursa" => source}
  65.               coll.insert(stire)
  66.             end # end pentru verificare unicitate
  67.           end # end pentru iterare printre iteme
  68.         end
  69.     end # end pentru iterare prin feeduri
  70.   end # end metoda
  71. end # end clasa
Advertisement
Add Comment
Please, Sign In to add comment