Created
April 3, 2011 01:11
-
-
Save ssig33/900074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PixivImage | |
include Mongoid::Document | |
field :image_id | |
field :pixiv_user_id | |
field :title | |
field :tags, :type => Array | |
field :url | |
field :image_url | |
field :description | |
field :saved, :type => Boolean, :default => false | |
field :created_at, :type => Time | |
validates_uniqueness_of :image_id | |
def fetch | |
agent = Pixiv.agent | |
self.url = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=#{self.image_id}" | |
page = agent.get URI.parse(self.url) | |
self.title = page.root.xpath("//div[@class='works_data']/h3").inner_text | |
self.tags = page.root.xpath("//*[@id='tags']/a").to_ary.delete_if{|x| x["href"] == "_blank"}.collect{|x| x.inner_text}.delete_if{|x| x.empty?} | |
self.image_url = page.root.xpath('//div[@class="works_display"]/a/img').attr("src").to_s | |
self.description = page.root.xpath('//div[@class="works_area"]/p').inner_text | |
end | |
def user | |
PixivUser.where(:pixiv_id => self.pixiv_user_id).first | |
end | |
end | |
class PixivUser | |
include Mongoid::Document | |
field :name | |
field :pixiv_id | |
validates_uniqueness_of :pixiv_id | |
def images | |
agent = Pixiv.agent | |
page = agent.get URI.parse("http://www.pixiv.net/member_illust.php?id=#{self.pixiv_id}") | |
illusts = page.root.xpath '//div[@class="display_works linkStyleWorks"]' | |
links = illusts.xpath '//ul/li/a' | |
image_ids = [] | |
links.each do |l| | |
link_to = l.attr("href") | |
if /medium/ =~ link_to | |
link_to.scan(/[0-9]*/).each do |ids| | |
if ids and ids != "" | |
image_ids << ids | |
end | |
end | |
end | |
end | |
image_ids.reverse! | |
image_ids.each{|i| | |
p = PixivImage.new unless p = PixivImage.where(:image_id => i).first | |
p.image_id = i | |
p.fetch | |
} | |
PixivImage.where(:pixiv_user_id => self.pixiv_id).order("created_at desc") | |
end | |
def self.find_from_pixiv pixiv_id, agent | |
unless p = where(:pixiv_id => pixiv_id).first | |
p = new | |
end | |
page = agent.get "http://www.pixiv.net/member.php?id=#{pixiv_id}" | |
p.name = page.root.xpath('//div[@class="profile_area"]/h2/span').inner_text.split("\n").pop | |
p.pixiv_id = pixiv_id | |
p.save! | |
p | |
end | |
end | |
# Service | |
class Pixiv | |
def self.agent | |
count = 0 | |
begin | |
agent = Mechanize.new | |
page = agent.get URI.parse("http://www.pixiv.net/") | |
form = page.forms[0] | |
form.pixiv_id = "ID" | |
form.pass = "PASSWORD" | |
res = agent.submit(form) | |
return agent | |
rescue | |
count += 1 | |
if count < 10 | |
retry | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
require File.dirname(__FILE__) + '/spec_helper' | |
describe 'Application' do | |
describe Pixiv do | |
it "ログイン済みの Agent を取得出来る" do | |
agent = Pixiv.agent | |
agent.class.should == Mechanize | |
agent.page.root.xpath("//div[@class='profile_area']/h2").inner_text.should == "ssig33" | |
end | |
end | |
describe PixivUser do | |
it "ユーザー ID を指定して情報が Pixiv から取得出来る" do | |
p = PixivUser.find_from_pixiv "1048706", Pixiv.agent | |
p.pixiv_id.should == "1048706" | |
p.name.should == "ファーボはオワコン" | |
end | |
it "一度 Pixiv から取得された情報は MongoDB に保存される" do | |
p = PixivUser.find_from_pixiv "1048706", Pixiv.agent | |
p.pixiv_id.should == "1048706" | |
p.name.should == "ファーボはオワコン" | |
p = PixivUser.where(:name => "ファーボはオワコン").first | |
p.pixiv_id.should == "1048706" | |
p.name.should == "ファーボはオワコン" | |
end | |
it "画像一覧の取得が出来る" | |
end | |
describe PixivImage do | |
it "画像の詳細を取得出来る" do | |
p = PixivImage.new | |
p.image_id = "9827118" | |
p.fetch | |
p.url.should == "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=9827118" | |
p.title.should == "ボゴ・ザ・イケブクロシティボーイ" | |
p.description.should == "サンリオダブルパロ第三弾です!キラリと光るナイフでバイオレンスにキメたボゴ原ボゴ也さんです!" | |
p.user.name.should == "ファーボはオワコン" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment