Files
danbooru/app/logical/sources/strategies/art_station.rb
evazion b4aea72d04 sources: remove preview_urls method from base strategy.
Remove the `preview_urls` method from strategies. The only place this was used was
when doing IQDB searches, to download the thumbnail image from the source instead of
the full image.

This wasn't worth it for a few reasons:

* Thumbnails on other sites are sometimes not the size we want, which could affect
  IQDB results.
* Grabbing thumbnails is complex for some sites. You can't always just rewrite the
  image URL. Sometimes it requires extra API calls, which can be slower than just
  grabbing the full image.
* For videos and animations, thumbnails from other sites don't always match our
  thumbnails. We do smart thumbnail generation to try to avoid blank thumbnails, which
  means we don't always pick the first frame, which could affect IQDB results.

API changes:

* /iqdb_queries?search[file_url] now downloads the URL as is without any modification.
  Before it tried to change thumbnail and sample size image URLs to the full version.

* /iqdb_queries?search[url] now returns an error if the URL is for a HTML page that
  contains multiple images. Before it would grab only the first image and silently
  ignore the rest.
2022-03-11 03:22:23 -06:00

103 lines
2.4 KiB
Ruby

# frozen_string_literal: true
# @see Source::URL::ArtStation
module Sources::Strategies
class ArtStation < Base
def match?
Source::URL::ArtStation === parsed_url
end
def site_name
parsed_url.site_name
end
def image_urls
@image_urls ||= image_urls_sub.map { |asset| asset_url(asset, :largest) }
end
def page_url
return nil if project_id.blank?
if artist_name.present?
"https://#{artist_name}.artstation.com/projects/#{project_id}"
else
"https://www.artstation.com/artwork/#{project_id}"
end
end
def profile_url
return nil if artist_name.blank?
"https://www.artstation.com/#{artist_name}"
end
def artist_name
artist_name_from_url || api_response.dig(:user, :username)
end
def artist_commentary_title
api_response[:title]
end
def artist_commentary_desc
api_response[:description]
end
def dtext_artist_commentary_desc
ActionView::Base.full_sanitizer.sanitize(artist_commentary_desc)
end
def tags
api_response[:tags].to_a.map do |tag|
[tag, "https://www.artstation.com/search?q=" + CGI.escape(tag)]
end
end
def normalize_for_source
return if project_id.blank?
if artist_name_from_url.present?
"https://#{artist_name_from_url}.artstation.com/projects/#{project_id}"
else
"https://www.artstation.com/artwork/#{project_id}"
end
end
def image_urls_sub
if parsed_url.image_url?
[url]
else
api_response[:assets].to_a.select { |asset| asset[:asset_type] == "image" }.pluck(:image_url)
end
end
def artist_name_from_url
parsed_url.username || parsed_referer&.username
end
def project_id
parsed_url.work_id || parsed_referer&.work_id
end
def api_response
return {} if project_id.blank?
resp = http.cache(1.minute).get("https://www.artstation.com/projects/#{project_id}.json")
return {} if resp.code != 200
resp.parse.with_indifferent_access
end
memoize :api_response
def asset_url(url, size)
parsed_url = Source::URL.parse(url)
image_sizes = %w[original 4k large medium small]
urls = image_sizes.map { |size| parsed_url.full_image_url(size) }
urls = urls.reverse if size == :smallest
chosen_url = urls.find { |url| http_exists?(url) }
chosen_url || url
end
end
end