Class: CrawlerParser

Inherits:
Object
  • Object
show all
Defined in:
lib/crawler_parser.rb

Constant Summary collapse

FS_LEN =

logger = Logger.new(‘/tmp/crawlerEngine.log’, ‘daily’) logger.level = Logger::INFO

80

Instance Method Summary collapse

Instance Method Details

#parse_rss(sources) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/crawler_parser.rb', line 14

def parse_rss(sources)
	return if sources.nil? or sources.size.eql?(0)
	#logger.info("Links to crawl >>"+links.to_s)
	puts "Links to crawl >>"+sources.to_s
	sources.uniq!
	sources.each do |source|
		begin
			rss = SimpleRSS.parse open(source.link)
		rescue Exception=>ex
			#logger.error(ex)
			#logger.info("SimpleRSS got unexpected error, rss exit")
			puts ex
		end
		#TODO:
		#puts rss.feed_tags.title
		#puts rss.feed_tags.description

		for item in rss.items
			print_rss_item(item)
			#parse post details
			begin
				doc = Nokogiri::HTML open(item.link.to_s)
			rescue Exception=>ex
				#logger.error(ex)
				#logger.info("Nokogiri got unexpected error")
				puts ex
			end
			return unless doc
			doc.xpath(source.filter).each do |content|
				#puts content.to_s.force_encoding('GB2312')
				#Iconv.iconv("GB2312//IGNORE","UTF-8//IGNORE", content)
				#Iconv.iconv("UTF-8//IGNORE","GB2312//IGNORE", content)
				Post.create(
					:title=>item.title.to_s.force_encoding('UTF-8'),
					:source=>item.link.to_s.force_encoding('UTF-8'),
					:content=>content.to_s.force_encoding('UTF-8'),
					:published_at=>item.pubDate,
					:site_name=>source.site_name,
					:category => source.category)
			end
		end
		#update crawler exec timestamp
		source.update_attribute("crawled_at", Time.now)
	end
end