moebooru: delegate to substrategy based on post source (#3911).
If the yande.re or konachan.com post has a source from a supported site, for example Pixiv or Twitter, then delegate the artist and commentary lookup to that substrategy. Only do this for sources from recognized sites, not the null strategy.
This commit is contained in:
@@ -12,15 +12,12 @@ module Sources
|
||||
Strategies::Nijie,
|
||||
Strategies::Pawoo,
|
||||
Strategies::Moebooru,
|
||||
|
||||
Strategies::Null # MUST BE LAST!
|
||||
]
|
||||
end
|
||||
|
||||
def self.find(url, referer=nil)
|
||||
all
|
||||
.detect { |strategy| strategy.match?(url, referer) }
|
||||
.new(url, referer)
|
||||
def self.find(url, referer=nil, default: Strategies::Null)
|
||||
strategy = all.detect { |strategy| strategy.match?(url, referer) } || default
|
||||
strategy&.new(url, referer)
|
||||
end
|
||||
|
||||
def self.canonical(url, referer)
|
||||
|
||||
@@ -36,6 +36,8 @@ module Sources
|
||||
URL_SLUG = %r!/(?:yande\.re%20|Konachan\.com%20-%20)(?<id>\d+).*!i
|
||||
IMAGE_URL = %r!#{BASE_URL}/(?<type>image|jpeg|sample)/(?<md5>\h{32})#{URL_SLUG}?\.(?<ext>jpg|jpeg|png|gif)\z!i
|
||||
|
||||
delegate :artist_name, :profile_url, :unique_id, :artist_commentary_title, :artist_commentary_desc, :dtext_artist_commentary_title, :dtext_artist_commentary_desc, to: :sub_strategy, allow_nil: true
|
||||
|
||||
def self.match?(*urls)
|
||||
urls.compact.any? { |x| x.match?(BASE_URL) }
|
||||
end
|
||||
@@ -73,14 +75,6 @@ module Sources
|
||||
image_url
|
||||
end
|
||||
|
||||
def profile_url
|
||||
nil
|
||||
end
|
||||
|
||||
def artist_name
|
||||
nil
|
||||
end
|
||||
|
||||
def tags
|
||||
api_response[:tags].to_s.split.map do |tag|
|
||||
[tag.tr("_", " "), "https://#{site_name}/post?tags=#{CGI.escape(tag)}"]
|
||||
@@ -109,6 +103,10 @@ module Sources
|
||||
memoize :api_response
|
||||
|
||||
concerning :HelperMethods do
|
||||
def sub_strategy
|
||||
@sub_strategy ||= Sources::Strategies.find(api_response[:source], default: nil)
|
||||
end
|
||||
|
||||
def file_host
|
||||
case site_name
|
||||
when "yande.re" then "files.yande.re"
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
module Sources
|
||||
module Strategies
|
||||
class Null < Base
|
||||
def self.match?(*urls)
|
||||
true
|
||||
end
|
||||
|
||||
def image_urls
|
||||
[url]
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user