Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A utility to download bookmarked items from pixiv
#!/usr/bin/env ruby
#
# Usage:
# export PIXIV_ID=<your pixiv ID>
# export PIXIV_PASSWORD=<your pixiv password>
# pixiv_bookmark_downloader.rb [download_dir]
#
# This script downloads your bookmarked items to
# <download_dir>/<member_id>/<image_name> (e.g. downloads/123/456.jpg)
#
require 'set'
require 'pixiv'
download_dir = ARGV[0] || 'downloads'
# Create a new client.
# You can configure agent (a Mechanize::HTTP::Agent)
# before logging in to pixiv.
pixiv = Pixiv.client(ENV['PIXIV_ID'], ENV['PIXIV_PASSWORD']) {|agent|
agent.user_agent_alias = 'Mac Safari'
#agent.set_proxy('127.0.0.1', 8080)
}
# Load or create a set to store ids of downloaded images.
seen = Marshal.load(open('seen.marshal')) rescue Set.new
at_exit do
puts 'Saving seen illust ids'
Marshal.dump(seen, open('seen.marshal', 'w'))
end
# Get the member object of logged-in user.
me = pixiv.member
puts "Start downloading bookmarks of #{ENV['PIXIV_ID']} (##{me.id})"
# Iterate over your bookmarks.
me.bookmarks.each do |illust|
# Skip known images.
if seen.include?(illust.id)
puts "Skipping #{illust.title} (##{illust.id})"
sleep 0.1
next
end
print "Downloading #{illust.title} (##{illust.id}) ... "
# Filename pattern:
# * :image_name is replaced with the name of the image
# * :member_id is replaced with the author's member id
dl_filename_pattern = [download_dir, '/', :member_id, '/', :image_name]
# Download images.
begin
if illust.manga?
pixiv.download_manga(illust, dl_filename_pattern)
else
pixiv.download_illust(illust, dl_filename_pattern)
end
puts 'done'
seen.add(illust.id)
puts 'Waiting for 3 seconds'
sleep 3
rescue
puts 'failed'
puts "Error: #{$!.message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment