fixes #3100
This commit is contained in:
42
app/logical/nico_seiga_api_client.rb
Normal file
42
app/logical/nico_seiga_api_client.rb
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
class NicoSeigaApiClient
|
||||||
|
BASE_URL = "http://seiga.nicovideo.jp/api"
|
||||||
|
attr_reader :user_id, :moniker, :image_id, :title, :desc
|
||||||
|
|
||||||
|
def initialize(illust_id)
|
||||||
|
get_illust(illust_id)
|
||||||
|
get_artist(user_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_illust(id)
|
||||||
|
uri = URI.parse("#{BASE_URL}/illust/info?id=#{id}")
|
||||||
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
||||||
|
resp = http.request_get(uri.request_uri)
|
||||||
|
if resp.is_a?(Net::HTTPSuccess)
|
||||||
|
parse_illust_xml_response(resp.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_artist(id)
|
||||||
|
uri = URI.parse("#{BASE_URL}/user/info?id=#{id}")
|
||||||
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.is_a?(URI::HTTPS)) do |http|
|
||||||
|
resp = http.request_get(uri.request_uri)
|
||||||
|
if resp.is_a?(Net::HTTPSuccess)
|
||||||
|
parse_artist_xml_response(resp.body)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_artist_xml_response(text)
|
||||||
|
doc = Nokogiri::Slop(text)
|
||||||
|
@moniker = doc.response.user.nickname.content
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_illust_xml_response(text)
|
||||||
|
doc = Nokogiri::Slop(text)
|
||||||
|
@image_id = doc.response.image.id.content.to_i
|
||||||
|
@user_id = doc.response.image.user_id.content.to_i
|
||||||
|
@title = doc.response.image.title.content
|
||||||
|
@desc = [doc.response.image.description.try(:content), doc.response.image.summary.try(:content)].compact.join("\n\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
module Sources
|
module Sources
|
||||||
module Strategies
|
module Strategies
|
||||||
class NicoSeiga < Base
|
class NicoSeiga < Base
|
||||||
|
extend Memoist
|
||||||
|
|
||||||
def self.url_match?(url)
|
def self.url_match?(url)
|
||||||
url =~ /^https?:\/\/(?:\w+\.)?nico(?:seiga|video)\.jp/
|
url =~ /^https?:\/\/(?:\w+\.)?nico(?:seiga|video)\.jp/
|
||||||
end
|
end
|
||||||
@@ -25,8 +27,9 @@ module Sources
|
|||||||
def get
|
def get
|
||||||
page = load_page
|
page = load_page
|
||||||
|
|
||||||
@artist_name, @profile_url = get_profile_from_page(page)
|
@artist_name, @profile_url = get_profile_from_api
|
||||||
@image_url = get_image_url_from_page(page)
|
@image_url = get_image_url_from_page(page)
|
||||||
|
@artist_commentary_title, @artist_commentary_desc = get_artist_commentary_from_api
|
||||||
|
|
||||||
# Log out before getting the tags.
|
# Log out before getting the tags.
|
||||||
# The reason for this is that if you're logged in and viewing a non-adult-rated work, the tags will be added with javascript after the page has loaded meaning we can't extract them easily.
|
# The reason for this is that if you're logged in and viewing a non-adult-rated work, the tags will be added with javascript after the page has loaded meaning we can't extract them easily.
|
||||||
@@ -47,12 +50,25 @@ module Sources
|
|||||||
|
|
||||||
def normalize_for_artist_finder!
|
def normalize_for_artist_finder!
|
||||||
page = load_page
|
page = load_page
|
||||||
@artist_name, @profile_url = get_profile_from_page(page)
|
@illust_id = get_illust_id_from_url
|
||||||
profile_url
|
@artist_name, @profile_url = get_profile_from_api
|
||||||
|
@profile_url + "/"
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
|
def api_client
|
||||||
|
NicoSeigaApiClient.new(get_illust_id_from_url)
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_illust_id_from_url
|
||||||
|
if normalized_url =~ %r!http://seiga.nicovideo.jp/seiga/im(\d+)!
|
||||||
|
$1.to_i
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def load_page
|
def load_page
|
||||||
page = agent.get(normalized_url)
|
page = agent.get(normalized_url)
|
||||||
|
|
||||||
@@ -66,18 +82,8 @@ module Sources
|
|||||||
page
|
page
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_profile_from_page(page)
|
def get_profile_from_api
|
||||||
links = page.search("li a").select {|x| x["href"] =~ /user\/illust/}
|
return [api_client.moniker, "http://seiga.nicovideo.jp/user/illust/#{api_client.user_id}"]
|
||||||
|
|
||||||
if links.any?
|
|
||||||
profile_url = "http://seiga.nicovideo.jp" + links[0]["href"]
|
|
||||||
artist_name = links[0].search("span")[0].children[0].text
|
|
||||||
else
|
|
||||||
profile_url = nil
|
|
||||||
artist_name = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
return [artist_name, profile_url].compact
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_image_url_from_page(page)
|
def get_image_url_from_page(page)
|
||||||
@@ -108,6 +114,10 @@ module Sources
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_artist_commentary_from_api
|
||||||
|
[api_client.title, api_client.desc]
|
||||||
|
end
|
||||||
|
|
||||||
def normalized_url
|
def normalized_url
|
||||||
@normalized_url ||= begin
|
@normalized_url ||= begin
|
||||||
if url =~ %r!\Ahttp://lohas\.nicoseiga\.jp/o/[a-f0-9]+/\d+/(\d+)!
|
if url =~ %r!\Ahttp://lohas\.nicoseiga\.jp/o/[a-f0-9]+/\d+/(\d+)!
|
||||||
@@ -163,6 +173,8 @@ module Sources
|
|||||||
mech
|
mech
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
memoize :api_client
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ class Artist < ActiveRecord::Base
|
|||||||
url = File.dirname(url) + "/"
|
url = File.dirname(url) + "/"
|
||||||
break if url =~ /pixiv\.net\/(?:img\/)?$/i
|
break if url =~ /pixiv\.net\/(?:img\/)?$/i
|
||||||
break if url =~ /lohas\.nicoseiga\.jp\/priv\/$/i
|
break if url =~ /lohas\.nicoseiga\.jp\/priv\/$/i
|
||||||
|
break if url =~ /nicovideo\.jp\/user\/illust/
|
||||||
break if url =~ /(?:data|media)\.tumblr\.com\/[a-z0-9]+\/$/i
|
break if url =~ /(?:data|media)\.tumblr\.com\/[a-z0-9]+\/$/i
|
||||||
break if url =~ /deviantart\.net\//i
|
break if url =~ /deviantart\.net\//i
|
||||||
break if url =~ %r!\Ahttps?://(?:mobile\.)?twitter\.com/\Z!i
|
break if url =~ %r!\Ahttps?://(?:mobile\.)?twitter\.com/\Z!i
|
||||||
|
|||||||
11
script/fixes/046_fix_nicovideo_artist_urls.rb
Normal file
11
script/fixes/046_fix_nicovideo_artist_urls.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'config', 'environment'))
|
||||||
|
|
||||||
|
ActiveRecord::Base.without_timeout do
|
||||||
|
ArtistUrl.where("normalized_url like ?", "\%nico\%").find_each do |url|
|
||||||
|
before = url.normalized_url
|
||||||
|
url.normalized_url = Sources::Site.new(before).normalize_for_artist_finder!
|
||||||
|
puts "#{before} -> #{url.normalized_url}" if before != url.normalized_url
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -250,6 +250,16 @@ class ArtistTest < ActiveSupport::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when finding nico seiga artists" do
|
||||||
|
setup do
|
||||||
|
FactoryGirl.create(:artist, :name => "osamari", :url_string => "http://seiga.nicovideo.jp/user/illust/7017777")
|
||||||
|
end
|
||||||
|
|
||||||
|
should "find the artist by the profile" do
|
||||||
|
assert_artist_found("osamari", "http://seiga.nicovideo.jp/seiga/im4937663")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "when finding twitter artists" do
|
context "when finding twitter artists" do
|
||||||
setup do
|
setup do
|
||||||
FactoryGirl.create(:artist, :name => "hammer_(sunset_beach)", :url_string => "http://twitter.com/hamaororon")
|
FactoryGirl.create(:artist, :name => "hammer_(sunset_beach)", :url_string => "http://twitter.com/hamaororon")
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ module Sources
|
|||||||
assert_equal("osamari", @site_2.artist_name)
|
assert_equal("osamari", @site_2.artist_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "get the artist commentary" do
|
||||||
|
assert_equal("コジコジ", @site_2.artist_commentary_title)
|
||||||
|
assert_equal("懐かしいですよね。テ また懐かしいものを ", @site_2.artist_commentary_desc)
|
||||||
|
end
|
||||||
|
|
||||||
should "get the image url" do
|
should "get the image url" do
|
||||||
assert_match(/^http:\/\/lohas\.nicoseiga\.jp\/priv\//, @site_1.image_url)
|
assert_match(/^http:\/\/lohas\.nicoseiga\.jp\/priv\//, @site_1.image_url)
|
||||||
assert_match(/^http:\/\/lohas\.nicoseiga\.jp\/priv\//, @site_2.image_url)
|
assert_match(/^http:\/\/lohas\.nicoseiga\.jp\/priv\//, @site_2.image_url)
|
||||||
|
|||||||
Reference in New Issue
Block a user