Class: RssNotifier::App

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

Constant Summary collapse

CONFIG_DIR =
Pathname.new('.')
CONFIG_FILE =
CONFIG_DIR.join('config.yml')
DB_FILE =
CONFIG_DIR.join('db.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notify: false) ⇒ App

Returns a new instance of App.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rss_notifier/app.rb', line 39

def initialize(notify: false)
  @config = RssNotifier::Config.load(CONFIG_FILE)
  @db = RssNotifier::Db.load(DB_FILE)
  @notify = []
  @options = {
    notify: notify
  }
  @html_coder = HTMLEntities.new

  setup_notify(@config.notify)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/rss_notifier/app.rb', line 19

def options
  @options
end

Class Method Details

.init(force: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rss_notifier/app.rb', line 22

def self.init(force: false)
  if !Dir.exists?(CONFIG_DIR.to_s) || force
    RssNotifier.logger.info("Creating #{CONFIG_DIR}")
    FileUtils.mkdir_p(CONFIG_DIR.to_s)
  else
    RssNotifier.logger.info("Directory #{CONFIG_DIR} already exists, skipping")
  end

  if !File.exists?(CONFIG_FILE) || force
    RssNotifier.logger.info("Creating #{CONFIG_FILE}")
    config = RssNotifier::Config.default
    config.save_to(CONFIG_FILE)
  else
    RssNotifier.logger.info("File #{CONFIG_FILE} already exists, skipping")
  end
end

Instance Method Details

#check_rss!Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
# File 'lib/rss_notifier/app.rb', line 79

def check_rss!
  changed_items = []

  rss_urls_from_config.each do |url, name|
    RssNotifier.logger.info "Checking #{name} | #{url}"

    feed = @db.get_feed(url: url, name: name)

    rss_raw = nil
    response = nil
    begin
      headers = {}
      max_cache_duration = 20*60 # 20 minutes
      if feed.last_modified
        headers['If-Modified-Since'] = feed.last_modified.utc.httpdate
      end
      headers['Cache-Control'] = 'no-cache'
      response = HTTP.headers(headers).get(url)

      if response.code == 304
        RssNotifier.logger.info "Not modified since #{feed.last_modified}: #{name} | #{url}"
        next
      elsif response.code != 200
        RssNotifier.logger.warn "got non 200 code: #{response.code}:"
        puts response.body.to_s
        next
      end

      feed.last_modified = Time.parse(response.headers['Last-Modified'])

      raw_encoded = response.to_s.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
      rss_raw = RSS::Parser.parse(raw_encoded)
    rescue => e
      RssNotifier.logger.warn "Cannot parse RSS: #{e}"
      puts e.backtrace
      next
    end

    rss_raw.items.each do |raw_item|
      item = feed.find_or_create_item(link: raw_item.link)
      item.update({
        title: decode_html(raw_item.title),
        description: decode_html(raw_item.description),
        date: raw_item.date
      })
      changed_items << item if item.changed?
    end

    feed.save
  end

  if changed_items.empty?
    RssNotifier.logger.info 'No changes'
    return
  end

  if options[:notify]
    RssNotifier.logger.info "#{changed_items.size} items changed, notifing..."

    changed_items.each do |item|
      notify!(item)
    end
  else
    RssNotifier.logger.info "#{changed_items.size} items changed"
  end
end

#decode_html(encoded) ⇒ Object



146
147
148
149
150
# File 'lib/rss_notifier/app.rb', line 146

def decode_html(encoded)
  @html_coder.decode(encoded)
rescue => e
  encoded
end

#notify!(item) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/rss_notifier/app.rb', line 152

def notify!(item)
  @notify.each do |notify|
    begin
      notify.notify(item)
    rescue => e
      puts "#{e}: #{item.link} | #{notify}"
      puts e.backtrace
    end
  end
end

#rss_urls_from_configArray

Returns of tuples (url, name).

Returns:

  • (Array)

    of tuples (url, name)



73
74
75
76
77
# File 'lib/rss_notifier/app.rb', line 73

def rss_urls_from_config
  @rss_urls_from_config ||= @config.rss_urls.map do |o|
    [ o[:url].to_s.strip, o[:name].to_s.strip ]
  end.to_h
end

#runObject



64
65
66
67
68
69
70
# File 'lib/rss_notifier/app.rb', line 64

def run
  loop do
    check_rss!
    RssNotifier.logger.info "Waiting for #{@config.period_in_minutes} minutes"
    sleep @config.period_in_minutes * 60
  end
end

#setup_notify(notify) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rss_notifier/app.rb', line 51

def setup_notify(notify)
  notify.each do |d|
    next unless d[:enabled]
    if d[:adapter] == 'email'
      @notify << Adapter::Email.new(d[:email], @config)
    elsif d[:adapter] == 'pushbullet'
      @notify << Adapter::Pushbullet.new(d[:name], d[:access_token])
    else
      raise "Unknown adapter #{d[:adapter]}"
    end
  end
end