Files
danbooru/app/logical/sources/strategies/pixiv_sketch.rb
evazion df0bb70486 sources: factor out Source::URL::PixivSketch.
Add upload support for Pixiv Sketch. Fetch tags, commentary, and artist,
and rewrite sample images to full images.

Authentication isn't required. R18 images are hidden in the browser but
visible in the API.
2022-03-08 18:24:12 -06:00

89 lines
1.9 KiB
Ruby

# frozen_string_literal: true
# @see Source::URL::PixivSketch
module Sources
module Strategies
class PixivSketch < Base
extend Memoist
def match?
Source::URL::PixivSketch === parsed_url
end
def site_name
parsed_url.site_name
end
def image_urls
if parsed_url.image_url?
[parsed_url.full_image_url]
else
image_urls_from_api
end
end
def image_urls_from_api
api_response.dig("data", "media").to_a.pluck("photo").pluck("original").pluck("url2x")
end
def profile_url
"https://sketch.pixiv.net/@#{artist_name}" if artist_name.present?
end
def artist_name
api_response.dig("data", "user", "unique_name")
end
def other_names
[artist_name, display_name].compact
end
def profile_urls
[profile_url, pixiv_profile_url].compact
end
def artist_commentary_desc
api_response.dig("data", "text")
end
def tags
api_response.dig("data", "tags").to_a.map do |tag|
[tag, "https://sketch.pixiv.net/tags/#{tag}"]
end
end
def display_name
api_response.dig("data", "user", "name")
end
def pixiv_profile_url
"https://www.pixiv.net/users/#{pixiv_user_id}" if pixiv_user_id.present?
end
def pixiv_user_id
api_response.dig("data", "user", "pixiv_user_id")
end
# curl https://sketch.pixiv.net/api/items/5835314698645024323.json | jq
def api_response
return {} if api_url.blank?
response = http.cache(1.minute).get(api_url)
return {} if response.status == 404
response.parse
end
def page_url
parsed_url.page_url || parsed_referer&.page_url
end
def api_url
parsed_url.api_url || parsed_referer&.api_url
end
memoize :api_response
end
end
end