Fix #5354: Add bookmarklet support for picdig.net.
This commit is contained in:
@@ -59,6 +59,7 @@ module Source
|
||||
Source::Extractor::Bilibili,
|
||||
Source::Extractor::Rule34DotUs,
|
||||
Source::Extractor::FourChan,
|
||||
Source::Extractor::Picdig,
|
||||
]
|
||||
|
||||
# Should return true if the extractor is configured correctly. Return false
|
||||
|
||||
83
app/logical/source/extractor/picdig.rb
Normal file
83
app/logical/source/extractor/picdig.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# @see https://picdig.net/
|
||||
# @see Source::URL::Picdig
|
||||
module Source
|
||||
class Extractor
|
||||
class Picdig < Source::Extractor
|
||||
def match?
|
||||
Source::URL::Picdig === parsed_url
|
||||
end
|
||||
|
||||
def image_urls
|
||||
if parsed_url.image_url?
|
||||
[parsed_url.to_s]
|
||||
else
|
||||
image_urls_from_commentary
|
||||
end
|
||||
end
|
||||
|
||||
def image_urls_from_commentary
|
||||
html = Nokogiri::HTML5.fragment(artist_commentary_desc)
|
||||
html.css("img").map { |img| img[:src] }
|
||||
end
|
||||
|
||||
def page_url
|
||||
parsed_url.page_url || parsed_referer&.page_url
|
||||
end
|
||||
|
||||
def profile_url
|
||||
parsed_url.profile_url || parsed_referer&.profile_url
|
||||
end
|
||||
|
||||
def profile_urls
|
||||
[
|
||||
api_response.dig(:user, :homepage_url),
|
||||
api_response.dig(:user, :twitter_url),
|
||||
api_response.dig(:user, :instagram_url),
|
||||
api_response.dig(:user, :facebook_url),
|
||||
api_response.dig(:user, :youtube_url),
|
||||
].compact_blank
|
||||
end
|
||||
|
||||
def tag_name
|
||||
username
|
||||
end
|
||||
|
||||
def artist_name
|
||||
api_response.dig(:user, :name)
|
||||
end
|
||||
|
||||
def artist_commentary_title
|
||||
api_response[:title]
|
||||
end
|
||||
|
||||
def artist_commentary_desc
|
||||
api_response[:content]
|
||||
end
|
||||
|
||||
def tags
|
||||
api_response[:project_tags].to_a.map do |tag|
|
||||
[tag[:name], "https://picdig.net/projects?search_tag=#{Danbooru::URL.escape(tag[:name])}"]
|
||||
end
|
||||
end
|
||||
|
||||
def username
|
||||
parsed_url.username || parsed_referer&.username
|
||||
end
|
||||
|
||||
def api_url
|
||||
parsed_url.api_page_url || parsed_referer&.api_page_url
|
||||
end
|
||||
|
||||
memoize def api_response
|
||||
return {} if api_url.blank?
|
||||
|
||||
response = http.cache(1.minute).get(api_url)
|
||||
return {} unless response.status == 200
|
||||
|
||||
response.parse.with_indifferent_access.dig(:data, :project)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -62,6 +62,7 @@ module Source
|
||||
Source::URL::Bilibili,
|
||||
Source::URL::Rule34DotUs,
|
||||
Source::URL::FourChan,
|
||||
Source::URL::Picdig,
|
||||
]
|
||||
|
||||
# Parse a URL into a subclass of Source::URL, or raise an exception if the URL is not a valid HTTP or HTTPS URL.
|
||||
|
||||
61
app/logical/source/url/picdig.rb
Normal file
61
app/logical/source/url/picdig.rb
Normal file
@@ -0,0 +1,61 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# @see https://picdig.net/
|
||||
# @see Source::Extractor::Picdig
|
||||
class Source::URL::Picdig < Source::URL
|
||||
RESERVED_NAMES = %w[api articles images my privacy-policy projects terms]
|
||||
|
||||
attr_reader :username, :account_id, :user_id, :project_id, :image_id
|
||||
|
||||
def self.match?(url)
|
||||
url.domain == "picdig.net"
|
||||
end
|
||||
|
||||
def parse
|
||||
case [domain, *path_segments]
|
||||
|
||||
# full: https://picdig.net/images/98a85315-ade6-42c7-b54a-a1ab7dc0c7da/54e476f5-f956-497d-b689-0db7e745907d/2021/12/b35f9c35-a37f-47b0-a5b6-e639a4535ce3.jpg (page: https://picdig.net/supercanoyan/projects/71c55605-3eca-4660-991c-ee24b9a7b684)
|
||||
# thumb: https://picdig.net/images/98a85315-ade6-42c7-b54a-a1ab7dc0c7da/54e476f5-f956-497d-b689-0db7e745907d/2021/12/63fffa1f-2862-4aa6-80dc-b5a73d91ab43.png
|
||||
in _, "images", /^[0-9a-f-]{36}$/ => account_id, /^[0-9a-f-]{36}$/ => user_id, /^\d{4}$/ => year, /^\d{2}$/ => month, /^([0-9a-f-]{36})\.\w+/
|
||||
@account_id = account_id
|
||||
@user_id = user_id
|
||||
@image_id = $1
|
||||
|
||||
# avatar: https://picdig.net/images/98a85315-ade6-42c7-b54a-a1ab7dc0c7da/2021/12/9fadd3f4-c131-4f26-bce5-26c9d5bd4927.jpg
|
||||
in _, "images", /^[0-9a-f-]{36}$/ => account_id, /^\d{4}$/ => year, /^\d{2}$/ => month, /^([0-9a-f-]{36})\.\w+/
|
||||
@account_id = account_id
|
||||
@image_id = $1
|
||||
|
||||
# https://picdig.net/supercanoyan/projects/71c55605-3eca-4660-991c-ee24b9a7b684
|
||||
in _, username, "projects", /^[0-9a-f-]{36}$/ => project_id
|
||||
@username = username
|
||||
@project_id = project_id
|
||||
|
||||
# https://picdig.net/supercanoyan/portfolio
|
||||
# https://picdig.net/supercanoyan/profile
|
||||
# https://picdig.net/supercanoyan/collections
|
||||
# https://picdig.net/supercanoyan/articles
|
||||
in _, username, *rest unless username in RESERVED_NAMES
|
||||
@username = username
|
||||
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def image_url?
|
||||
image_id.present?
|
||||
end
|
||||
|
||||
def page_url
|
||||
"https://picdig.net/#{username}/projects/#{project_id}" if username.present? && project_id.present?
|
||||
end
|
||||
|
||||
def profile_url
|
||||
"https://picdig.net/#{username}/portfolio" if username.present?
|
||||
end
|
||||
|
||||
def api_page_url
|
||||
"https://picdig.net/api/users/#{username}/projects/#{project_id}" if username.present? && project_id.present?
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user