Skip to content

Instantly share code, notes, and snippets.

@helloitszak
Created July 3, 2014 19:23
Show Gist options
  • Save helloitszak/1af970f3a36475ce0924 to your computer and use it in GitHub Desktop.
Save helloitszak/1af970f3a36475ce0924 to your computer and use it in GitHub Desktop.
A swanky pixiv ugoku (those cool hip animated things) downloader.

Pixiv Ugoku

Downloads the frames from pixiv ugokus (like this one)

Works on R-18 as well without an account.

Requirements

You need a couple of gems.

  • nokogiri
  • rubyzip

Simply gem install those for maximum profit.

Tested on Ruby 2.1.2 but it should work on any 2.x and probably any 1.9 branch.

Usage

ruby pixiv_ugoku.rb illust_id [output_folder]

The illust_id in the case of http://www.pixiv.net/member_illust.php?mode=medium&illust_id=44320117 would be 44320117

Will download the image contained in illust_id to output_folder. By default it will output to the ugoku folder if you didn't specify one.

Help, Bugs, Commits

Hit me up on twitter as @ubercow or on Google+ as +ZakKristjanson if you have anything to add. I might make this into a gem later with a proper git repo, but for now it's staying as a simple gist.

License

The MIT License (MIT)

Copyright (c) 2014 Zak "Ubercow" Kristjanson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
require 'json'
require 'open-uri'
require 'nokogiri'
require 'zip'
require 'net/http'
if ARGV.length < 1
puts "Usage: #{$0} illust_id [outfolder]"
puts "outfolder defaults to 'ugoku'"
exit 1
end
UGOKU_REGEX = /pixiv.context.ugokuIllustData *= *({.+});/
pixiv_url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{ARGV[0]}"
outdir = ARGV[1].nil? ? "ugoku" : ARGV[1]
puts "Parsing #{pixiv_url}"
page = Nokogiri::HTML(open(pixiv_url))
match = UGOKU_REGEX.match(page.css("script").inner_text)
unless match
puts "Could not find ugokuIllustData on the page. sanity check?"
exit 1
end
ugoku_illust_data = JSON.parse(match[1])
high_quality_zip = ugoku_illust_data["src"].gsub(/(\d+_ugoira)(\d+x\d+).zip/, '\11920x1080.zip')
puts "High quality zip is: #{high_quality_zip}"
hq_uri = URI.parse(high_quality_zip)
zip_file = nil
Net::HTTP.start(hq_uri.host, hq_uri.port) do |http|
request = Net::HTTP::Get.new(hq_uri)
request.add_field("Referer", "http://www.pixiv.net")
response = http.request(request)
puts "Response Code: #{response.code}"
if response.code != '200'
puts "Something went wrong"
exit 1
end
zip_file = response.body
end
unless File.directory?(outdir)
FileUtils.mkdir_p(outdir)
end
Zip::InputStream.open(StringIO.new(zip_file)) do |io|
while entry = io.get_next_entry
frame = ugoku_illust_data["frames"].find { |x| x["file"] == entry.name }
unless frame
puts "Unknown file in zip: #{entry.name}, skipping."
next
end
file_ext = File.extname(entry.name)
file_without_ext = File.basename(entry.name, file_ext)
puts "Found Frame: #{file_without_ext} (Delay: #{frame["delay"]})"
new_filename = "#{file_without_ext}_#{frame["delay"]}#{file_ext}"
File.open(outdir + "/" + new_filename, 'w') do |file|
file.write(entry.get_input_stream.read)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment