From 2d88569fac2fd1be5f9074cebede16c73cd067fd Mon Sep 17 00:00:00 2001 From: BrokenEagle Date: Sun, 24 May 2020 00:15:52 +0000 Subject: [PATCH] Fixes issues with Artstation source strategy The reason that the download was failing was not because the 4k size didn't exist, but because the Artstation had no way to handle image cover URLs. This caused it to pass nil to the download function. Additionally, there was no way to get the preview URL size, i.e. the smallest available image for an Arstation image URL. - Adds support for cover URLs - Adds support for preview URL size --- app/logical/sources/strategies/art_station.rb | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/app/logical/sources/strategies/art_station.rb b/app/logical/sources/strategies/art_station.rb index 1c7d1ba09..0471ecee3 100644 --- a/app/logical/sources/strategies/art_station.rb +++ b/app/logical/sources/strategies/art_station.rb @@ -16,6 +16,8 @@ # * https://cdna.artstation.com/p/assets/images/images/005/804/224/large/titapa-khemakavat-sa-dui-srevere.jpg?1493887236 # * https://cdnb.artstation.com/p/assets/images/images/014/410/217/smaller_square/bart-osz-bartosz1812041.jpg?1543866276 # * https://cdna.artstation.com/p/assets/images/images/007/253/680/4k/ina-wong-demon-girl-done-ttd-comp.jpg?1504793833 +# +# * https://cdna.artstation.com/p/assets/covers/images/007/262/828/small/monica-kyrie-1.jpg?1504865060 module Sources::Strategies class ArtStation < Base @@ -27,7 +29,7 @@ module Sources::Strategies ARTIST3 = %r{\Ahttps?://www\.artstation\.com/(?[\w-]+)/?\z}i ARTIST = Regexp.union(ARTIST1, ARTIST2, ARTIST3) - ASSET = %r!\Ahttps?://cdn\w*\.artstation\.com/p/assets/images/images/(?\d+/\d+/\d+)/(?[^/]+)/(?.+)\z!i + ASSET = %r!\Ahttps?://cdn\w*\.artstation\.com/p/assets/(?images|covers)/images/(?\d+/\d+/\d+)/(?[^/]+)/(?.+)\z!i attr_reader :json @@ -40,7 +42,11 @@ module Sources::Strategies end def image_urls - @image_urls ||= image_urls_sub.map { |asset| largest_asset_url(asset) } + @image_urls ||= image_urls_sub.map { |asset| asset_url(asset, :largest) } + end + + def preview_urls + @preview_urls ||= image_urls_sub.map { |asset| asset_url(asset, :smallest) } end def page_url @@ -116,17 +122,26 @@ module Sources::Strategies end memoize :api_response - def largest_asset_url(url) + def image_url_sizes(type, id, filename) + [ + "https://cdn.artstation.com/p/assets/#{type}/images/#{id}/original/#{filename}", + "https://cdn.artstation.com/p/assets/#{type}/images/#{id}/4k/#{filename}", + "https://cdn.artstation.com/p/assets/#{type}/images/#{id}/large/#{filename}", + "https://cdn.artstation.com/p/assets/#{type}/images/#{id}/medium/#{filename}", + "https://cdn.artstation.com/p/assets/#{type}/images/#{id}/small/#{filename}", + ] + end + + def asset_url(url, size) return url unless url =~ ASSET - urls = [ - "https://cdn.artstation.com/p/assets/images/images/#{$~[:id]}/original/#{$~[:filename]}", - "https://cdn.artstation.com/p/assets/images/images/#{$~[:id]}/4k/#{$~[:filename]}", - "https://cdn.artstation.com/p/assets/images/images/#{$~[:id]}/large/#{$~[:filename]}", - ] + urls = image_url_sizes($~[:type], $~[:id], $~[:filename]) + if size == :smallest + urls = urls.reverse() + end - largest_url = urls.find { |url| http_exists?(url, headers) } - largest_url || url + chosen_url = urls.find { |url| http_exists?(url, headers) } + chosen_url || url end end end