From 8eb9bc612b7641293aeea58c18daa082e1a49a1d Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 29 Sep 2011 11:34:31 -0400 Subject: [PATCH] implemented pixa source --- app/logical/sources/strategies/pixa.rb | 74 ++++++++++++++++++++----- app/logical/sources/strategies/pixiv.rb | 4 -- config/danbooru_default_config.rb | 8 +++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/app/logical/sources/strategies/pixa.rb b/app/logical/sources/strategies/pixa.rb index 5e6995538..c79fce3da 100644 --- a/app/logical/sources/strategies/pixa.rb +++ b/app/logical/sources/strategies/pixa.rb @@ -1,28 +1,76 @@ module Sources module Strategies class Pixa < Base + attr_reader :artist_name, :profile_url, :image_url, :tags + + def self.url_match?(url) + url =~ /^https?:\/\/(?:\w+\.)?pixa\.cc/ + end + def site_name "Pixa" end - def artist_name - "?" + def unique_id + image_url =~ /\/img\/([^\/]+)/ + $1 end - def profile_url - url - end - - def image_url - url - end - - def tags - [] + def get + agent.get(URI.parse(url).request_uri) do |page| + @artist_name, @profile_url = get_profile_from_page(page) + @image_url = get_image_url_from_page(page) + @tags = get_tags_from_page(page) + end end protected - def create_agent + def get_profile_from_page(page) + links = page.search("p.profile_name a") + + if links.any? + profile_url = "http://www.pixa.cc" + links[0]["href"] + artist_name = links[0].text + return [artist_name, profile_url] + else + return [] + end + end + + def get_image_url_from_page(page) + img = page.search("img.illust_image").first + if img + img.attr("src") + else + nil + end + end + + def get_tags_from_page(page) + links = page.search("div#tag_list a") + + if links.any? + links.map do |node| + [node.inner_text, "http://www.pixa.cc" + node.attr("href")] + end + else + [] + end + end + + def agent + @agent ||= begin + mech = Mechanize.new + + mech.get("http://www.pixa.cc") do |page| + page.form_with(:action => "/session") do |form| + form['email'] = Danbooru.config.pixa_login + form['password'] = Danbooru.config.pixa_password + end.click_button + end + + mech + end end end end diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 4abc22c55..a723e5418 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -7,10 +7,6 @@ module Sources url =~ /^https?:\/\/(?:\w+\.)?pixiv\.net/ end - def initialize(url) - super - end - def site_name "Pixiv" end diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index dba26b9f5..ef5abaec7 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -258,5 +258,13 @@ module Danbooru def nico_seiga_password nil end + + def pixa_login + nil + end + + def pixa_password + nil + end end end