Files
danbooru/app/logical/sources/strategies/nico_seiga.rb
evazion cf8b8207e2 artists: change how artist urls are normalized.
Change how artist URLs are normalized in artist entries. Don't try to secretly
convert image URLs to profile URLs in artist entries. For example, if someone puts a
Pixiv image URL in an artist entry, don't secretly try to fetch the source and
convert it into a profile URL in the `normalized_url` field.

We did this because years ago, it was standard practice to put image URLs in artist
entries. Pixiv image URLs used to contain the artist's username, so we used to put
image URLs in artist entries for artist finding purposes. But Pixiv changed it so
that image URLs no longer contained the username, so we dealt with it by adding a
`normalized_url` column to artist_urls and secretly converting image URLs to profile
URLs in this field. But this is no longer necessary because now we don't normally put
image URLs in artist entries in the first place.

Now the `profile_url` method in `Source::URL` is used to normalize URLs in artist
entries. This lets us parse various profile URL formats and normalize them into a
single canonical form.

This also removes the `normalize_for_artist_finder` method from source strategies.
Instead the `profile_url` method is used for artist finding purposes. So the profile
URL returned by the source strategy needs to be the same as the URL in the artist
entry in order for artist finding to work.
2022-03-13 03:54:17 -05:00

116 lines
3.0 KiB
Ruby

# frozen_string_literal: true
# @see Source::URL::NicoSeiga
module Sources
module Strategies
class NicoSeiga < Base
def self.enabled?
Danbooru.config.nico_seiga_user_session.present?
end
def match?
Source::URL::NicoSeiga === parsed_url
end
def image_urls
if image_id.present?
[image_url_for("https://seiga.nicovideo.jp/image/source/#{image_id}")]
elsif illust_id.present?
[image_url_for("https://seiga.nicovideo.jp/image/source/#{illust_id}")]
elsif manga_id.present? && api_client.image_ids.present?
api_client.image_ids.map { |id| image_url_for("https://seiga.nicovideo.jp/image/source/#{id}") }
else
[image_url_for(url)]
end
end
def image_url_for(url)
return url if api_client.blank?
resp = api_client.head(url)
if resp.uri.to_s =~ %r{https?://.+/(\w+/\d+/\d+)\z}i
"https://lohas.nicoseiga.jp/priv/#{$1}"
else
url
end
end
def page_url
parsed_referer&.page_url || parsed_url.page_url
end
def profile_url
"https://seiga.nicovideo.jp/user/illust/#{api_client.user_id}" if api_client.user_id.present?
end
def artist_name
return if api_client.blank?
api_client.user_name
end
def artist_commentary_title
return if api_client.blank?
api_client.title
end
def artist_commentary_desc
return if api_client.blank?
api_client.description
end
def dtext_artist_commentary_desc
DText.from_html(artist_commentary_desc) do |element|
if element.name == "font" && element["color"] == "white"
element.content = "[spoiler]#{element.content}[/spoiler]"
end
end.gsub(/[^\w]im(\d+)/, ' seiga #\1 ').chomp
end
def normalize_for_source
page_url
end
def tag_name
return if api_client&.user_id.blank?
"nicoseiga#{api_client.user_id}"
end
def tags
return [] if api_client.blank?
base_url = "https://seiga.nicovideo.jp/"
base_url += "manga/" if manga_id.present?
base_url += "tag/"
api_client.tags.map do |name|
[name, base_url + CGI.escape(name)]
end
end
def image_id
parsed_url.image_id || parsed_referer&.image_id
end
def illust_id
parsed_url.illust_id || parsed_referer&.illust_id
end
def manga_id
parsed_url.manga_id || parsed_referer&.manga_id
end
def api_client
if illust_id.present?
NicoSeigaApiClient.new(work_id: illust_id, type: "illust", http: http)
elsif manga_id.present?
NicoSeigaApiClient.new(work_id: manga_id, type: "manga", http: http)
elsif image_id.present?
# We default to illust to attempt getting the api anyway
NicoSeigaApiClient.new(work_id: image_id, type: "illust", http: http)
end
end
memoize :api_client
end
end
end