From ef51e8bc1ec4c8ae40d7a5c46cc32dfe342b3bf3 Mon Sep 17 00:00:00 2001 From: albert Date: Wed, 28 Sep 2011 18:46:28 -0400 Subject: [PATCH] added nico seiga sources --- app/logical/sources/strategies/nico_seiga.rb | 72 +++++++++++++++++--- config/danbooru_default_config.rb | 8 +++ 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/app/logical/sources/strategies/nico_seiga.rb b/app/logical/sources/strategies/nico_seiga.rb index 018f0b93d..dd2bd1235 100644 --- a/app/logical/sources/strategies/nico_seiga.rb +++ b/app/logical/sources/strategies/nico_seiga.rb @@ -1,28 +1,80 @@ module Sources module Strategies class NicoSeiga < Base + attr_reader :artist_name, :profile_url, :image_url, :tags + + def self.url_match?(url) + url =~ /^https?:\/\/(?:\w+\.)?nico(?:seiga|video)\.jp/ + end + def site_name "Nico Seiga" end - def artist_name - "?" + def get + url = URI.parse(normalized_url).request_uri + agent.get(url) 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 - def profile_url + def normalized_url url end - def image_url - url - end - - def tags - [] + def unique_id + profile_url =~ /\/illust\/(\d+)/ + "nicoseiga" + $1 end protected - def create_agent + def get_profile_from_page(page) + links = page.search("div.illust_user_name a") + + if links.any? + profile_url = "http://seiga.nicovideo.jp" + links[0]["href"] + artist_name = links[0].text.gsub(/<\/?strong>/, "") + else + profile_url = nil + artist_name = nil + end + + return [artist_name, profile_url].compact + end + + def get_image_url_from_page(page) + links = page.search("a#illust_link") + + if links.any? + "http://seiga.nicovideo.jp" + links[0]["href"] + else + nil + end + end + + def get_tags_from_page(page) + links = page.search("div#tag_block nobr a.tag") + + links.map do |node| + [node.text, "http://seiga.nicovideo.jp" + node.attr("href")] + end + end + + def agent + @agent ||= begin + mech = Mechanize.new + + mech.get("http://seiga.nicovideo.jp/login/redirect?next_url=") do |page| + page.form_with do |form| + form["mail"] = Danbooru.config.nico_seiga_login + form["password"] = Danbooru.config.nico_seiga_password + end.click_button + end + + mech + end end end end diff --git a/config/danbooru_default_config.rb b/config/danbooru_default_config.rb index 5c8818f21..dba26b9f5 100644 --- a/config/danbooru_default_config.rb +++ b/config/danbooru_default_config.rb @@ -250,5 +250,13 @@ module Danbooru def tinami_password nil end + + def nico_seiga_login + nil + end + + def nico_seiga_password + nil + end end end