asakusa-rss-monitor 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY.md +8 -0
- data/README.md +29 -3
- data/lib/asakusa-rss-monitor.rb +1 -1
- data/lib/asakusa-rss-monitor/{as-info.rb → bot-config.rb} +2 -2
- data/lib/asakusa-rss-monitor/date-checker.rb +4 -4
- data/lib/asakusa-rss-monitor/date-save.rb +6 -2
- data/lib/asakusa-rss-monitor/post-bot.rb +4 -6
- data/lib/asakusa-rss-monitor/rss-monitor.rb +5 -5
- data/lib/asakusa-rss-monitor/version.rb +1 -1
- metadata +3 -2
data/HISTORY.md
ADDED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ To post rss updates on AsakusaSatellite.
|
|
8
8
|
monitor = AsakusaRssMonitor.new({
|
9
9
|
:check_file => 'last_time.txt',
|
10
10
|
:rss_url => '<TARGET RSS>',
|
11
|
-
:
|
11
|
+
:bot_config => AsakusaRssMonitor::BotConfig.new({
|
12
12
|
:api_key => '<YOUR AS API KEY>',
|
13
13
|
:entry_point => '<YOUR AS ENTRY POINT>', # cf. 'http://localhost:3000/api/v1'
|
14
14
|
:room_id => '<ROOM ID>'
|
@@ -21,7 +21,7 @@ To post rss updates on AsakusaSatellite.
|
|
21
21
|
def initialize(config)
|
22
22
|
super(config)
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# override this.
|
26
26
|
def perform(rss_entry)
|
27
27
|
"New Article. - #{rss_entry.date}\n#{rss_entry.title}\n#{rss_entry.link}"
|
@@ -29,7 +29,33 @@ To post rss updates on AsakusaSatellite.
|
|
29
29
|
end
|
30
30
|
|
31
31
|
## Monitoring RSS
|
32
|
-
To monitoring rss, you can use this script with cron
|
32
|
+
To monitoring rss, you can use this script with cron or some nice libraries like
|
33
|
+
[clockwork](https://github.com/tomykaira/clockwork).
|
34
|
+
|
35
|
+
### Using clockwork
|
36
|
+
|
37
|
+
gem install clockwork
|
38
|
+
|
39
|
+
Create clock.rb:
|
40
|
+
|
41
|
+
require 'clockwork'
|
42
|
+
require 'asakusa-rss-monitor'
|
43
|
+
include Clockwork
|
44
|
+
|
45
|
+
handler do |job|
|
46
|
+
job.call
|
47
|
+
end
|
48
|
+
|
49
|
+
monitor = AsakusaRssMonitor::RssMonitor.new({..})
|
50
|
+
another_monitor = AsakusaRssMonitor::RssMonitor.new({..}) # if you want to monitor another RSS.
|
51
|
+
every(5.minutes, monitor)
|
52
|
+
every(5.minutes, another_monitor)
|
53
|
+
|
54
|
+
Run it with the clockwork binary:
|
55
|
+
|
56
|
+
clockwork clock.rb
|
57
|
+
|
58
|
+
If you want to daemonize it, see [Daemonization](https://github.com/tomykaira/clockwork#daemonization) page on clockwork.
|
33
59
|
|
34
60
|
## Milestone
|
35
61
|
* spec..
|
data/lib/asakusa-rss-monitor.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
2
|
|
3
3
|
module AsakusaRssMonitor
|
4
|
-
class
|
5
|
-
|
4
|
+
class BotConfig
|
5
|
+
attr_reader :api_key, :entry_point, :room_id
|
6
6
|
def initialize(config)
|
7
7
|
@api_key = config[:api_key]
|
8
8
|
@entry_point = config[:entry_point]
|
@@ -8,17 +8,17 @@ module AsakusaRssMonitor
|
|
8
8
|
@save = DateSave.new(:file_name => config[:file_name])
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
@
|
11
|
+
def last_time
|
12
|
+
@last_time ||= @save.read
|
13
13
|
end
|
14
14
|
|
15
15
|
def check(time)
|
16
|
-
time == @
|
16
|
+
time == @last_time
|
17
17
|
end
|
18
18
|
|
19
19
|
def update(time)
|
20
20
|
@save.write time
|
21
|
-
@
|
21
|
+
@last_time = time
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -11,12 +11,16 @@ module AsakusaRssMonitor
|
|
11
11
|
|
12
12
|
def read
|
13
13
|
return Time.new(1999, 1, 1, 0, 0, 0) unless File.exist?(@file_name) # 十分古い日付
|
14
|
-
text = File.read(@file_name, :encoding => Encoding::UTF_8)
|
14
|
+
#text = File.read(@file_name, :encoding => Encoding::UTF_8)
|
15
|
+
text = File.read(@file_name)
|
15
16
|
Time.parse text
|
16
17
|
end
|
17
18
|
|
18
19
|
def write(time)
|
19
|
-
File.write(@file_name, time.to_s)
|
20
|
+
#File.write(@file_name, time.to_s)
|
21
|
+
open(@file_name, 'w') do |f|
|
22
|
+
f.write time.to_s
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
end
|
@@ -5,22 +5,20 @@ module AsakusaRssMonitor
|
|
5
5
|
require 'net/http'
|
6
6
|
|
7
7
|
def initialize(config)
|
8
|
-
@
|
9
|
-
@entry_point = config[:as_info][:entry_point]
|
10
|
-
@room_id = config[:as_info][:room_id]
|
8
|
+
@config = config[:bot_config]
|
11
9
|
end
|
12
10
|
|
13
11
|
def post(message)
|
14
|
-
uri = URI(@entry_point)
|
12
|
+
uri = URI(@config.entry_point)
|
15
13
|
Net::HTTP.version_1_2
|
16
14
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
17
15
|
p http.post(uri.path + "/message.json",
|
18
|
-
"room_id=#{@room_id}&api_key=#{@api_key}&message=#{message}")
|
16
|
+
"room_id=#{@config.room_id}&api_key=#{@config.api_key}&message=#{message}")
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
20
|
def get_rooms
|
23
|
-
uri = URI(@entry_point)
|
21
|
+
uri = URI(@config.entry_point)
|
24
22
|
Net::HTTP.version_1_2
|
25
23
|
Net::HTTP.start(uri.host, uri.port) do |http|
|
26
24
|
response = http.get(uri.path + "/room/list.json")
|
@@ -11,26 +11,26 @@ module AsakusaRssMonitor
|
|
11
11
|
def initialize(config)
|
12
12
|
@check_file = config[:check_file]
|
13
13
|
@rss_url = config[:rss_url]
|
14
|
-
@
|
14
|
+
@bot_config = config[:bot_config]
|
15
15
|
end
|
16
16
|
|
17
17
|
def call
|
18
18
|
checker = DateChecker.new(:file_name => @check_file)
|
19
|
-
|
19
|
+
last_time= checker.last_time
|
20
20
|
|
21
21
|
rss = RssGetter.new(:url => @rss_url)
|
22
|
-
new_articles = rss.get_new_articles
|
22
|
+
new_articles = rss.get_new_articles last_time
|
23
23
|
return unless new_articles.any?
|
24
24
|
|
25
25
|
begin
|
26
|
-
bot = PostBot.new(:
|
26
|
+
bot = PostBot.new(:bot_config => @bot_config)
|
27
27
|
new_articles.each do |entry|
|
28
28
|
bot.post perform(entry)
|
29
29
|
end
|
30
30
|
rescue => exception
|
31
31
|
p exception
|
32
|
+
return
|
32
33
|
end
|
33
|
-
|
34
34
|
# rss.get_new_entries returns articles by ascending order
|
35
35
|
checker.update new_articles.last.date
|
36
36
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asakusa-rss-monitor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -22,11 +22,12 @@ extra_rdoc_files: []
|
|
22
22
|
files:
|
23
23
|
- .gitignore
|
24
24
|
- Gemfile
|
25
|
+
- HISTORY.md
|
25
26
|
- README.md
|
26
27
|
- Rakefile
|
27
28
|
- asakusa-rss-monitor.gemspec
|
28
29
|
- lib/asakusa-rss-monitor.rb
|
29
|
-
- lib/asakusa-rss-monitor/
|
30
|
+
- lib/asakusa-rss-monitor/bot-config.rb
|
30
31
|
- lib/asakusa-rss-monitor/date-checker.rb
|
31
32
|
- lib/asakusa-rss-monitor/date-save.rb
|
32
33
|
- lib/asakusa-rss-monitor/post-bot.rb
|