Skip to content

Instantly share code, notes, and snippets.

@Santarh
Last active December 13, 2015 22:38
Show Gist options
  • Save Santarh/4985536 to your computer and use it in GitHub Desktop.
Save Santarh/4985536 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-
require "rubygems"
require "mechanize"
class Pixiv
attr_accessor :agent
def initialize
@agent = Mechanize.new
@agent.user_agent_alias = "Mac Safari"
print "username: "
username = $stdin.gets.chop
print "password: "
system "stty -echo"
password = $stdin.gets.chop
system "ssty echo"
print "\n"
page = @agent.get "http://www.pixiv.net"
page.form.pixiv_id = username
page.form.pass = password
page.form.submit
end
end
def view_bookmarks agent, number
url = "http://www.pixiv.net/bookmark.php?rest=show&p=" + number.to_s
page = agent.get url
results = page.search "div.display_works>ul>li"
results.each do |r|
illust_link = r.search("a").first
if not illust_link
next
end
illust_url = "http://www.pixiv.net/" + illust_link.attributes["href"].value
illust_id = illust_url.scan(/illust_id=(\d+)/).first.first
illust_page = agent.get illust_url
thumb = illust_page.search("div.works_display>a>img")
if thumb.length == 0
next
end
if File.exists? illust_id or File.exists? "#{illust_id}.jpg"
p "id: #{illust_id} exists. end"
break
end
thumb_url = thumb.first.attributes["src"].value
image_url = thumb_url.gsub /(\d+)_m/, '\1'
meta = illust_page.search("ul.meta>li")
manga = meta.select{|m|m.text =~ /漫画/}
if manga.length == 1
page = manga.first.text.scan(/\d+/).first.to_i
p "Manga Downloading..."
system "mkdir #{illust_id}"
page.times do |i|
manga_url = image_url.gsub /\/(\d+)\.(\w+)/, "\/\\1_p#{i}.\\2"
extension = image_url.scan(/\/\d+\.(\w+)/).first.first
p manga_url
agent.get(manga_url, nil, illust_url).save("./#{illust_id}/#{i}.#{extension}")
end
else
p "Illust Downloading..."
p image_url
agent.get(image_url, nil, illust_url).save
end
end
end
pixiv = Pixiv.new
print "START Page: "
start_page = $stdin.gets.chop.to_i
print "\n"
print "END Page: "
end_page = $stdin.gets.chop.to_i
print "\n"
for i in start_page..end_page
p "START Page number: #{i}"
view_bookmarks pixiv.agent, i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment