Commit b8bcd24b00c2a03c95b4f8b8f22cf617d68580de
1 parent
be6c3d11
Exists in
master
and in
22 other branches
Remove merge artifact added by mistake
Showing
1 changed file
with
0 additions
and
110 deletions
Show diff stats
lib/feed_handler.rb.orig
... | ... | @@ -1,110 +0,0 @@ |
1 | -require 'feedparser' | |
2 | -require 'open-uri' | |
3 | - | |
4 | -# This class is responsible for processing feeds and pass the items to the | |
5 | -# respective container. | |
6 | -# | |
7 | -# The <tt>max_errors</tt> attribute controls how many times it will retry in | |
8 | -# case of failure. If a feed fails for <tt>max_errors+1</tt> times, it will be | |
9 | -# disabled and the last error message will be recorder in the container. | |
10 | -# The default value is *6*, if you need to change it you can do that in your | |
11 | -# config/local.rb file like this: | |
12 | -# | |
13 | -# FeedHandler.max_errors = 10 | |
14 | -# | |
15 | -# For the update interval, see FeedUpdater. | |
16 | -class FeedHandler | |
17 | - | |
18 | - # The maximum number | |
19 | - cattr_accessor :max_errors | |
20 | - cattr_accessor :disabled_period | |
21 | - | |
22 | - self.max_errors = 6 | |
23 | - self.disabled_period = 1.week | |
24 | - | |
25 | - def parse(content) | |
26 | - raise FeedHandler::ParseError, "Content is nil" if content.nil? | |
27 | - begin | |
28 | - return FeedParser::Feed::new(content) | |
29 | - rescue Exception => ex | |
30 | - raise FeedHandler::ParseError, "Invalid feed format." | |
31 | - end | |
32 | - end | |
33 | - | |
34 | - def fetch(address) | |
35 | - begin | |
36 | - content = "" | |
37 | - block = lambda { |s| content = s.read } | |
38 | - content = | |
39 | - if Rails.env == 'test' && File.exists?(address) | |
40 | - File.read(address) | |
41 | - else | |
42 | - if !valid_url?(address) | |
43 | - raise InvalidUrl.new("\"%s\" is not a valid URL" % address) | |
44 | - end | |
45 | - open(address, "User-Agent" => "Noosfero/#{Noosfero::VERSION}", &block) | |
46 | - end | |
47 | - return content | |
48 | - rescue Exception => ex | |
49 | - raise FeedHandler::FetchError, ex.message | |
50 | - end | |
51 | - end | |
52 | - | |
53 | - def process(container) | |
54 | -<<<<<<< HEAD | |
55 | - Rails.logger.info("Processing %s with id = %d" % [container.class.name, container.id]) | |
56 | -======= | |
57 | ->>>>>>> rails235 | |
58 | - begin | |
59 | - container.class.transaction do | |
60 | - if container.update_errors > FeedHandler.max_errors && container.fetched_at < (Time.now - FeedHandler.disabled_period) | |
61 | - container.enabled = true | |
62 | - container.update_errors = 0 | |
63 | - container.save | |
64 | - end | |
65 | - next unless container.enabled | |
66 | - actually_process_container(container) | |
67 | - container.update_errors = 0 | |
68 | - container.finish_fetch | |
69 | - end | |
70 | - rescue Exception => exception | |
71 | - Rails.logger.warn("Unknown error from %s ID %d\n%s" % [container.class.name, container.id, exception.to_s]) | |
72 | - Rails.logger.warn("Backtrace:\n%s" % exception.backtrace.join("\n")) | |
73 | - container.reload | |
74 | - container.update_errors += 1 | |
75 | - container.error_message = exception.to_s | |
76 | - if container.update_errors > FeedHandler.max_errors | |
77 | - container.fetched_at = Time.now | |
78 | - container.enabled = false | |
79 | - end | |
80 | - begin | |
81 | - container.finish_fetch | |
82 | - rescue Exception => finish_fetch_exception | |
83 | - Rails.logger.warn("Unable to finish fetch from %s ID %d\n%s" % [container.class.name, container.id, finish_fetch_exception.to_s]) | |
84 | - Rails.logger.warn("Backtrace:\n%s" % finish_fetch_exception.backtrace.join("\n")) | |
85 | - end | |
86 | - end | |
87 | - end | |
88 | - | |
89 | - class InvalidUrl < Exception; end | |
90 | - class ParseError < Exception; end | |
91 | - class FetchError < Exception; end | |
92 | - | |
93 | - protected | |
94 | - | |
95 | - def actually_process_container(container) | |
96 | - container.clear | |
97 | - content = fetch(container.address) | |
98 | - container.fetched_at = Time.now | |
99 | - parsed_feed = parse(content) | |
100 | - container.feed_title = parsed_feed.title | |
101 | - parsed_feed.items[0..container.limit-1].reverse.each do |item| | |
102 | - container.add_item(item.title, item.link, item.date, item.content) | |
103 | - end | |
104 | - end | |
105 | - | |
106 | - def valid_url?(url) | |
107 | - url =~ URI.regexp('http') || url =~ URI.regexp('https') | |
108 | - end | |
109 | - | |
110 | -end |