add support for nico seiga manga (fixes #4060)
This commit is contained in:
@@ -1,8 +1,47 @@
|
||||
# http://seiga.nicovideo.jp/api/manga/info?id=376206
|
||||
# http://api.search.nicovideo.jp/api/v2/manga/contents/search?
|
||||
|
||||
class NicoSeigaApiClient
|
||||
extend Memoist
|
||||
BASE_URL = "http://seiga.nicovideo.jp/api"
|
||||
attr_reader :illust_id
|
||||
|
||||
def self.agent
|
||||
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.week)
|
||||
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"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech.redirect_ok = true
|
||||
mech
|
||||
end
|
||||
|
||||
def initialize(illust_id:, user_id: nil)
|
||||
@illust_id = illust_id
|
||||
@user_id = user_id
|
||||
|
||||
58
app/logical/nico_seiga_manga_api_client.rb
Normal file
58
app/logical/nico_seiga_manga_api_client.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
class NicoSeigaMangaApiClient
|
||||
extend Memoist
|
||||
BASE_URL = "http://seiga.nicovideo.jp/api"
|
||||
attr_reader :theme_id
|
||||
|
||||
def initialize(theme_id)
|
||||
@theme_id = theme_id
|
||||
end
|
||||
|
||||
def user_id
|
||||
theme_info_xml["response"]["theme"]["user_id"].to_i
|
||||
end
|
||||
|
||||
def title
|
||||
theme_info_xml["response"]["theme"]["title"]
|
||||
end
|
||||
|
||||
def desc
|
||||
theme_info_xml["response"]["theme"]["description"]
|
||||
end
|
||||
|
||||
def moniker
|
||||
artist_xml["response"]["user"]["nickname"]
|
||||
end
|
||||
|
||||
def image_ids
|
||||
theme_data_xml["response"]["image_list"]["image"].map {|x| x["id"]}
|
||||
end
|
||||
|
||||
def tags
|
||||
theme_info_xml["response"]["theme"]["tag_list"]["tag"].map {|x| x["name"]}
|
||||
end
|
||||
|
||||
def theme_data_xml
|
||||
uri = "#{BASE_URL}/theme/data?theme_id=#{theme_id}"
|
||||
body = NicoSeigaApiClient.agent.get(uri).body
|
||||
Hash.from_xml(body)
|
||||
end
|
||||
memoize :theme_data_xml
|
||||
|
||||
def theme_info_xml
|
||||
uri = "#{BASE_URL}/theme/info?id=#{theme_id}"
|
||||
body = NicoSeigaApiClient.agent.get(uri).body
|
||||
Hash.from_xml(body)
|
||||
end
|
||||
memoize :theme_info_xml
|
||||
|
||||
def artist_xml
|
||||
uri = "#{BASE_URL}/user/info?id=#{user_id}"
|
||||
body, code = HttpartyCache.get(uri)
|
||||
if code == 200
|
||||
Hash.from_xml(body)
|
||||
else
|
||||
raise "nico seiga api call failed (code=#{code}, body=#{body})"
|
||||
end
|
||||
end
|
||||
memoize :artist_xml
|
||||
end
|
||||
@@ -3,7 +3,8 @@ module Sources
|
||||
def self.all
|
||||
return [
|
||||
Strategies::Pixiv,
|
||||
Strategies::NicoSeiga,
|
||||
Strategies::NicoSeigaManga, # must come before NicoSeiga
|
||||
Strategies::NicoSeiga,
|
||||
Strategies::Twitter,
|
||||
Strategies::Stash, # must come before DeviantArt
|
||||
Strategies::DeviantArt,
|
||||
|
||||
@@ -153,39 +153,7 @@ module Sources
|
||||
memoize :page
|
||||
|
||||
def agent
|
||||
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"
|
||||
cookie.path = "/"
|
||||
mech.cookie_jar.add(cookie)
|
||||
|
||||
mech.redirect_ok = true
|
||||
mech
|
||||
NicoSeigaApiClient.agent
|
||||
end
|
||||
memoize :agent
|
||||
end
|
||||
|
||||
71
app/logical/sources/strategies/nico_seiga_manga.rb
Normal file
71
app/logical/sources/strategies/nico_seiga_manga.rb
Normal file
@@ -0,0 +1,71 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class NicoSeigaManga < Base
|
||||
PAGE_URL = %r!\Ahttps?://seiga\.nicovideo\.jp/watch/mg(\d+)!i
|
||||
|
||||
def domains
|
||||
["nicoseiga.jp", "nicovideo.jp"]
|
||||
end
|
||||
|
||||
def site_name
|
||||
"Nico Seiga (manga)"
|
||||
end
|
||||
|
||||
def image_urls
|
||||
api_client.image_ids.map do |image_id|
|
||||
"https://seiga.nicovideo.jp/image/source/#{image_id}"
|
||||
end
|
||||
end
|
||||
|
||||
def page_url
|
||||
[url, referer_url].each do |x|
|
||||
if x =~ PAGE_URL
|
||||
return x
|
||||
end
|
||||
end
|
||||
|
||||
return super
|
||||
end
|
||||
|
||||
def canonical_url
|
||||
image_url
|
||||
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 theme_id
|
||||
if page_url =~ PAGE_URL
|
||||
return $1
|
||||
end
|
||||
end
|
||||
|
||||
def api_client
|
||||
NicoSeigaMangaApiClient.new(theme_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user