From 7d5d098636109adc6e0d2c39418600f9389a485f Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 10 Sep 2018 18:47:13 -0500 Subject: [PATCH] Fix #3877: Add sta.sh strategy. Co-authored-by: lllusion3469 <31420484+lllusion3469@users.noreply.github.com> --- app/logical/sources/strategies.rb | 1 + app/logical/sources/strategies/deviant_art.rb | 10 +++-- app/logical/sources/strategies/stash.rb | 39 +++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 app/logical/sources/strategies/stash.rb diff --git a/app/logical/sources/strategies.rb b/app/logical/sources/strategies.rb index aad194f3e..709b42915 100644 --- a/app/logical/sources/strategies.rb +++ b/app/logical/sources/strategies.rb @@ -5,6 +5,7 @@ module Sources Strategies::Pixiv, Strategies::NicoSeiga, Strategies::Twitter, + Strategies::Stash, # must come before DeviantArt Strategies::DeviantArt, Strategies::Tumblr, Strategies::ArtStation, diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index 6fd513a0f..b0033dee3 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -198,15 +198,19 @@ module Sources self.class.artist_name_from_url(url) || self.class.artist_name_from_url(referer_url) end - def page + def api_url return nil if deviation_id.blank? - deviation_url = "https://www.deviantart.com/deviation/#{deviation_id}" + "https://www.deviantart.com/deviation/#{deviation_id}" + end + + def page + return nil if api_url.blank? options = Danbooru.config.httparty_options.deep_merge( format: :plain, headers: { "Accept-Encoding" => "gzip" } ) - resp = HTTParty.get(deviation_url, **options) + resp = HTTParty.get(api_url, **options) if resp.success? body = Zlib.gunzip(resp.body) diff --git a/app/logical/sources/strategies/stash.rb b/app/logical/sources/strategies/stash.rb new file mode 100644 index 000000000..8394add28 --- /dev/null +++ b/app/logical/sources/strategies/stash.rb @@ -0,0 +1,39 @@ +module Sources + module Strategies + class Stash < DeviantArt + STASH = %r{\Ahttps?://sta\.sh/(?[0-9a-zA-Z]+)}i + + def self.match?(*urls) + urls.compact.any? { |x| x =~ STASH } + end + + def site_name + "Sta.sh" + end + + def canonical_url + page_url + end + + def page_url + "https://sta.sh/#{stash_id}" + end + + def api_url + page_url + end + + def self.stash_id_from_url(url) + if url =~ STASH + $~[:post_id].downcase + else + nil + end + end + + def stash_id + [url, referer_url].map{ |x| self.class.stash_id_from_url(x) }.compact.first + end + end + end +end