Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'nokogiri'
- require 'open-uri'
- require 'mongo'
- require 'date'
- include Mongo
- DBHOST = ''
- DBPORT = 27017
- DBNAME = ''
- DBADMIN = ''
- DBPASSWORD = ''
- class Worker
- def initialize
- mongo_client = MongoClient.new(DBHOST, DBPORT)
- @db = mongo_client.db(DBNAME)
- auth = @db.authenticate(DBADMIN, DBPASSWORD)
- end
- def doWork(collection, feeds)
- puts "SUNT IN METODA doWORK --------->"
- coll = @db[collection]
- feeds.each do |feed|
- puts feed
- if feed.include? 'feedburner'
- doc = Nokogiri::XML(open(feed))
- items = doc.xpath('rss/channel/item')
- source = doc.xpath('rss/channel/generator').text
- existentTitles = coll.distinct('titlu')
- i = 0
- items.each do |item|
- titlu = item.xpath("title").text
- if not existentTitles.include? titlu
- i += 1
- link = item.xpath("feedburner:origLink").text
- publishDate = DateTime.parse(item.xpath("pubDate").text).to_datetime.to_s
- publishDate["T"] = " "
- imagine = item.xpath("enclosure/@url").text
- stire = {"titlu" => titlu, "link" => link, "publishDate" => publishDate, "imagine" => imagine, "sursa" => source}
- coll.insert(stire)
- end # end pentru verificare unicitate
- end # end pentru iterare printre iteme
- else #daca nu am feedburner
- doc = Nokogiri::XML(open(feed))
- items = doc.xpath('rss/channel/item')
- source = doc.xpath('rss/channel/title').text
- source = source.split(' - ')
- source = source[1]
- existentTitles = coll.distinct('titlu')
- i = 0
- items.each do |item|
- titlu = item.xpath("title").text
- if not existentTitles.include? titlu
- i += 1
- link = item.xpath("link").text
- publishDate = DateTime.parse(item.xpath("pubDate").text).to_datetime.to_s
- publishDate["T"] = " "
- imagine = item.xpath("enclosure/@url").text
- stire = {"titlu" => titlu, "link" => link, "publishDate" => publishDate, "imagine" => imagine, "sursa" => source}
- coll.insert(stire)
- end # end pentru verificare unicitate
- end # end pentru iterare printre iteme
- end
- end # end pentru iterare prin feeduri
- end # end metoda
- end # end clasa
Advertisement
Add Comment
Please, Sign In to add comment