asakusa-rss-monitor 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hoge.gemspec
4
+ gemspec
@@ -0,0 +1,4 @@
1
+ asakusa-rss-monitor
2
+ ===================
3
+
4
+ To post rss updates on AsakusaSatellite.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "asakusa-rss-monitor/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "asakusa-rss-monitor"
7
+ s.version = AsakusaRssMonitor::VERSION
8
+ s.authors = ["3100"]
9
+ s.email = ["sugar16g@gmail.com"]
10
+ s.homepage = "https://github.com/3100/asakusa-rss-monitor"
11
+ s.summary = %q{Posting rss updates to a room in your AsakusaSatellite.}
12
+ s.description = %q{Posting rss updates to a room in your AsakusaSatellite. To monitor rss with this script, you need to use a cron job.}
13
+
14
+ s.rubyforge_project = "asakusa-rss-monitor"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
@@ -0,0 +1,4 @@
1
+ # -*- coding: utf-8 -*-
2
+ require "asakusa-rss-monitor/version"
3
+ require "asakusa-rss-monitor/rss-monitor"
4
+ require "asakusa-rss-monitor/as-info"
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module AsakusaRssMonitor
4
+ class ASInfo
5
+ attr_accessor :api_key, :entry_point, :room_id
6
+ def initialize(config)
7
+ @api_key = config[:api_key]
8
+ @entry_point = config[:entry_point]
9
+ @room_id = config[:room_id]
10
+ end
11
+ end
12
+ end
13
+
@@ -0,0 +1,25 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'asakusa-rss-monitor/date-save'
4
+
5
+ module AsakusaRssMonitor
6
+ class DateChecker
7
+ def initialize(config)
8
+ @save = DateSave.new(:file_name => config[:file_name])
9
+ end
10
+
11
+ def latest
12
+ @latest ||= @save.read
13
+ end
14
+
15
+ def check(time)
16
+ time == @latest
17
+ end
18
+
19
+ def update(time)
20
+ @save.write time
21
+ @latest = time
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,22 @@
1
+ # -*- coding: utf-8 -*-
2
+ #/usr/bin/ruby
3
+
4
+ module AsakusaRssMonitor
5
+ class DateSave
6
+ require 'time'
7
+
8
+ def initialize(config)
9
+ @file_name = config[:file_name]
10
+ end
11
+
12
+ def read
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)
15
+ Time.parse text
16
+ end
17
+
18
+ def write(time)
19
+ File.write(@file_name, time.to_s)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module AsakusaRssMonitor
4
+ class PostBot
5
+ require 'net/http'
6
+
7
+ def initialize(config)
8
+ @api_key = config[:as_info][:api_key]
9
+ @entry_point = config[:as_info][:entry_point]
10
+ @room_id = config[:as_info][:room_id]
11
+ end
12
+
13
+ def post(message)
14
+ uri = URI(@entry_point)
15
+ Net::HTTP.version_1_2
16
+ Net::HTTP.start(uri.host, uri.port) do |http|
17
+ p http.post(uri.path + "/message.json",
18
+ "room_id=#{@room_id}&api_key=#{@api_key}&message=#{message}")
19
+ end
20
+ end
21
+
22
+ def get_rooms
23
+ uri = URI(@entry_point)
24
+ Net::HTTP.version_1_2
25
+ Net::HTTP.start(uri.host, uri.port) do |http|
26
+ response = http.get(uri.path + "/room/list.json")
27
+ p response.body
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ module AsakusaRssMonitor
4
+ class RssGetter
5
+ require 'rss'
6
+
7
+ def initialize(config)
8
+ @url = config[:url]
9
+ end
10
+
11
+ def get_new_articles(time)
12
+ results = RSS::Parser.parse(@url)
13
+ results.items.select{|item|
14
+ item.date > time
15
+ }.reverse
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,42 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'asakusa-rss-monitor/date-checker'
4
+ require 'asakusa-rss-monitor/date-save'
5
+ require 'asakusa-rss-monitor/post-bot'
6
+ require 'asakusa-rss-monitor/rss-getter'
7
+
8
+ module AsakusaRssMonitor
9
+
10
+ class RssMonitor
11
+ def initialize(config)
12
+ @check_file = config[:check_file]
13
+ @rss_url = config[:rss_url]
14
+ @as_info = config[:as_info]
15
+ end
16
+
17
+ def call
18
+ checker = DateChecker.new(:file_name => @check_file)
19
+ latest_time= checker.latest
20
+
21
+ rss = RssGetter.new(:url => @rss_url)
22
+ new_articles = rss.get_new_articles latest_time
23
+ return unless new_articles.any?
24
+
25
+ begin
26
+ bot = PostBot.new(:as_info => @as_info)
27
+ new_articles.each do |entry|
28
+ bot.post perform(entry)
29
+ end
30
+ rescue => exception
31
+ p exception
32
+ end
33
+
34
+ # rss.get_new_entries returns articles by ascending order
35
+ checker.update new_articles.last.date
36
+ end
37
+
38
+ def perform(rss_entry)
39
+ "New Article - #{rss_entry.date}\n#{rss_entry.title}\n #{rss_entry.link} "
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ module AsakusaRssMonitor
3
+ VERSION = "0.0.2"
4
+ end
5
+
6
+
@@ -0,0 +1 @@
1
+ Fri, 18 Jan 2013 15:24:56 +0900
@@ -0,0 +1,26 @@
1
+ # -*- coding: utf-8 -*-
2
+ #/usr/bin/ruby
3
+
4
+ require 'asakusa-rss-monitor'
5
+
6
+ class QaMonitor < AsakusaRssMonitor::RssMonitor
7
+ def initialize(config)
8
+ super(config)
9
+ end
10
+
11
+ def perform(rss_entry)
12
+ "新着の質問がありました。 - #{rss_entry.date}\n#{rss_entry.title}\n #{rss_entry.link} "
13
+ end
14
+ end
15
+
16
+ monitor = QaMonitor.new({
17
+ :check_file => 'qa-checker.txt',
18
+ :rss_url => 'http://localhost/?type=rss',
19
+ :as_info => AsakusaRssMonitor::ASInfo.new({
20
+ :api_key => ENV['AS_API'],
21
+ :entry_point => ENV['ENTRY_POINT'],
22
+ :room_id => '<target-room-id>'
23
+ })
24
+ })
25
+ monitor.call
26
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: asakusa-rss-monitor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - '3100'
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-19 00:00:00.000000000 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Posting rss updates to a room in your AsakusaSatellite. To monitor rss
16
+ with this script, you need to use a cron job.
17
+ email:
18
+ - sugar16g@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - .gitignore
24
+ - Gemfile
25
+ - README.md
26
+ - Rakefile
27
+ - asakusa-rss-monitor.gemspec
28
+ - lib/asakusa-rss-monitor.rb
29
+ - lib/asakusa-rss-monitor/as-info.rb
30
+ - lib/asakusa-rss-monitor/date-checker.rb
31
+ - lib/asakusa-rss-monitor/date-save.rb
32
+ - lib/asakusa-rss-monitor/post-bot.rb
33
+ - lib/asakusa-rss-monitor/rss-getter.rb
34
+ - lib/asakusa-rss-monitor/rss-monitor.rb
35
+ - lib/asakusa-rss-monitor/version.rb
36
+ - sample/qa-checker.txt
37
+ - sample/qa-monitor.rb
38
+ has_rdoc: true
39
+ homepage: https://github.com/3100/asakusa-rss-monitor
40
+ licenses: []
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project: asakusa-rss-monitor
59
+ rubygems_version: 1.6.2
60
+ signing_key:
61
+ specification_version: 3
62
+ summary: Posting rss updates to a room in your AsakusaSatellite.
63
+ test_files: []