Files
danbooru/app/logical/nico_seiga_api_client.rb
2019-12-22 21:23:37 -06:00

84 lines
2.0 KiB
Ruby

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
end
def image_id
illust_xml["response"]["image"]["id"].to_i
end
def user_id
@user_id || illust_xml["response"]["image"]["user_id"].to_i
end
def title
illust_xml["response"]["image"]["title"]
end
def desc
illust_xml["response"]["image"]["description"]
end
def moniker
artist_xml["response"]["user"]["nickname"]
end
def illust_xml
get("#{BASE_URL}/illust/info?id=#{illust_id}")
end
def artist_xml
get("#{BASE_URL}/user/info?id=#{user_id}")
end
def get(url)
response = Danbooru::Http.cache(1.minute).get(url)
raise "nico seiga api call failed (code=#{response.code}, body=#{response.body})" if response.code != 200
Hash.from_xml(response.to_s)
end
memoize :artist_xml, :illust_xml
end