ruboty-monday_is_coming 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +22 -0
- data/lib/ruboty-monday_is_coming/pixiv_client.rb +115 -0
- data/lib/ruboty-monday_is_coming/version.rb +1 -1
- data/lib/ruboty/handlers/monday_is_coming.rb +9 -39
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d6b4bd2b6213d0c009261d5a7e124b66b61711b
|
4
|
+
data.tar.gz: e6b3812a63d2aed96bfaf3c6ad640c2d33943eb8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f316f15c4258432c7b481dffc394c28e7c2339b36a40f5408ef555f175266aa0d3df810afc57ab69056c404c18a31e693e9057d5483343cc164f56ad5b2c8cae
|
7
|
+
data.tar.gz: 89098270a20d488e6a66e2ba1ab5753ea7cc37c1b24d779abec59014e30c8ec360d768944b26661214ac1f689f30f5d12ef8be663f9f944e75341d7bcdeff50f
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Shota Fukumori (sora_h)
|
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.
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
require 'open-uri'
|
5
|
+
|
6
|
+
module Ruboty
|
7
|
+
module MondayIsComing
|
8
|
+
class PixivClient
|
9
|
+
USER_AGENT = "ruboty-monday_is_coming"
|
10
|
+
ACCESS_TOKEN_RETRIEVE_INTERVAL = 3000
|
11
|
+
|
12
|
+
CLIENT_ID = "bYGKuGVw91e0NMfPGp44euvGt59s"
|
13
|
+
CLIENT_SECRET = "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK"
|
14
|
+
|
15
|
+
OAUTH_TOKEN_URL = URI.parse("https://oauth.secure.pixiv.net/auth/token").freeze
|
16
|
+
|
17
|
+
def initialize(login, password)
|
18
|
+
@login, @password = login, password
|
19
|
+
@access_token = nil
|
20
|
+
@access_token_issued_at = nil
|
21
|
+
|
22
|
+
@lock = Mutex.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def work(id, image_sizes: %w(large), include_stats: true)
|
26
|
+
params = {
|
27
|
+
"image_sizes" => image_sizes.join(?,),
|
28
|
+
"include_stats" => (!!include_stats).inspect,
|
29
|
+
}
|
30
|
+
|
31
|
+
url = URI.parse("https://public-api.secure.pixiv.net/v1/works/#{id.to_i}.json")
|
32
|
+
url.query = URI.encode_www_form(params)
|
33
|
+
|
34
|
+
request_get(url)
|
35
|
+
end
|
36
|
+
|
37
|
+
def tag(tag, **kwargs)
|
38
|
+
works q: tag, mode: :exact_tag, **kwargs
|
39
|
+
end
|
40
|
+
|
41
|
+
def works(q:, mode: , per_page: 30, page: 1, order: :desc, sort: :date, period: :all, include_sanity_level: true, include_stats: true, image_sizes: %w(px_480mw large), profile_image_sizes: %w(px_170x170))
|
42
|
+
params = {
|
43
|
+
q: q.to_s,
|
44
|
+
mode: mode.to_s,
|
45
|
+
per_page: per_page.to_s,
|
46
|
+
page: page.to_s,
|
47
|
+
order: order.to_s,
|
48
|
+
sort: sort.to_s,
|
49
|
+
period: period.to_s,
|
50
|
+
include_sanity_level: (!!include_sanity_level).inspect,
|
51
|
+
include_stats: (!!include_stats).inspect,
|
52
|
+
image_sizes: image_sizes.join(?,),
|
53
|
+
profile_image_sizes: profile_image_sizes.join(?,),
|
54
|
+
}
|
55
|
+
|
56
|
+
url = URI.parse("https://public-api.secure.pixiv.net/v1/search/works.json")
|
57
|
+
url.query = URI.encode_www_form(params)
|
58
|
+
|
59
|
+
request_get(url)
|
60
|
+
end
|
61
|
+
|
62
|
+
def access_token
|
63
|
+
if !@access_token || (Time.now - @access_token_issued_at) > ACCESS_TOKEN_RETRIEVE_INTERVAL
|
64
|
+
issued_at = @access_token_issued_at
|
65
|
+
@lock.synchronize do
|
66
|
+
if !@access_token || issued_at < @access_token_issued_at
|
67
|
+
@access_token = get_access_token
|
68
|
+
@access_token_issued_at = Time.now
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
@access_token
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def request_get(url, headers = headers())
|
79
|
+
Net::HTTP.start(url.host, url.port, :use_ssl => true) do |http|
|
80
|
+
resp = http.request_get(url.request_uri, headers).tap(&:value)
|
81
|
+
JSON.parse(resp.body)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def headers
|
86
|
+
{
|
87
|
+
"Referer" => "http://spapi.pixiv-app.net",
|
88
|
+
"User-Agent" => USER_AGENT,
|
89
|
+
"Content-Type" => "application/x-www-form-urlencoded",
|
90
|
+
"Authorization" => "Bearer #{access_token}"
|
91
|
+
}
|
92
|
+
end
|
93
|
+
|
94
|
+
def get_access_token
|
95
|
+
headers = {
|
96
|
+
"Referer" => "http://www.pixiv.net",
|
97
|
+
}
|
98
|
+
params = {
|
99
|
+
"username" => @login,
|
100
|
+
"password" => @password,
|
101
|
+
"grant_type" => "password",
|
102
|
+
"client_id" => CLIENT_ID,
|
103
|
+
"client_secret" => CLIENT_SECRET,
|
104
|
+
}
|
105
|
+
|
106
|
+
Net::HTTP.start(OAUTH_TOKEN_URL.host, OAUTH_TOKEN_URL.port, use_ssl: true) do |http|
|
107
|
+
resp = http.request_post(OAUTH_TOKEN_URL.request_uri, URI.encode_www_form(params), headers).tap(&:value)
|
108
|
+
json = JSON.parse(resp.body)
|
109
|
+
json["response"]["access_token"]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require 'ruboty/handlers/base'
|
3
|
-
require '
|
4
|
-
require 'uri'
|
5
|
-
require 'csv'
|
3
|
+
require 'ruboty-monday_is_coming/pixiv_client'
|
6
4
|
require 'time'
|
7
5
|
|
8
6
|
module Ruboty
|
9
7
|
module Handlers
|
10
8
|
class MondayIsComing < Base
|
9
|
+
env 'PIXIV_LOGIN', 'pixiv user id, or email', optional: false
|
10
|
+
env 'PIXIV_PASSWORD', 'pixiv password', optional: false
|
11
|
+
|
11
12
|
on(/月曜日?(?:よりの使者)?/, name: 'show', description: '日高愛ちゃん')
|
12
13
|
on(/monday(?:\s+is\s+coming)?/, name: 'show', description: '日高愛ちゃん')
|
13
14
|
|
@@ -16,6 +17,7 @@ module Ruboty
|
|
16
17
|
super
|
17
18
|
@illusts_cache = nil
|
18
19
|
@illusts_cached_at = 0
|
20
|
+
@pixiv = Ruboty::MondayIsComing::PixivClient.new(ENV['PIXIV_LOGIN'], ENV['PIXIV_PASSWORD'])
|
19
21
|
end
|
20
22
|
|
21
23
|
def show(message)
|
@@ -30,11 +32,11 @@ module Ruboty
|
|
30
32
|
time = Time.now
|
31
33
|
illust = if time.monday?
|
32
34
|
beg = Time.local(time.year, time.month, time.day, 0, 0, 0, '+09:00')
|
33
|
-
illusts.select { |_| _[
|
35
|
+
illusts.select { |_| t = Time.parse("#{_['created_time']} +09:00") rescue nil; next unless t; t >= beg }.min || illusts.sample
|
34
36
|
else
|
35
37
|
illusts.sample
|
36
38
|
end
|
37
|
-
illust[
|
39
|
+
illust['image_urls']['px_480mw']
|
38
40
|
end
|
39
41
|
|
40
42
|
def illusts
|
@@ -46,41 +48,9 @@ module Ruboty
|
|
46
48
|
end
|
47
49
|
|
48
50
|
PIXIV_TARGET_TAG = "月曜日よりの使者"
|
49
|
-
PIXIV_TARGET_AUTHOR_ID =
|
51
|
+
PIXIV_TARGET_AUTHOR_ID = 2493100
|
50
52
|
def illusts_without_cache
|
51
|
-
|
52
|
-
end
|
53
|
-
|
54
|
-
PIXIV_SPAPI_SEARCH_TAG_URL = 'http://spapi.pixiv.net/iphone/search.php?s_mode=s_tag&word=%s'
|
55
|
-
def pixiv_search_tag(tag)
|
56
|
-
result = open(PIXIV_SPAPI_SEARCH_TAG_URL % [URI.encode_www_form_component(tag)], 'r', &:read)
|
57
|
-
lines = CSV.parse(result)
|
58
|
-
lines.map do |line|
|
59
|
-
{
|
60
|
-
id: line[0],
|
61
|
-
user_id: line[1],
|
62
|
-
extension: line[2],
|
63
|
-
image_title: line[3],
|
64
|
-
image_directory: line[4],
|
65
|
-
artist_nickname: line[5],
|
66
|
-
mobile_thumbnail_128_url: line[6],
|
67
|
-
mobile_thumbnail_max_width_480: line[9],
|
68
|
-
uploaded_at: Time.parse(line[12] + ' +09:00'),
|
69
|
-
tags: line[13],
|
70
|
-
software: line[14],
|
71
|
-
ratings_count: line[15],
|
72
|
-
score: line[16],
|
73
|
-
views: line[17],
|
74
|
-
image_description: line[18],
|
75
|
-
pages_count: line[19],
|
76
|
-
favorites_count: line[22],
|
77
|
-
comments_count: line[23],
|
78
|
-
artist_username: line[24],
|
79
|
-
r18: line[26],
|
80
|
-
series_id: line[27],
|
81
|
-
mobile_profile_image: line[29],
|
82
|
-
}
|
83
|
-
end
|
53
|
+
@pixiv.tag(PIXIV_TARGET_TAG, image_sizes: %w(px_480mw))['response'].select { |_| _['user']['id'] == PIXIV_TARGET_AUTHOR_ID }
|
84
54
|
end
|
85
55
|
end
|
86
56
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruboty-monday_is_coming
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shota Fukumori (sora_h)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -61,8 +61,10 @@ extra_rdoc_files: []
|
|
61
61
|
files:
|
62
62
|
- ".gitignore"
|
63
63
|
- Gemfile
|
64
|
+
- LICENSE.txt
|
64
65
|
- README.md
|
65
66
|
- Rakefile
|
67
|
+
- lib/ruboty-monday_is_coming/pixiv_client.rb
|
66
68
|
- lib/ruboty-monday_is_coming/version.rb
|
67
69
|
- lib/ruboty/handlers/monday_is_coming.rb
|
68
70
|
- lib/ruboty/monday_is_coming.rb
|
@@ -88,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
90
|
version: '0'
|
89
91
|
requirements: []
|
90
92
|
rubyforge_project:
|
91
|
-
rubygems_version: 2.
|
93
|
+
rubygems_version: 2.4.5
|
92
94
|
signing_key:
|
93
95
|
specification_version: 4
|
94
96
|
summary: ruboty monday is coming
|