Refactor sources
This commit is contained in:
@@ -1,182 +1,188 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class NicoSeiga < Base
|
||||
extend Memoist
|
||||
|
||||
def self.url_match?(url)
|
||||
url =~ /^https?:\/\/(?:\w+\.)?nico(?:seiga|video)\.jp/
|
||||
end
|
||||
URL = %r!\Ahttps?://(?:\w+\.)?nico(?:seiga|video)\.jp!
|
||||
DIRECT = %r!\Ahttps?://lohas\.nicoseiga\.jp/priv/[0-9a-f]+!
|
||||
PAGE = %r!\Ahttps?://seiga\.nicovideo\.jp/seiga/im(\d+)!i
|
||||
PROFILE = %r!\Ahttps?://seiga\.nicovideo\.jp/user/illust/(\d+)!i
|
||||
|
||||
def referer_url
|
||||
if @referer_url =~ /seiga\.nicovideo\.jp\/seiga\/im\d+/ && @url =~ /http:\/\/lohas\.nicoseiga\.jp\/(?:priv|o)\//
|
||||
@referer_url
|
||||
else
|
||||
@url
|
||||
end
|
||||
def self.match?(*urls)
|
||||
urls.compact.any? { |x| x.match?(URL) }
|
||||
end
|
||||
|
||||
def site_name
|
||||
"Nico Seiga"
|
||||
end
|
||||
|
||||
def unique_id
|
||||
profile_url =~ /\/illust\/(\d+)/
|
||||
"nicoseiga" + $1
|
||||
end
|
||||
|
||||
def get
|
||||
page = load_page
|
||||
|
||||
@artist_name, @profile_url = get_profile_from_api
|
||||
@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.
|
||||
# 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.
|
||||
# This does not apply if you're logged out (or if you're viewing an adult-rated work).
|
||||
agent.cookie_jar.clear!
|
||||
agent.get(normalized_url) do |page|
|
||||
@tags = get_tags_from_page(page)
|
||||
end
|
||||
end
|
||||
|
||||
def normalized_for_artist_finder?
|
||||
url =~ %r!https?://seiga\.nicovideo\.jp/user/illust/\d+/!i
|
||||
end
|
||||
|
||||
def normalizable_for_artist_finder?
|
||||
url =~ %r!https?://seiga\.nicovideo\.jp/seiga/im\d+!i
|
||||
end
|
||||
|
||||
def normalize_for_artist_finder!
|
||||
page = load_page
|
||||
@illust_id = get_illust_id_from_url
|
||||
@artist_name, @profile_url = get_profile_from_api
|
||||
@profile_url + "/"
|
||||
end
|
||||
|
||||
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
|
||||
page = agent.get(normalized_url)
|
||||
|
||||
if page.search("a#link_btn_login").any?
|
||||
# Session cache is invalid, clear it and log in normally.
|
||||
Cache.delete("nico-seiga-session")
|
||||
@agent = nil
|
||||
page = agent.get(normalized_url)
|
||||
def image_urls
|
||||
if url =~ DIRECT
|
||||
return [url]
|
||||
end
|
||||
|
||||
page
|
||||
end
|
||||
|
||||
def get_profile_from_api
|
||||
return [api_client.moniker, "http://seiga.nicovideo.jp/user/illust/#{api_client.user_id}"]
|
||||
end
|
||||
|
||||
def get_image_url_from_page(page)
|
||||
link = page.search("a#illust_link")
|
||||
|
||||
if link.any?
|
||||
image_url = "http://seiga.nicovideo.jp" + link[0]["href"]
|
||||
page = agent.get(image_url) # need to follow this redirect while logged in or it won't work
|
||||
|
||||
if page.is_a?(Mechanize::Image)
|
||||
return page.uri.to_s
|
||||
return [page.uri.to_s]
|
||||
end
|
||||
|
||||
images = page.search("div.illust_view_big").select {|x| x["data-src"] =~ /\/priv\//}
|
||||
|
||||
if images.any?
|
||||
image_url = "http://lohas.nicoseiga.jp" + images[0]["data-src"]
|
||||
end
|
||||
else
|
||||
image_url = nil
|
||||
end
|
||||
|
||||
return image_url
|
||||
end
|
||||
|
||||
def get_tags_from_page(page)
|
||||
links = page.search("a.tag")
|
||||
|
||||
links.map do |node|
|
||||
[node.text, "http://seiga.nicovideo.jp" + node.attr("href")]
|
||||
end
|
||||
end
|
||||
|
||||
def get_artist_commentary_from_api
|
||||
[api_client.title, api_client.desc]
|
||||
end
|
||||
|
||||
def normalized_url
|
||||
@normalized_url ||= begin
|
||||
if url =~ %r!\Ahttps?://lohas\.nicoseiga\.jp/o/[a-f0-9]+/\d+/(\d+)!
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/(\d+)\?e=\d+&h=[a-f0-9]+}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/[a-f0-9]+/\d+/(\d+)}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/(\d+)}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{\Ahttps?://lohas\.nicoseiga\.jp//?thumb/(\d+)i?}i
|
||||
"http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
elsif url =~ %r{/seiga/im\d+}
|
||||
url
|
||||
else
|
||||
nil
|
||||
return ["http://lohas.nicoseiga.jp" + images[0]["data-src"]]
|
||||
end
|
||||
end
|
||||
|
||||
raise "image url not found for (#{url}, #{referer_url})"
|
||||
end
|
||||
|
||||
def page_url
|
||||
[url, referer_url].each do |x|
|
||||
if x =~ %r!\Ahttps?://lohas\.nicoseiga\.jp/o/[a-f0-9]+/\d+/(\d+)!
|
||||
return "http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
end
|
||||
|
||||
if x =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/(\d+)\?e=\d+&h=[a-f0-9]+}i
|
||||
return "http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
end
|
||||
|
||||
if x =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/[a-f0-9]+/\d+/(\d+)}i
|
||||
return "http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
end
|
||||
|
||||
if x =~ %r{\Ahttps?://lohas\.nicoseiga\.jp/priv/(\d+)}i
|
||||
return "http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
end
|
||||
|
||||
if x =~ %r{\Ahttps?://lohas\.nicoseiga\.jp//?thumb/(\d+)i?}i
|
||||
return "http://seiga.nicovideo.jp/seiga/im#{$1}"
|
||||
end
|
||||
|
||||
if x =~ %r{/seiga/im\d+}
|
||||
return x
|
||||
end
|
||||
end
|
||||
|
||||
return super
|
||||
end
|
||||
|
||||
def profile_url
|
||||
if url =~ PROFILE
|
||||
return url
|
||||
end
|
||||
|
||||
"http://seiga.nicovideo.jp/user/illust/#{api_client.user_id}"
|
||||
end
|
||||
|
||||
def artist_name
|
||||
api_client.moniker
|
||||
end
|
||||
|
||||
def artist_commentary_title
|
||||
api_client.title
|
||||
end
|
||||
|
||||
def artist_commentary_desc
|
||||
api_client.desc
|
||||
end
|
||||
|
||||
def headers
|
||||
super.merge(
|
||||
"Referer" => "https://seiga.nicovideo.jp"
|
||||
)
|
||||
end
|
||||
|
||||
def normalized_for_artist_finder?
|
||||
url =~ PROFILE
|
||||
end
|
||||
|
||||
def normalizable_for_artist_finder?
|
||||
url =~ PAGE || url =~ PROFILE
|
||||
end
|
||||
|
||||
def normalize_for_artist_finder
|
||||
"#{profile_url}/"
|
||||
end
|
||||
|
||||
def unique_id
|
||||
"nicoseiga#{api_client.user_id}"
|
||||
end
|
||||
|
||||
def tags
|
||||
string = page.at("meta[name=keywords]").try(:[], "content") || ""
|
||||
string.split(/,/).map do |name|
|
||||
[name, "https://seiga.nicovideo.jp/tag/#{CGI.escape(name)}"]
|
||||
end
|
||||
end
|
||||
memoize :tags
|
||||
|
||||
public
|
||||
|
||||
def api_client
|
||||
NicoSeigaApiClient.new(illust_id)
|
||||
end
|
||||
memoize :api_client
|
||||
|
||||
def illust_id
|
||||
if page_url =~ PAGE
|
||||
return $1.to_i
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
def page
|
||||
doc = agent.get(page_url)
|
||||
|
||||
if doc.search("a#link_btn_login").any?
|
||||
# Session cache is invalid, clear it and log in normally.
|
||||
Cache.delete("nico-seiga-session")
|
||||
doc = agent.get(page_url)
|
||||
end
|
||||
|
||||
doc
|
||||
end
|
||||
memoize :page
|
||||
|
||||
def agent
|
||||
@agent ||= begin
|
||||
mech = Mechanize.new
|
||||
mech.redirect_ok = false
|
||||
mech.keep_alive = false
|
||||
mech = Mechanize.new
|
||||
mech.redirect_ok = false
|
||||
mech.keep_alive = false
|
||||
|
||||
session = Cache.get("nico-seiga-session")
|
||||
if session
|
||||
cookie = Mechanize::Cookie.new("user_session", session)
|
||||
cookie.domain = ".nicovideo.jp"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
else
|
||||
mech.get("https://account.nicovideo.jp/login") do |page|
|
||||
page.form_with(:id => "login_form") do |form|
|
||||
form["mail_tel"] = Danbooru.config.nico_seiga_login
|
||||
form["password"] = Danbooru.config.nico_seiga_password
|
||||
end.click_button
|
||||
end
|
||||
session = mech.cookie_jar.cookies.select{|c| c.name == "user_session"}.first
|
||||
if session
|
||||
Cache.put("nico-seiga-session", session.value, 1.month)
|
||||
else
|
||||
raise "Session not found"
|
||||
end
|
||||
end
|
||||
|
||||
# This cookie needs to be set to allow viewing of adult works
|
||||
cookie = Mechanize::Cookie.new("skip_fetish_warning", "1")
|
||||
cookie.domain = "seiga.nicovideo.jp"
|
||||
session = Cache.get("nico-seiga-session")
|
||||
if session
|
||||
cookie = Mechanize::Cookie.new("user_session", session)
|
||||
cookie.domain = ".nicovideo.jp"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech.redirect_ok = true
|
||||
mech
|
||||
else
|
||||
mech.get("https://account.nicovideo.jp/login") do |page|
|
||||
page.form_with(:id => "login_form") do |form|
|
||||
form["mail_tel"] = Danbooru.config.nico_seiga_login
|
||||
form["password"] = Danbooru.config.nico_seiga_password
|
||||
end.click_button
|
||||
end
|
||||
session = mech.cookie_jar.cookies.select{|c| c.name == "user_session"}.first
|
||||
if session
|
||||
Cache.put("nico-seiga-session", session.value, 1.month)
|
||||
else
|
||||
raise "Session not found"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
memoize :api_client
|
||||
# This cookie needs to be set to allow viewing of adult works
|
||||
cookie = Mechanize::Cookie.new("skip_fetish_warning", "1")
|
||||
cookie.domain = "seiga.nicovideo.jp"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech.redirect_ok = true
|
||||
mech
|
||||
end
|
||||
memoize :agent
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user