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_member_id = member_id, page_num = 1) ⇒ Object
- #bookmarks(list_or_member, 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
. -
#illust(illust_id) ⇒ Pixiv::Illust
Illust bound to
self
. -
#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
.
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_member_id = member_id, page_num = 1) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/pixiv/client.rb', line 73 def bookmark_list(member_or_member_id = member_id, page_num = 1) x = member_or_member_id member_id = x.is_a?(Member) ? x.member_id : x attrs = {member_id: member_id, page_num: page_num} BookmarkList.lazy_new(attrs) { agent.get(BookmarkList.url(member_id, page_num)) } end |
#bookmarks(list_or_member, opts = {}) ⇒ Pixiv::PageCollection::Enumerator
85 86 87 88 89 |
# File 'lib/pixiv/client.rb', line 85 def bookmarks(list_or_member, opts = {}) list = list_or_member.is_a?(BookmarkList) ? list_or_member : bookmark_list(list_or_member) PageCollection::Enumerator.new(self, list, !!opts[:include_deleted]) end |
#download_illust(illust, io_or_filename, size = :original) ⇒ Object
Downloads the image to io_or_filename
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pixiv/client.rb', line 95 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.original_image_referer 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
TODO:
Document &block
Note:
illust#manga? must be true
Downloads the images to pattern
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/pixiv/client.rb', line 117 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 |
#illust(illust_id) ⇒ Pixiv::Illust
57 58 59 60 61 |
# File 'lib/pixiv/client.rb', line 57 def illust(illust_id) attrs = {illust_id: illust_id} illust = Illust.lazy_new(attrs) { agent.get(Illust.url(illust_id)) } illust.bind(self) 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("#{ROOT_URL}/index.php") return if doc && doc.body =~ /logout/ form = doc.forms_with(action: '/login.php').first puts doc.body and raise Error::LoginFailed, 'login form is not available' unless form form.pixiv_id = pixiv_id form.pass = 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
65 66 67 68 69 |
# File 'lib/pixiv/client.rb', line 65 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 |