ruboty-bokete 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +23 -0
- data/Rakefile +1 -0
- data/lib/ruboty/bokete.rb +9 -0
- data/lib/ruboty/bokete/client.rb +46 -0
- data/lib/ruboty/bokete/version.rb +5 -0
- data/lib/ruboty/handlers/bokete.rb +44 -0
- data/ruboty-bokete.gemspec +25 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc271d55e38ae4c506c53c76168d450545ba3f1d
|
4
|
+
data.tar.gz: 5f8dd41753c588f0d3396979d9c6d95e2b389135
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 785ad5cccc11ed8e9cec90eeb71b4d147ba620fd7cddcde59b2cb1c75a4ff38a3a07619b19db2fe3f914d44a95b79dffbe515d82504c86bb3705bbe29dcd98d3
|
7
|
+
data.tar.gz: 838b135ce6932b06cbe1c3889d1172744274edc7f4f800f1e39743f8891bb5f5ca59f2246dc2cc5214efd50ced5966860df9bcc30068584ab0b419495ec143ff
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 kaihar4
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Ruboty::Bokete
|
2
|
+
|
3
|
+
Ruboty handler to get a bokete.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ruboty-bokete'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
```
|
16
|
+
@ruboty bokete <mode>
|
17
|
+
```
|
18
|
+
|
19
|
+
### Available modes
|
20
|
+
|
21
|
+
```
|
22
|
+
['recent', 'hot', 'popular', 'pickup', 'select', 'legend']
|
23
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'mechanize'
|
2
|
+
|
3
|
+
class Client
|
4
|
+
BOKETE_URL = 'http://bokete.jp'
|
5
|
+
|
6
|
+
def initialize(max_results: 30)
|
7
|
+
@max_results = max_results
|
8
|
+
@results = []
|
9
|
+
@agent = Mechanize.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def get(mode)
|
13
|
+
@agent.get("#{BOKETE_URL}/boke/#{mode}") do |page|
|
14
|
+
target_page = page
|
15
|
+
|
16
|
+
while @results.size < @max_results
|
17
|
+
images = scraping_from_page(target_page)
|
18
|
+
@results.concat(images)
|
19
|
+
|
20
|
+
target_page = get_next_page(target_page)
|
21
|
+
break if target_page.nil?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@results
|
26
|
+
end
|
27
|
+
|
28
|
+
def scraping_from_page(page)
|
29
|
+
images = []
|
30
|
+
|
31
|
+
page.search('a>img').each do |node|
|
32
|
+
image = node.attribute('src').value rescue nil
|
33
|
+
alt = node.attribute('alt').value rescue nil
|
34
|
+
|
35
|
+
if image && alt && image.match(%r|\A//|)
|
36
|
+
images << "http:#{image}\n#{alt}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
images
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_next_page(page)
|
44
|
+
page.links_with(href: /\?page/)[-1].click rescue nil
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
require 'redis'
|
4
|
+
|
5
|
+
module Ruboty
|
6
|
+
module Handlers
|
7
|
+
class Bokete < Base
|
8
|
+
AVAILABLE_MODES = ['recent', 'hot', 'popular', 'pickup', 'select', 'legend']
|
9
|
+
CACHE_EXPIRE = 10800
|
10
|
+
|
11
|
+
on /bokete (?<mode>.+)/, name: 'bokete', description: 'Get a bokete'
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
super
|
15
|
+
|
16
|
+
@client = Client.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def bokete(message)
|
20
|
+
mode = message[:mode]
|
21
|
+
|
22
|
+
unless AVAILABLE_MODES.include?(mode)
|
23
|
+
message.reply("Usage: [#{AVAILABLE_MODES.join('|')}]")
|
24
|
+
return
|
25
|
+
end
|
26
|
+
|
27
|
+
cache_updated = brain['cache_updated'].to_i
|
28
|
+
if !brain[mode] || cache_updated + CACHE_EXPIRE < Time.now.to_i
|
29
|
+
results = @client.get(mode)
|
30
|
+
|
31
|
+
brain[mode] = results.to_json
|
32
|
+
brain['cache_updated'] = Time.now.to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
result = JSON.parse(brain[mode]).sample
|
36
|
+
message.reply(result)
|
37
|
+
end
|
38
|
+
|
39
|
+
def brain
|
40
|
+
robot.brain.data[Ruboty::Bokete::NAMESPACE] ||= {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path('../lib', __FILE__)
|
2
|
+
require 'ruboty/bokete/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'ruboty-bokete'
|
6
|
+
spec.version = Ruboty::Bokete::VERSION
|
7
|
+
spec.authors = ['kaihar4']
|
8
|
+
spec.email = ['kaihar4@gmail.com']
|
9
|
+
spec.summary = 'Ruboty handler to get a bokete.'
|
10
|
+
spec.description = spec.summary
|
11
|
+
spec.homepage = 'https://github.com/kaihar4/ruboty-bokete'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_dependency 'ruboty'
|
20
|
+
spec.add_dependency 'redis'
|
21
|
+
spec.add_dependency 'mechanize'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruboty-bokete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- kaihar4
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruboty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: redis
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mechanize
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Ruboty handler to get a bokete.
|
84
|
+
email:
|
85
|
+
- kaihar4@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- lib/ruboty/bokete.rb
|
96
|
+
- lib/ruboty/bokete/client.rb
|
97
|
+
- lib/ruboty/bokete/version.rb
|
98
|
+
- lib/ruboty/handlers/bokete.rb
|
99
|
+
- ruboty-bokete.gemspec
|
100
|
+
homepage: https://github.com/kaihar4/ruboty-bokete
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.2.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Ruboty handler to get a bokete.
|
124
|
+
test_files: []
|