Nicoseiga: rewrite tests and fix several bugs

* Fixed a bug where manga posts with a single tag would raise an error
* Fixed a bug where dic.nicovideo.jp/oekaki posts weren't uploadable due
  to SSL issues
* Added support for more manga corner cases
This commit is contained in:
nonamethanks
2022-09-29 00:46:47 +02:00
committed by N. Oname
parent d629c81aa1
commit d51cc17eaf
6 changed files with 212 additions and 179 deletions

View File

@@ -40,7 +40,7 @@ module Danbooru
attr_accessor :max_size, :http
class << self
delegate :get, :head, :put, :post, :delete, :cache, :follow, :max_size, :timeout, :auth, :basic_auth, :headers, :cookies, :use, :public_only, :download_media, to: :new
delegate :get, :head, :put, :post, :delete, :cache, :follow, :max_size, :timeout, :auth, :basic_auth, :headers, :cookies, :use, :public_only, :with_legacy_ssl, :download_media, to: :new
end
def initialize
@@ -136,6 +136,18 @@ module Danbooru
end
end
# allow requests to sites using unsafe legacy renegotiations (such as dic.nicovideo.jp)
# see https://github.com/openssl/openssl/commit/72d2670bd21becfa6a64bb03fa55ad82d6d0c0f3
def with_legacy_ssl
dup.tap do |o|
o.http = o.http.dup.tap do |http|
ctx = OpenSSL::SSL::SSLContext.new
ctx.options |= OpenSSL::SSL::OP_LEGACY_SERVER_CONNECT
http.default_options = http.default_options.with_ssl_context(ctx)
end
end
end
concerning :DownloadMethods do
# Download a file from `url` and return a {MediaFile}.
#

View File

@@ -19,12 +19,7 @@ class NicoSeigaApiClient
when "illust"
[api_response["id"]]
when "manga"
manga_api_response.map do |x|
case x["meta"]["source_url"]
when %r{/thumb/(\d+)\w}i then Regexp.last_match(1)
when %r{nicoseiga\.cdn\.nimg\.jp/drm/image/\w+/(\d+)\w}i then Regexp.last_match(1)
end
end
manga_api_response.map { |x| Source::URL.parse(x.dig("meta", "source_url"))&.image_id }.compact
end
end
@@ -37,11 +32,21 @@ class NicoSeigaApiClient
end
def tags
api_response.dig("tag_list", "tag").to_a.map { |t| t["name"] }.compact
tags = api_response.dig("tag_list", "tag")
if tags.instance_of? Hash
# When a manga post has a single tag, the XML parser thinks it's a hash instead of an array of hashes,
# so we need to manually turn it into the latter. Example: https://seiga.nicovideo.jp/watch/mg302561
tags = [tags]
end
tags.to_a.map { |t| t["name"] }.compact
end
def user_id
api_response["user_id"]
if api_response["user_id"].to_i == 0 # anonymous: https://nico.ms/mg310193
nil
else
api_response["user_id"]
end
end
def user_name
@@ -80,6 +85,7 @@ class NicoSeigaApiClient
end
def user_api_response(user_id)
return {} unless user_id.present?
resp = get("#{XML_API}/user/info?id=#{user_id}")
return {} if resp.blank? || resp.code.to_i == 404
Hash.from_xml(resp.to_s)["response"]["user"]

View File

@@ -95,6 +95,14 @@ module Source
parsed_url.manga_id || parsed_referer&.manga_id
end
def http
if parsed_url.oekaki_id.present?
super.with_legacy_ssl
else
super
end
end
def api_client
if illust_id.present?
NicoSeigaApiClient.new(work_id: illust_id, type: "illust", http: http)

View File

@@ -18,8 +18,7 @@
# Unhandled URLs
#
# * https://lohas.nicoseiga.jp/material/5746c5/4459092
# * https://dic.nicovideo.jp/oekaki/52833.png
#
module Source
class URL::NicoSeiga < Source::URL
attr_reader :illust_id, :manga_id, :image_id, :oekaki_id, :sm_video_id, :nm_video_id, :user_id, :username, :profile_url
@@ -92,6 +91,10 @@ module Source
in "dcdn.cdn.nimg.jpg", *, /^\d+$/ => image_id
@image_id = image_id
# https://deliver.cdn.nicomanga.jp/thumb/7891081p?1590171867
in "deliver.cdn.nicomanga.jp", "thumb", /^(\d+)p$/ => image_id
@image_id = $1
# https://deliver.cdn.nicomanga.jp/thumb/aHR0cHM6Ly9kZWxpdmVyLmNkbi5uaWNvbWFuZ2EuanAvdGh1bWIvODEwMDk2OHA_MTU2NTY5OTg4MA.webp (page: https://seiga.nicovideo.jp/watch/mg316708, full image: https://lohas.nicoseiga.jp/priv/1f6d38ef2ba6fc9d9e27823babc4cf721cef16ec/1646906617/8100969)
in "deliver.cdn.nicomanga.jp", *rest
# unhandled
@@ -131,7 +134,7 @@ module Source
# https://www.nicovideo.jp/user/4572975
# https://www.nicovideo.jp/user/20446930/mylist/28674289
in ("www.nicovideo.jp"), "user", /^\d+$/ => user_id, *rest
in "www.nicovideo.jp", "user", /^\d+$/ => user_id, *rest
@user_id = user_id
@profile_url = "https://www.nicovideo.jp/user/#{user_id}"
@@ -191,8 +194,8 @@ module Source
"https://www.nicovideo.jp/watch/sm#{sm_video_id}"
elsif oekaki_id.present?
"https://dic.nicovideo.jp/oekaki_id/#{oekaki_id}"
#elsif image_id.present?
# "https://seiga.nicovideo.jp/image/source/#{image_id}"
# elsif image_id.present?
# "https://seiga.nicovideo.jp/image/source/#{image_id}"
end
end
end