Skip to content

Commit 4e995f2

Browse files
committed
Add rss input handler
1 parent 4d737ee commit 4e995f2

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

example.config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ inputs:
1212
trigger:
1313
schedule: '*/5 9-16 * * *'
1414
now: true
15+
- class: Rss
16+
enabled: false
17+
uri: https://www.archlinux.org/feeds/news/
18+
trigger:
19+
schedule: '*/10 * * * *'
20+
now: false
1521
- class: RocketChat
1622
enabled: false
1723
channel: '#xify-in'

lib/xify/input/rss.rb

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'rss'
2+
require 'open-uri'
3+
4+
module Input
5+
class Rss
6+
def initialize(config)
7+
@config = config
8+
9+
@uri = URI.parse config['uri']
10+
11+
working_dir = "#{ENV['HOME']}/.xify/Rss"
12+
Dir.mkdir working_dir rescue Errno::EEXIST
13+
@latest_file = "#{working_dir}/#{@uri.to_s.gsub(/\W+/, '_')}"
14+
end
15+
16+
def updates
17+
opts = {}
18+
opts[:first] = :now if @config['trigger']['now']
19+
Rufus::Scheduler.singleton.repeat @config['trigger']['schedule'], opts do
20+
open(@uri) do |rss|
21+
latest = Time.parse File.read(@latest_file) rescue Time.now - 24*60*60
22+
feed = RSS::Parser.parse(rss)
23+
feed.items
24+
.select do |item|
25+
item.pubDate > latest
26+
end
27+
.sort_by do |item|
28+
item.pubDate
29+
end
30+
.each do |item|
31+
yield Xify::Event.new(
32+
item.dc_creator,
33+
"*#{item.title}*\n\n#{item.description}",
34+
link: item.link,
35+
parent: feed.channel.title,
36+
parent_link: feed.channel.link,
37+
time: item.pubDate
38+
)
39+
update_latest item
40+
end
41+
end
42+
end
43+
end
44+
45+
private
46+
47+
def update_latest(latest)
48+
File.write @latest_file, latest.pubDate.to_s
49+
end
50+
end
51+
end

xify.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ require 'date'
22

33
Gem::Specification.new do |s|
44
s.name = 'xify'
5-
s.version = '0.4.2'
5+
s.version = '0.5.0'
66
s.date = Date.today.to_s
77
s.summary = 'Cross-post content from one service to another.'
88
s.description = 'Cross-post content from one service to another.'

0 commit comments

Comments
 (0)