Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created July 15, 2013 09:32
Show Gist options
  • Save Santarh/5998648 to your computer and use it in GitHub Desktop.
Save Santarh/5998648 to your computer and use it in GitHub Desktop.
Stats of pixiv bookmark's tag
#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-
require "rubygems"
require "mechanize"
class Pixiv
attr_accessor :agent
attr_accessor :my_id
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"
login_form = page.forms.at 2
login_form.pixiv_id = username
login_form.pass = password
login_form.submit
mypage = @agent.get "http://www.pixiv.net/mypage.php"
@my_id = mypage.search("a.user-link").attr("href").value.scan(/\d+/).first
end
end
#!/usr/local/bin/ruby
# -*- encoding: utf-8 -*-
require "./pixiv.rb"
def view_bookmark agent, user_id, page_number
url = "http://www.pixiv.net/bookmark.php?rest=show&id=#{user_id}&p=#{page_number.to_s}"
page = agent.get url
results = page.search "div.display_works>ul>li"
return false if results.first.text == "見つかりませんでした"
print "Searching ID:#{user_id}, PAGE:#{page_number}\n"
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
tags = illust_page.search "ul.tags>li.tag>a.text"
for tag in tags
$tag_hash[tag.text] = $tag_hash[tag.text] + 1
end
end
return true
end
pixiv = Pixiv.new
print "Whose ID: "
id = $stdin.gets.chop.to_i
id = pixiv.my_id if id == 0
print "\n"
$tag_hash = Hash.new
$tag_hash.default = 0
page_number = 1
loop do
success = view_bookmark pixiv.agent, id, page_number
break unless success
page_number += 1
end
f = File::open("tags_#{id}.csv", "w")
sorted_tags = $tag_hash.sort{|a, b| b[1] <=> a[1]}
sorted_tags.each do |pair|
f.puts(pair.join ",")
end
f.close
# print "START Page: "
# start_page = $stdin.gets.chop.to_i
# print "\n"
# print "END Page: "
# end_page = $stdin.gets.chop.to_i
# print "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment