Class: Pixiv::Client
- Inherits:
-
Object
- Object
- Pixiv::Client
- Defined in:
- lib/pixiv/client.rb
Instance Attribute Summary collapse
- #agent ⇒ Mechanize::HTTP::Agent readonly
- #member_id ⇒ Integer readonly
Class Method Summary collapse
-
.new_agent ⇒ Mechanize::HTTP::Agent
A new agent.
Instance Method Summary collapse
-
#bookmark_list(member_or_id = member_id, page = 1) ⇒ Pixiv::BookmarkList
Bookmark list bound to
self
. - #bookmarks(member_or_id = member_id, page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
-
#download_illust(illust, io_or_filename, size = :original) ⇒ Object
Downloads the image to
io_or_filename
. -
#download_manga(illust, pattern, &block) ⇒ Object
Downloads the images to
pattern
. -
#filename_from_pattern(pattern, illust, url) ⇒ String
private
Generate filename from
pattern
in context ofillust
andurl
. -
#illust(illust_id) ⇒ Pixiv::Illust
Illust bound to
self
. - #illusts(list, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
-
#initialize(*args) ⇒ Pixiv::Client
constructor
A new instance of Client, logged in with the given credentials.
-
#login(pixiv_id, password) ⇒ Object
Log in to Pixiv.
-
#member(member_id = member_id) ⇒ Pixiv::Member
Member bound to
self
. -
#private_bookmark_list(member_or_id = member_id, page = 1) ⇒ Pixiv::PrivateBookmarkList
Private bookmark list bound to
self
. - #private_bookmarks(page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
- #search(query, opts = {}) ⇒ Object
- #search_result_list(query, opts = {}) ⇒ Object
-
#work_list(member_or_id = member_id, page = 1) ⇒ Pixiv::WorkList
Work list bound to
self
. - #works(member_or_id = member_id, page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
Constructor Details
#initialize(pixiv_id, password) {|agent| ... } ⇒ Pixiv::Client #initialize(agent) ⇒ Pixiv::Client
A new instance of Client, logged in with the given credentials
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pixiv/client.rb', line 27 def initialize(*args) if args.size < 2 @agent = args.first || self.class.new_agent yield @agent if block_given? ensure_logged_in else pixiv_id, password = *args @agent = self.class.new_agent yield @agent if block_given? login(pixiv_id, password) end end |
Instance Attribute Details
#agent ⇒ Mechanize::HTTP::Agent (readonly)
15 16 17 |
# File 'lib/pixiv/client.rb', line 15 def agent @agent end |
#member_id ⇒ Integer (readonly)
17 18 19 |
# File 'lib/pixiv/client.rb', line 17 def member_id @member_id end |
Class Method Details
.new_agent ⇒ Mechanize::HTTP::Agent
A new agent
5 6 7 8 9 10 11 12 |
# File 'lib/pixiv/client.rb', line 5 def self.new_agent agent = Mechanize.new agent.max_history = 1 agent.pluggable_parser['image/gif'] = Mechanize::Download agent.pluggable_parser['image/jpeg'] = Mechanize::Download agent.pluggable_parser['image/png'] = Mechanize::Download agent end |
Instance Method Details
#bookmark_list(member_or_id = member_id, page = 1) ⇒ Pixiv::BookmarkList
Returns bookmark list bound to self
.
84 85 86 |
# File 'lib/pixiv/client.rb', line 84 def bookmark_list(member_or_id = member_id, page = 1) illust_list_with_class(BookmarkList, member_or_id, page) end |
#bookmarks(member_or_id = member_id, page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
122 123 124 |
# File 'lib/pixiv/client.rb', line 122 def bookmarks(member_or_id = member_id, page = 1, opts = {}) illusts(bookmark_list(member_or_id, page), opts) end |
#download_illust(illust, io_or_filename, size = :original) ⇒ Object
Downloads the image to io_or_filename
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/pixiv/client.rb', line 141 def download_illust(illust, io_or_filename, size = :original) size = {:s => :small, :m => :medium, :o => :original}[size] || size url = illust.__send__("#{size}_image_url") referer = case size when :small then nil when :medium then illust.url when :original then illust.url else raise ArgumentError, "unknown size `#{size}`" end save_to = io_or_filename if save_to.is_a?(Array) save_to = filename_from_pattern(save_to, illust, url) end FileUtils.mkdir_p(File.dirname(save_to)) unless save_to.respond_to?(:write) @agent.download(url, save_to, [], referer) end |
#download_manga(illust, pattern, &block) ⇒ Object
Document &block
illust#manga? must be true
Downloads the images to pattern
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/pixiv/client.rb', line 163 def download_manga(illust, pattern, &block) action = DownloadActionRegistry.new(&block) illust.original_image_urls.each_with_index do |url, n| begin action.before_each.call(url, n) if action.before_each filename = filename_from_pattern(pattern, illust, url) FileUtils.mkdir_p(File.dirname(filename)) @agent.download(url, filename, [], illust.original_image_referer) action.after_each.call(url, n) if action.after_each rescue action.on_error ? action.on_error.call($!) : raise end end end |
#filename_from_pattern(pattern, illust, url) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generate filename from pattern
in context of illust
and url
The pattern
is an array of string, symbol, or object that responds to #call
. Each component of the pattern
is replaced by the following rules and then the pattern
is concatenated as the returning filename
.
-
:image_name
in thepattern
is replaced with the base name of theurl
-
Any other symbol is replaced with the value of illust.send(the_symbol)
-
#call
-able object is replaced with the value of the_object.call(illust) -
String is left as-is
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pixiv/client.rb', line 194 def filename_from_pattern(pattern, illust, url) pattern.map {|i| if i == :image_name name = File.basename(url) if name =~ /\.(\w+)\?\d+$/ name += '.' + $1 end name elsif i.is_a?(Symbol) then illust.send(i) elsif i.respond_to?(:call) then i.call(illust) else i end }.join('') end |
#illust(illust_id) ⇒ Pixiv::Illust
Returns illust bound to self
.
65 66 67 68 69 |
# File 'lib/pixiv/client.rb', line 65 def illust(illust_id) attrs = {illust_id: illust_id} illust = Illust.lazy_new(attrs) { agent.get(Illust.url(illust_id)) } illust.bind(self) end |
#illusts(list, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
108 109 110 |
# File 'lib/pixiv/client.rb', line 108 def illusts(list, opts = {}) PageCollection::Enumerator.new(self, list, !!opts[:include_deleted]) end |
#login(pixiv_id, password) ⇒ Object
Log in to Pixiv
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pixiv/client.rb', line 43 def login(pixiv_id, password) doc = agent.get("https://accounts.pixiv.net/login?lang=ja&source=pc&view_type=page") return if doc && doc.body =~ /logout/ form = doc.forms_with(action: '/login').first puts doc.body and raise Error::LoginFailed, 'login form is not available' unless form form.pixiv_id = pixiv_id form.password = password doc = agent.submit(form) raise Error::LoginFailed unless doc && doc.body =~ /logout/ @member_id = member_id_from_mypage(doc) end |
#member(member_id = member_id) ⇒ Pixiv::Member
Returns member bound to self
.
57 58 59 60 61 |
# File 'lib/pixiv/client.rb', line 57 def member(member_id = member_id) attrs = {member_id: member_id} member = Member.lazy_new(attrs) { agent.get(Member.url(member_id)) } member.bind(self) end |
#private_bookmark_list(member_or_id = member_id, page = 1) ⇒ Pixiv::PrivateBookmarkList
Returns private bookmark list bound to self
.
91 92 93 |
# File 'lib/pixiv/client.rb', line 91 def private_bookmark_list(member_or_id = member_id, page = 1) illust_list_with_class(PrivateBookmarkList, member_or_id, page) end |
#private_bookmarks(page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
128 129 130 |
# File 'lib/pixiv/client.rb', line 128 def private_bookmarks(page = 1, opts = {}) illusts(private_bookmark_list(member_id, page), opts) end |
#search(query, opts = {}) ⇒ Object
133 134 135 |
# File 'lib/pixiv/client.rb', line 133 def search(query, opts = {}) illusts(search_result_list(query, opts)) end |
#search_result_list(query, opts = {}) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/pixiv/client.rb', line 95 def search_result_list(query, opts = {}) attrs = {query: query, search_opts: opts} SearchResultList.lazy_new(attrs) { agent.get(SearchResultList.url(query, opts)) }.bind(self) end |
#work_list(member_or_id = member_id, page = 1) ⇒ Pixiv::WorkList
Returns work list bound to self
.
74 75 76 77 78 79 |
# File 'lib/pixiv/client.rb', line 74 def work_list(member_or_id = member_id, page = 1) list = illust_list_with_class(WorkList, member_or_id, page) # Cheat; member_name will not found on your own work list. list.send(:set_attrs!, member_name: member.name) if list.owner_id == member_id list end |
#works(member_or_id = member_id, page = 1, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
115 116 117 |
# File 'lib/pixiv/client.rb', line 115 def works(member_or_id = member_id, page = 1, opts = {}) illusts(work_list(member_or_id, page), opts) end |