pixiv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -1
- data/lib/pixiv/bookmark_list.rb +4 -2
- data/lib/pixiv/client.rb +46 -13
- data/lib/pixiv/page.rb +7 -0
- data/lib/pixiv/page_collection.rb +13 -5
- data/lib/pixiv/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -14,12 +14,16 @@ crawl the pixiv service. Do not abuse this library or you may be banned!
|
|
14
14
|
|
15
15
|
Add this line to your application's Gemfile:
|
16
16
|
|
17
|
-
gem 'pixiv'
|
17
|
+
gem 'pixiv'
|
18
18
|
|
19
19
|
And then execute:
|
20
20
|
|
21
21
|
$ bundle
|
22
22
|
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install pixiv
|
26
|
+
|
23
27
|
## Usage
|
24
28
|
|
25
29
|
See [a sample script](https://gist.github.com/4362297)
|
data/lib/pixiv/bookmark_list.rb
CHANGED
@@ -10,6 +10,8 @@ module Pixiv
|
|
10
10
|
"#{ROOT_URL}/bookmark.php?id=#{member_id}&rest=show&p=#{page_num}"
|
11
11
|
end
|
12
12
|
|
13
|
+
# @return [Integer]
|
14
|
+
lazy_attr_reader(:bookmarks_count) { at!('a[href="/bookmark.php?type=illust_all"]').inner_text.match(/(\d+)/).to_a[1].to_i }
|
13
15
|
# @return [Integer]
|
14
16
|
lazy_attr_reader(:page_num) { at!('li.pages-current').inner_text.to_i }
|
15
17
|
# @return [Boolean]
|
@@ -18,9 +20,9 @@ module Pixiv
|
|
18
20
|
lazy_attr_reader(:member_id) { doc.body.match(/pixiv\.context\.userId = '(\d+)'/).to_a[1].to_i }
|
19
21
|
# @return [Array<Integer>]
|
20
22
|
lazy_attr_reader(:illust_ids) { search!('li[id^="li_"] a[href^="member_illust.php?mode=medium"]').map {|n| n.attr('href').match(/illust_id=(\d+)$/).to_a[1].to_i } }
|
21
|
-
# @return [Array<Hash{Symbol=>Object}>]
|
23
|
+
# @return [Array<Hash{Symbol=>Object}, nil>]
|
22
24
|
lazy_attr_reader(:illust_hashes) {
|
23
|
-
search!('li[id^="li_"]').map {|node| illust_hash_from_bookmark_item(node) }
|
25
|
+
search!('li[id^="li_"]').map {|node| illust_hash_from_bookmark_item(node) }
|
24
26
|
}
|
25
27
|
|
26
28
|
alias page_hashes illust_hashes
|
data/lib/pixiv/client.rb
CHANGED
@@ -4,6 +4,7 @@ module Pixiv
|
|
4
4
|
# @return [Mechanize::HTTP::Agent]
|
5
5
|
def self.new_agent
|
6
6
|
agent = Mechanize.new
|
7
|
+
agent.max_history = 1
|
7
8
|
agent.pluggable_parser['image/gif'] = Mechanize::Download
|
8
9
|
agent.pluggable_parser['image/jpeg'] = Mechanize::Download
|
9
10
|
agent.pluggable_parser['image/png'] = Mechanize::Download
|
@@ -40,12 +41,14 @@ module Pixiv
|
|
40
41
|
# @param [String] pixiv_id
|
41
42
|
# @param [String] password
|
42
43
|
def login(pixiv_id, password)
|
43
|
-
|
44
|
-
|
44
|
+
doc = agent.get("#{ROOT_URL}/index.php")
|
45
|
+
return if doc && doc.body =~ /logout/
|
46
|
+
form = doc.forms_with(action: '/login.php').first
|
47
|
+
puts doc.body and raise Error::LoginFailed, 'login form is not available' unless form
|
45
48
|
form.pixiv_id = pixiv_id
|
46
49
|
form.pass = password
|
47
50
|
doc = agent.submit(form)
|
48
|
-
raise Error::LoginFailed unless doc.body =~ /logout/
|
51
|
+
raise Error::LoginFailed unless doc && doc.body =~ /logout/
|
49
52
|
@member_id = member_id_from_mypage(doc)
|
50
53
|
end
|
51
54
|
|
@@ -72,11 +75,15 @@ module Pixiv
|
|
72
75
|
BookmarkList.lazy_new(attrs) { agent.get(BookmarkList.url(member_id, page_num)) }
|
73
76
|
end
|
74
77
|
|
75
|
-
# @param [Pixiv::Member, Integer]
|
76
|
-
# @param [
|
77
|
-
|
78
|
-
|
79
|
-
|
78
|
+
# @param [Pixiv::BookmarkList, Pixiv::Member, Integer] list_or_member
|
79
|
+
# @param [Hash] opts
|
80
|
+
# @option opts [Boolean] :include_deleted (false)
|
81
|
+
# whether the returning enumerator yields deleted illust as +nil+ or not
|
82
|
+
# @return [Pixiv::PageCollection::Enumerator]
|
83
|
+
def bookmarks(list_or_member, opts = {})
|
84
|
+
list = list_or_member.is_a?(BookmarkList) ? list_or_member
|
85
|
+
: bookmark_list(list_or_member)
|
86
|
+
PageCollection::Enumerator.new(self, list, !!opts[:include_deleted])
|
80
87
|
end
|
81
88
|
|
82
89
|
# Downloads the image to +io_or_filename+
|
@@ -104,11 +111,19 @@ module Pixiv
|
|
104
111
|
# @param [Pixiv::Illust] illust the manga to download
|
105
112
|
# @param [Array<String, Symbol, #call>] pattern pattern (see {#filename_from_pattern})
|
106
113
|
# @note +illust#manga?+ must be +true+
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
114
|
+
# @todo Document +&block+
|
115
|
+
def download_manga(illust, pattern, &block)
|
116
|
+
action = DownloadActionRegistry.new(&block)
|
117
|
+
illust.original_image_urls.each_with_index do |url, n|
|
118
|
+
begin
|
119
|
+
action.before_each.call(url, n) if action.before_each
|
120
|
+
filename = filename_from_pattern(pattern, illust, url)
|
121
|
+
FileUtils.mkdir_p(File.dirname(filename))
|
122
|
+
@agent.download(url, filename, [], illust.original_image_referer)
|
123
|
+
action.after_each.call(url, n) if action.after_each
|
124
|
+
rescue
|
125
|
+
action.on_error ? action.on_error.call($!) : raise
|
126
|
+
end
|
112
127
|
end
|
113
128
|
end
|
114
129
|
|
@@ -154,4 +169,22 @@ module Pixiv
|
|
154
169
|
}.join('')
|
155
170
|
end
|
156
171
|
end
|
172
|
+
|
173
|
+
class DownloadActionRegistry
|
174
|
+
def initialize(&block)
|
175
|
+
instance_eval(&block) if block
|
176
|
+
end
|
177
|
+
|
178
|
+
def before_each(&block)
|
179
|
+
block ? (@before_each = block) : @before_each
|
180
|
+
end
|
181
|
+
|
182
|
+
def after_each(&block)
|
183
|
+
block ? (@after_each = block) : @after_each
|
184
|
+
end
|
185
|
+
|
186
|
+
def on_error(&block)
|
187
|
+
block ? (@on_error = block) : @on_error
|
188
|
+
end
|
189
|
+
end
|
157
190
|
end
|
data/lib/pixiv/page.rb
CHANGED
@@ -23,6 +23,13 @@ module Pixiv
|
|
23
23
|
set_attrs!(attrs)
|
24
24
|
end
|
25
25
|
|
26
|
+
# Whether +attr_name+ is fetched or not
|
27
|
+
# @param [String, Symbol] attr_name
|
28
|
+
# @return [Boolean]
|
29
|
+
def fetched?(attr_name = :doc)
|
30
|
+
instance_variable_get(:"@#{attr_name}") != nil
|
31
|
+
end
|
32
|
+
|
26
33
|
# @return [Nokogiri::HTTP::Document]
|
27
34
|
def doc
|
28
35
|
@doc ||= begin
|
@@ -32,14 +32,16 @@ module Pixiv
|
|
32
32
|
class PageCollection::Enumerator
|
33
33
|
include Enumerable
|
34
34
|
|
35
|
-
def initialize(client, collection)
|
35
|
+
def initialize(client, collection, include_deleted_page = false)
|
36
36
|
@client = client
|
37
37
|
@collection = collection
|
38
|
+
@include_deleted_page = include_deleted_page
|
38
39
|
end
|
39
40
|
|
40
41
|
def each_page
|
41
42
|
each_collection do |collection|
|
42
43
|
pages_from_collection(collection).each do |page|
|
44
|
+
next if page.nil? && !@include_deleted_page
|
43
45
|
yield page
|
44
46
|
end
|
45
47
|
end
|
@@ -56,7 +58,11 @@ module Pixiv
|
|
56
58
|
yield pages_from_collection(collection)
|
57
59
|
end
|
58
60
|
else
|
59
|
-
::Enumerator.new {|y|
|
61
|
+
::Enumerator.new {|y|
|
62
|
+
each_slice do |slice|
|
63
|
+
y << (@include_deleted_page ? slice.compact : slice)
|
64
|
+
end
|
65
|
+
}
|
60
66
|
end
|
61
67
|
end
|
62
68
|
end
|
@@ -65,8 +71,10 @@ module Pixiv
|
|
65
71
|
|
66
72
|
def pages_from_collection(collection)
|
67
73
|
collection.page_hashes.map {|attrs|
|
68
|
-
|
69
|
-
|
74
|
+
if attrs
|
75
|
+
url = attrs.delete(:url)
|
76
|
+
collection.page_class.lazy_new(attrs) { @client.agent.get(url) }
|
77
|
+
end
|
70
78
|
}
|
71
79
|
end
|
72
80
|
|
@@ -78,4 +86,4 @@ module Pixiv
|
|
78
86
|
end
|
79
87
|
end
|
80
88
|
end
|
81
|
-
end
|
89
|
+
end
|
data/lib/pixiv/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pixiv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mechanize
|