ruboty-bokete 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6544c8b122280deebf01f0a342b3613668b7568e
4
- data.tar.gz: 2035986702a98b650147b49d71870a94db8441a8
3
+ metadata.gz: 9f98edcf8358f16cdd3aaf186045b564ffdd87c6
4
+ data.tar.gz: 358ee77031f53d2c18bd97e950b0521e81561384
5
5
  SHA512:
6
- metadata.gz: 37dd6ebc7bb3643fce701cf2ce89619487bdd10b90564a8cd192658c5717e0cf724ade98772fbf8e6436a29ecd3a501223a305233625f8bcc4989a96d4c45fec
7
- data.tar.gz: 2df59c9df61898c661ae41ec034999065d25085adaee225dbf8350bea7482585b3480739823d43b3dc3caf81b685f8f06d8fb772df4a90aaaf8c849f751b5c09
6
+ metadata.gz: c589ffc54172226212e9c16fd62ce4f4277a727cabbcdb673099c4713adf6831c6c9203ea2421af03da33496b9c69e34380ea8b2b477a260119141d29cfea279
7
+ data.tar.gz: 3229a9e4169193e35ecf65c067bcc9c2cde0bd6d59c5bc9787da0bd336efbc3875d74a15789ecd7c9ba680a74c7faf05ea8a7128cc0ad3aa0e716fb5347bbb6d
@@ -1,47 +1,50 @@
1
1
  require 'mechanize'
2
2
 
3
- class Client
4
- BOKETE_URL = 'http://bokete.jp'
3
+ module Ruboty
4
+ module Bokete
5
+ class Client
6
+ BOKETE_URL = 'http://bokete.jp'
7
+
8
+ def initialize(max_results: 30)
9
+ @max_results = max_results
10
+ @agent = Mechanize.new
11
+ end
5
12
 
6
- def initialize(max_results: 30)
7
- @max_results = max_results
8
- @agent = Mechanize.new
9
- end
13
+ def get(mode)
14
+ results = []
10
15
 
11
- def get(mode)
12
- results = []
16
+ @agent.get("#{BOKETE_URL}/boke/#{mode}") do |page|
17
+ target_page = page
13
18
 
14
- @agent.get("#{BOKETE_URL}/boke/#{mode}") do |page|
15
- target_page = page
19
+ while results.size < @max_results
20
+ images = scraping_from_page(target_page)
21
+ results.concat(images)
16
22
 
17
- while results.size < @max_results
18
- images = scraping_from_page(target_page)
19
- results.concat(images)
23
+ target_page = get_next_page(target_page)
24
+ break if target_page.nil?
25
+ end
26
+ end
20
27
 
21
- target_page = get_next_page(target_page)
22
- break if target_page.nil?
28
+ results
23
29
  end
24
- end
25
-
26
- results
27
- end
28
30
 
29
- def scraping_from_page(page)
30
- images = []
31
-
32
- page.search('a>img').each do |node|
33
- image = node.attribute('src').value rescue nil
34
- alt = node.attribute('alt').value rescue nil
31
+ def scraping_from_page(page)
32
+ page.search('.boke').map do |node|
33
+ node.children.each_with_object({}) do |child, hash|
34
+ case child.name
35
+ when 'div'
36
+ img = child.children.children.children.detect {|o| o.name == 'img' }
37
+ hash[:url] ||= img.attribute('src').value rescue nil
38
+ when 'h3'
39
+ hash[:boke] = child.text
40
+ end
41
+ end
42
+ end
43
+ end
35
44
 
36
- if image && alt && image.match(%r|\A//|)
37
- images << "http:#{image}\n#{alt}"
45
+ def get_next_page(page)
46
+ page.links_with(href: /\?page/)[-1].click rescue nil
38
47
  end
39
48
  end
40
-
41
- images
42
- end
43
-
44
- def get_next_page(page)
45
- page.links_with(href: /\?page/)[-1].click rescue nil
46
49
  end
47
50
  end
@@ -1,5 +1,5 @@
1
1
  module Ruboty
2
2
  module Bokete
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -11,9 +11,9 @@ module Ruboty
11
11
  on /bokete (?<mode>.+)/, name: 'bokete', description: 'Get a bokete'
12
12
 
13
13
  def initialize(*args)
14
- super
14
+ super(*args)
15
15
 
16
- @client = Client.new
16
+ @client = Ruboty::Bokete::Client.new
17
17
  end
18
18
 
19
19
  def bokete(message)
@@ -33,7 +33,7 @@ module Ruboty
33
33
  end
34
34
 
35
35
  result = JSON.parse(brain[mode]).sample
36
- message.reply(result)
36
+ message.reply((result['url'].start_with?('//') ? 'http:' + result['url'] : result['url']) + "\n" + result['boke'])
37
37
  end
38
38
 
39
39
  def brain
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruboty-bokete
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-12 00:00:00.000000000 Z
11
+ date: 2016-07-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruboty
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
117
  version: '0'
118
118
  requirements: []
119
119
  rubyforge_project:
120
- rubygems_version: 2.2.2
120
+ rubygems_version: 2.5.1
121
121
  signing_key:
122
122
  specification_version: 4
123
123
  summary: Ruboty handler to get a bokete.