consadole_aggregator 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -3,23 +3,23 @@ GEM
3
3
  specs:
4
4
  diff-lcs (1.1.2)
5
5
  git (1.2.5)
6
- jeweler (1.5.2)
7
- bundler (~> 1.0.0)
6
+ jeweler (1.6.2)
7
+ bundler (~> 1.0)
8
8
  git (>= 1.2.5)
9
9
  rake
10
10
  json (1.5.1)
11
11
  nokogiri (1.4.4)
12
12
  oauth (0.4.4)
13
- rake (0.8.7)
13
+ rake (0.9.1)
14
14
  rcov (0.9.9)
15
- rspec (2.5.0)
16
- rspec-core (~> 2.5.0)
17
- rspec-expectations (~> 2.5.0)
18
- rspec-mocks (~> 2.5.0)
19
- rspec-core (2.5.1)
20
- rspec-expectations (2.5.0)
15
+ rspec (2.6.0)
16
+ rspec-core (~> 2.6.0)
17
+ rspec-expectations (~> 2.6.0)
18
+ rspec-mocks (~> 2.6.0)
19
+ rspec-core (2.6.3)
20
+ rspec-expectations (2.6.0)
21
21
  diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.5.0)
22
+ rspec-mocks (2.6.0)
23
23
  rubytter (1.4.2)
24
24
  json (>= 1.1.3)
25
25
  oauth (>= 0.3.6)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{consadole_aggregator}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["niku"]
12
- s.date = %q{2011-05-08}
12
+ s.date = %q{2011-06-09}
13
13
  s.default_executable = %q{consadole_aggregator}
14
14
  s.description = %q{ It aggregates infomation of 'Consadole Sapporo' }
15
15
  s.email = %q{niku@niku.name}
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "consadole_aggregator.gemspec",
33
33
  "db/.gitignore",
34
34
  "lib/consadole_aggregator.rb",
35
+ "lib/consadole_aggregator/aggregatable.rb",
35
36
  "lib/consadole_aggregator/helper.rb",
36
37
  "lib/consadole_aggregator/live.rb",
37
38
  "lib/consadole_aggregator/live/timeline.rb",
@@ -46,7 +47,6 @@ Gem::Specification.new do |s|
46
47
  "spec/ext/consaclub.txt",
47
48
  "spec/ext/consadolenews.txt",
48
49
  "spec/ext/consadolephotos.txt",
49
- "spec/ext/consadolesponsornews.txt",
50
50
  "spec/ext/forzaconsadole.txt",
51
51
  "spec/ext/hochiyomiuri.txt",
52
52
  "spec/ext/jsgoalnews.txt",
@@ -179,15 +179,8 @@ Gem::Specification.new do |s|
179
179
  s.homepage = %q{http://github.com/niku/consadole_aggregator}
180
180
  s.licenses = ["MIT"]
181
181
  s.require_paths = ["lib"]
182
- s.rubygems_version = %q{1.5.2}
182
+ s.rubygems_version = %q{1.6.2}
183
183
  s.summary = %q{It aggregates infomation of 'Consadole Sapporo'}
184
- s.test_files = [
185
- "spec/consadole_aggregator/helper_spec.rb",
186
- "spec/consadole_aggregator/live/timeline_spec.rb",
187
- "spec/consadole_aggregator/live_spec.rb",
188
- "spec/consadole_aggregator/news_spec.rb",
189
- "spec/spec_helper.rb"
190
- ]
191
184
 
192
185
  if s.respond_to? :specification_version then
193
186
  s.specification_version = 3
@@ -0,0 +1,61 @@
1
+ require 'logger'
2
+ require 'yaml'
3
+
4
+ module ConsadoleAggregator
5
+ module Aggregatable
6
+ def initialize logger=nil
7
+ @logger = logger || Logger.new(nil)
8
+ end
9
+
10
+ def get_new_articles
11
+ get_resource = self.class.get_resource
12
+ parse_list = self.class.parse_list
13
+ parse_article = self.class.parse_article
14
+ raise NotImplementedError unless get_resource && parse_list && parse_article
15
+ list_url = get_resource.call
16
+ article_urls = parse_list.call(list_url)
17
+ article_urls.each_with_object([]) do |article_url, memo|
18
+ article = parse_article.call(article_url)
19
+ memo.push(article) if article && !get_strage.include?(article)
20
+ end
21
+ end
22
+
23
+ def update
24
+ @logger.info('begin of update')
25
+ get_new_articles.each do |article|
26
+ begin
27
+ yield article if block_given?
28
+ @strage << article
29
+ rescue
30
+ @logger.error $!
31
+ end
32
+ end
33
+ save_strage
34
+ @logger.info('end of update')
35
+ end
36
+
37
+ def get_strage
38
+ @strage ||= YAML.load_file(build_strage_path) || [] # fix when YAML.load_file is nil
39
+ rescue
40
+ @strage = []
41
+ end
42
+
43
+ def save_strage
44
+ YAML.dump(@strage, File.new(build_strage_path, 'w'))
45
+ end
46
+
47
+ def build_strage_path
48
+ class_name = /([^:]+)$/.match(self.class.to_s)[1]
49
+ File.expand_path(File.dirname(__FILE__) + "/../../db/#{class_name}.yaml")
50
+ end
51
+
52
+ # define class method's
53
+ def self.included(mod)
54
+ mod.extend ClassMethods
55
+ end
56
+
57
+ module ClassMethods
58
+ attr_accessor :get_resource, :parse_list, :parse_article
59
+ end
60
+ end
61
+ end
@@ -8,25 +8,13 @@ require_relative 'live/timeline.rb'
8
8
 
9
9
  module ConsadoleAggregator
10
10
  module Live
11
- BASE_URI = URI.parse('http://www.consadole-sapporo.jp/view/s674.html')
12
11
 
13
12
  def self.reserve reservation_time=nil, opt ={}
14
13
  Live.new(reservation_time, opt)
15
14
  end
16
15
 
17
- def self.get_resource
18
- Net::HTTP.get(BASE_URI).toutf8
19
- end
20
-
21
- def self.parse
22
- doc = Nokogiri::HTML.parse(get_resource)
23
- doc.search('hr + p').last.inner_html.split(/<br>|\n/).reverse.each_with_object([]) do |line, memo|
24
- timeline = Timeline.parse line
25
- memo << timeline if timeline
26
- end
27
- end
28
-
29
16
  class Live
17
+ include Aggregatable
30
18
  attr_reader :reservation_time, :posted, :times, :wait_sec
31
19
 
32
20
  def initialize reservation_time=nil, opt ={}
@@ -70,6 +58,27 @@ module ConsadoleAggregator
70
58
  end
71
59
  end
72
60
 
61
+ def self.get_resource
62
+ ->{ Net::HTTP.get(URI.parse('http://www.consadole-sapporo.jp/view/s674.html')).toutf8 }
63
+ end
64
+
65
+ def self.parse_list
66
+ ->(list){
67
+ live_block = Nokogiri::HTML::parse(list)
68
+ .search("hr + p")
69
+ .last
70
+ .inner_html
71
+ lines = live_block
72
+ .split(/<br>|\n/)
73
+ .delete_if{ |line| line.empty? || line =~ /&lt;前|後半&gt;/ }
74
+ lines.map{ |line| line.sub(/ +$/, "") }.reverse
75
+ }
76
+ end
77
+
78
+ def self.parse_article
79
+ ->(article){ Timeline.parse article }
80
+ end
81
+
73
82
  private
74
83
  def be_daemonize
75
84
  Process.daemon
@@ -4,67 +4,9 @@ require 'rss'
4
4
  require 'uri'
5
5
  require 'kconv'
6
6
  require 'net/http'
7
- require 'yaml'
8
7
  require 'nokogiri'
9
8
 
10
9
  module ConsadoleAggregator
11
- module Aggregatable
12
- def initialize logger=nil
13
- @logger = logger || Logger.new(nil)
14
- end
15
-
16
- def get_new_articles
17
- get_resource = self.class.get_resource
18
- parse_list = self.class.parse_list
19
- parse_article = self.class.parse_article
20
- raise NotImplementedError unless get_resource && parse_list && parse_article
21
- list_url = get_resource.call
22
- article_urls = parse_list.call(list_url)
23
- article_urls.each_with_object([]) do |article_url, memo|
24
- article = parse_article.call(article_url)
25
- memo.push(article) if article && !get_strage.include?(article)
26
- end
27
- end
28
-
29
- def update
30
- @logger.info('begin of update')
31
- get_new_articles.each do |article|
32
- begin
33
- yield article if block_given?
34
- @strage << article
35
- rescue
36
- @logger.error $!
37
- end
38
- end
39
- save_strage
40
- @logger.info('end of update')
41
- end
42
-
43
- def get_strage
44
- @strage ||= YAML.load_file(build_strage_path) || [] # fix when YAML.load_file is nil
45
- rescue
46
- @strage = []
47
- end
48
-
49
- def save_strage
50
- YAML.dump(@strage, File.new(build_strage_path, 'w'))
51
- end
52
-
53
- def build_strage_path
54
- class_name = /([^:]+)$/.match(self.class.to_s)[1]
55
- File.expand_path(File.dirname(__FILE__) + "/../../db/#{class_name}.yaml")
56
- end
57
-
58
- # define class method's
59
- def self.included(mod)
60
- mod.extend ClassMethods
61
- end
62
-
63
- module ClassMethods
64
- attr_accessor :get_resource, :parse_list, :parse_article
65
- end
66
- end
67
-
68
10
  module News
69
11
  def self.get_resource(url_path)
70
12
  Net::HTTP.get(URI.parse(url_path)).toutf8
@@ -120,24 +62,15 @@ module ConsadoleAggregator
120
62
  ],
121
63
  Consadolenews:
122
64
  [
123
- ->{ get_resource('http://www.consadole-sapporo.jp/news/diary.cgi') },
124
- ->(list){ Nokogiri::HTML(list).search('table.frametable > tr a').reverse },
125
- ->(article){ { url:article['href'], title:article.text } }
126
- ],
127
- Consadolesponsornews:
128
- [
129
- ->{ get_resource('http://www.consadole-sapporo.jp/snews/diary.cgi') },
130
- ->(list){ Nokogiri::HTML(list).search('table.frametable > tr a').reverse },
131
- ->(article){ { url:article['href'], title:article.text } }
65
+ ->{ get_resource('http://www.consadole-sapporo.jp/news/atom.xml') },
66
+ ->(list){ RSS::Parser.parse(list, false).items.map{ |e| { url:e.link, title:e.title } }.reverse },
67
+ ->(article){ article }
132
68
  ],
133
69
  Consadolephotos:
134
70
  [
135
- ->{ get_resource('http://www.consadole-sapporo.jp/comment.txt') },
136
- ->(list){ list.split("\n").reverse },
137
- ->(article){
138
- photo = article.match(/^&?text(?<number>\d\d)=(?<title>.+)/)
139
- { url:"http://www.consadole-sapporo.jp/img/#{photo[:number]}.jpg", title:photo[:title] }
140
- }
71
+ ->{ get_resource('http://www.consadole-sapporo.jp/') },
72
+ ->(list){ Nokogiri::HTML(list).search('div.anythingSlider img').reverse },
73
+ ->(article){ { url:article['src'], title:article['alt'] } }
141
74
  ],
142
75
  Jsgoalnews:
143
76
  [
@@ -1,5 +1,6 @@
1
1
  require 'logger'
2
2
  require_relative 'consadole_aggregator/helper.rb'
3
+ require_relative 'consadole_aggregator/aggregatable.rb'
3
4
  require_relative 'consadole_aggregator/live.rb'
4
5
  require_relative 'consadole_aggregator/news.rb'
5
6
 
@@ -7,37 +7,43 @@ describe ConsadoleAggregator do
7
7
  describe Live do
8
8
  describe '.get_resource' do
9
9
  it 'should exec Net::HTTP.get with Live::BASE_URI' do
10
- Net::HTTP.should_receive(:get).with(Live::BASE_URI).and_return(File.read(File.dirname(__FILE__) + '/../ext/live/s674.html'))
11
- Live.get_resource
10
+ Net::HTTP.should_receive(:get)
11
+ .with(URI.parse('http://www.consadole-sapporo.jp/view/s674.html'))
12
+ .and_return(File.read(File.dirname(__FILE__) + '/../ext/live/s674.html'))
13
+ Live::Live.get_resource.call
12
14
  end
13
15
  end
14
16
 
15
- describe '.parse' do
17
+ describe '.parse_list' do
16
18
  context 'when start of game' do
17
- before { Live.stub!(:get_resource).and_return(File.read(File.dirname(__FILE__) + '/../ext/live/s674.html').toutf8) }
18
- subject{ Live.parse }
19
+ let(:resource){ File.read(File.dirname(__FILE__) + '/../ext/live/s674.html').toutf8 }
20
+ let(:parsed_list){ Live::Live.parse_list.call(resource) }
21
+ subject{ parsed_list }
19
22
  it{ should have(3).items }
20
23
  end
21
24
  context 'when end of game' do
22
- before { Live.stub!(:get_resource).and_return(File.read(File.dirname(__FILE__) + '/../ext/live/s674.html.120').toutf8) }
25
+ let(:resource){ File.read(File.dirname(__FILE__) + '/../ext/live/s674.html.120').toutf8 }
26
+ let(:parsed_list){ Live::Live.parse_list.call(resource) }
23
27
  describe 'first TimeLine' do
24
- subject{ Live.parse.first }
25
- its(:time){ should == '試合開始' }
26
- its(:post){ should == '札幌ボールでキックオフ' }
28
+ subject{ parsed_list.first }
29
+ it{ should == '試合開始 札幌ボールでキックオフ' }
27
30
  end
28
31
  describe 'second TimeLine' do
29
- subject{ Live.parse[1] }
30
- its(:time){ should == '1分' }
31
- its(:post){ should == '右サイドからボールをつながれ攻撃を仕掛けられるが札幌DFが落ち着いてクリア' }
32
+ subject{ parsed_list[1] }
33
+ it{ should == '1分 右サイドからボールをつながれ攻撃を仕掛けられるが札幌DFが落ち着いてクリア' }
32
34
  end
33
35
  describe 'last TimeLine' do
34
- subject{ Live.parse.last }
35
- its(:time){ should == '試合終了' }
36
- its(:post){ should == 'ロスタイムも余裕のプレーで相手の攻撃を許さず、3試合連続完封で3連勝を飾る' }
36
+ subject{ parsed_list.last }
37
+ it{ should == '試合終了 ロスタイムも余裕のプレーで相手の攻撃を許さず、3試合連続完封で3連勝を飾る' }
37
38
  end
38
39
  end
39
40
  end
40
41
 
42
+ describe '.parse_article' do
43
+ let(:article){ '1分 右サイドからボールをつながれ攻撃を仕掛けられるが札幌DFが落ち着いてクリア' }
44
+ it{ Live::Live.parse_article.call(article) }
45
+ end
46
+
41
47
  describe '.reserve' do
42
48
  context 'given Time' do
43
49
  it 'give constructor with Time ' do
@@ -103,8 +103,6 @@ describe ConsadoleAggregator do
103
103
  ->{ File.read('./spec/ext/consaclub.txt').toutf8 }
104
104
  ConsadoleAggregator::News::Consadolenews.get_resource =
105
105
  ->{ File.read('./spec/ext/consadolenews.txt').toutf8 }
106
- ConsadoleAggregator::News::Consadolesponsornews.get_resource =
107
- ->{ File.read('./spec/ext/consadolesponsornews.txt').toutf8 }
108
106
  ConsadoleAggregator::News::Consadolephotos.get_resource =
109
107
  ->{ File.read('./spec/ext/consadolephotos.txt').toutf8 }
110
108
  ConsadoleAggregator::News::Jsgoalnews.get_resource =
@@ -137,14 +135,19 @@ describe ConsadoleAggregator do
137
135
  it 'Consaclub should not raise Exception' do
138
136
  expect{ ConsadoleAggregator::News::Consaclub.new.get_new_articles }.to_not raise_error
139
137
  end
140
- it 'Consadolenews should not raise Exception' do
141
- expect{ ConsadoleAggregator::News::Consadolenews.new.get_new_articles }.to_not raise_error
142
- end
143
- it 'Consadolesponsornews should not raise Exception' do
144
- expect{ ConsadoleAggregator::News::Consadolesponsornews.new.get_new_articles }.to_not raise_error
138
+ describe ConsadoleAggregator::News::Consadolenews do
139
+ subject{ ConsadoleAggregator::News::Consadolenews.new }
140
+ it 'Consadolenews should not raise Exception' do
141
+ expect{ subject.get_new_articles }.to_not raise_error
142
+ end
143
+ it{ subject.get_new_articles.should have_at_least(1).items }
145
144
  end
146
- it 'Consadolephotos should not raise Exception' do
147
- expect{ ConsadoleAggregator::News::Consadolephotos.new.get_new_articles }.to_not raise_error
145
+ describe ConsadoleAggregator::News::Consadolephotos do
146
+ subject{ ConsadoleAggregator::News::Consadolephotos.new }
147
+ it 'Consadolephotos should not raise Exception' do
148
+ expect{ subject.get_new_articles }.to_not raise_error
149
+ end
150
+ it{ subject.get_new_articles.should have_at_least(1).items }
148
151
  end
149
152
  it 'Jsgoalnews should not raise Exception' do
150
153
  expect{ ConsadoleAggregator::News::Jsgoalnews.new.get_new_articles }.to_not raise_error