From 787b5c8e2713c9e14b284f942887a9e455c022a2 Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 12 Mar 2022 00:40:52 -0600 Subject: [PATCH] sources: merge Sta.sh strategy into DeviantArt strategy. This turns out to be a little simpler than keeping them separate. The only thing special we have to do for Sta.sh is use the Sta.sh page when we have a DeviantArt image with a Sta.sh referer. --- app/logical/source/url.rb | 1 - app/logical/source/url/deviant_art.rb | 20 +++++++-- app/logical/source/url/stash.rb | 36 ---------------- app/logical/sources/strategies.rb | 1 - app/logical/sources/strategies/deviant_art.rb | 18 +++++++- app/logical/sources/strategies/stash.rb | 43 ------------------- 6 files changed, 33 insertions(+), 86 deletions(-) delete mode 100644 app/logical/source/url/stash.rb delete mode 100644 app/logical/sources/strategies/stash.rb diff --git a/app/logical/source/url.rb b/app/logical/source/url.rb index 9316d9c56..1ea615cc6 100644 --- a/app/logical/source/url.rb +++ b/app/logical/source/url.rb @@ -39,7 +39,6 @@ module Source Source::URL::PixivSketch, Source::URL::Plurk, Source::URL::Skeb, - Source::URL::Stash, Source::URL::Tumblr, Source::URL::TwitPic, Source::URL::Weibo, diff --git a/app/logical/source/url/deviant_art.rb b/app/logical/source/url/deviant_art.rb index 2cd36e0e2..0b32d338f 100644 --- a/app/logical/source/url/deviant_art.rb +++ b/app/logical/source/url/deviant_art.rb @@ -18,10 +18,10 @@ module Source class URL::DeviantArt < Source::URL RESERVED_SUBDOMAINS = %w[www] - attr_reader :username, :work_id, :title + attr_reader :username, :work_id, :stash_id, :title def self.match?(url) - url.domain.in?(%w[deviantart.net deviantart.com fav.me]) || url.host.in?(%w[images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com wixmp-ed30a86b8c4ca887773594c2.wixmp.com api-da.wixmp.com]) + url.domain.in?(%w[deviantart.net deviantart.com fav.me sta.sh]) || url.host.in?(%w[images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com wixmp-ed30a86b8c4ca887773594c2.wixmp.com api-da.wixmp.com]) end def parse @@ -78,6 +78,18 @@ module Source in "fav.me", base36_id @work_id = base36_id.delete_prefix("d").to_i(36) + # https://sta.sh/21leo8mz87ue (folder) + # https://sta.sh/2uk0v5wabdt (subfolder) + # https://sta.sh/0wxs31o7nn2 (single image) + # Ref: https://www.deviantartsupport.com/en/article/what-is-stash-3391708 + # Ref: https://www.deviantart.com/developers/http/v1/20160316/stash_item/4662dd8b10e336486ea9a0b14da62b74 + in "sta.sh", stash_id + @stash_id = stash_id + + # https://sta.sh/zip/21leo8mz87ue + in "sta.sh", "zip", stash_id + @stash_id = stash_id + else end end @@ -119,7 +131,9 @@ module Source end def page_url - if username.present? && pretty_title.present? && work_id.present? + if stash_id.present? + "https://sta.sh/#{stash_id}" + elsif username.present? && pretty_title.present? && work_id.present? "https://www.deviantart.com/#{username}/art/#{pretty_title}-#{work_id}" elsif work_id.present? "https://www.deviantart.com/deviation/#{work_id}" diff --git a/app/logical/source/url/stash.rb b/app/logical/source/url/stash.rb deleted file mode 100644 index a10c5c62f..000000000 --- a/app/logical/source/url/stash.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -module Source - class URL::Stash < Source::URL - attr_reader :work_id, :username - - def self.match?(url) - url.domain == "sta.sh" - end - - def site_name - "Sta.sh" - end - - def parse - case [domain, *path_segments] - - # https://sta.sh/21leo8mz87ue (folder) - # https://sta.sh/2uk0v5wabdt (subfolder) - # https://sta.sh/0wxs31o7nn2 (single image) - in "sta.sh", work_id - @work_id = work_id - - # https://sta.sh/zip/21leo8mz87ue - in "sta.sh", "zip", work_id - @work_id = work_id - - else - end - end - - def page_url - "https://sta.sh/#{work_id}" - end - end -end diff --git a/app/logical/sources/strategies.rb b/app/logical/sources/strategies.rb index 3ced69356..66376d3e8 100644 --- a/app/logical/sources/strategies.rb +++ b/app/logical/sources/strategies.rb @@ -8,7 +8,6 @@ module Sources Strategies::Twitter, Strategies::Tumblr, Strategies::NicoSeiga, - Strategies::Stash, # must come before DeviantArt Strategies::DeviantArt, Strategies::Moebooru, Strategies::Nijie, diff --git a/app/logical/sources/strategies/deviant_art.rb b/app/logical/sources/strategies/deviant_art.rb index 364276b1d..0e9dd6ec5 100644 --- a/app/logical/sources/strategies/deviant_art.rb +++ b/app/logical/sources/strategies/deviant_art.rb @@ -38,7 +38,9 @@ module Sources end def page_url - if api_deviation.present? + if stash_page.present? + stash_page + elsif api_deviation.present? api_deviation[:url] elsif deviation_id.present? page_url_from_image_url @@ -48,7 +50,19 @@ module Sources end def page_url_from_image_url - parsed_url.page_url || parsed_referer&.page_url + stash_page || parsed_url.page_url || parsed_referer&.page_url + end + + # Sta.sh posts have the same image URLs as DeviantArt but different page URLs. We use the Sta.sh page if we have one. + # + # Image: https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/83d3eb4d-13e5-4aea-a08f-8d4331d033c4/dcmjs1s-389a7505-142d-4b34-a790-ab4ea1ec9eaa.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOjdlMGQxODg5ODIyNjQzNzNhNWYwZDQxNWVhMGQyNmUwIiwiaXNzIjoidXJuOmFwcDo3ZTBkMTg4OTgyMjY0MzczYTVmMGQ0MTVlYTBkMjZlMCIsIm9iaiI6W1t7InBhdGgiOiJcL2ZcLzgzZDNlYjRkLTEzZTUtNGFlYS1hMDhmLThkNDMzMWQwMzNjNFwvZGNtanMxcy0zODlhNzUwNS0xNDJkLTRiMzQtYTc5MC1hYjRlYTFlYzllYWEucG5nIn1dXSwiYXVkIjpbInVybjpzZXJ2aWNlOmZpbGUuZG93bmxvYWQiXX0.pIddc32BoLpAJt6D8YcRFonoVy9nC8RgROlYwMp3huo + # Page: https://sta.sh/01pwva4zzf98 + def stash_page + if parsed_url.stash_id.present? + parsed_url.page_url + elsif parsed_referer&.stash_id.present? + parsed_referer.page_url + end end def normalize_for_source diff --git a/app/logical/sources/strategies/stash.rb b/app/logical/sources/strategies/stash.rb deleted file mode 100644 index 711cfe57f..000000000 --- a/app/logical/sources/strategies/stash.rb +++ /dev/null @@ -1,43 +0,0 @@ -# frozen_string_literal: true - -# Page URLs: -# * https://sta.sh/0wxs31o7nn2 (single image) -# * https://sta.sh/21leo8mz87ue (folder) -# -# Image URLs: -# * https://orig00.deviantart.net/0fd2/f/2018/252/9/c/a_pepe_by_noizave-dcmga0s.png -# -# Ref: -# * https://github.com/danbooru/danbooru/issues/3877 -# * https://www.deviantartsupport.com/en/article/what-is-stash-3391708 -# * https://www.deviantart.com/developers/http/v1/20160316/stash_item/4662dd8b10e336486ea9a0b14da62b74 -# -module Sources - module Strategies - class Stash < DeviantArt - def match? - Source::URL::Stash === parsed_url || Source::URL::Stash === parsed_referer - end - - def site_name - "Sta.sh" - end - - def canonical_url - page_url - end - - def page_url - page_url_from_image_url - end - - def page_url_from_image_url - if Source::URL::Stash === parsed_url - parsed_url.page_url - elsif Source::URL::Stash === parsed_referer - parsed_referer.page_url - end - end - end - end -end