diff --git a/app/services/neopets_media_archive.rb b/app/services/neopets_media_archive.rb index 7d45eced..e4350947 100644 --- a/app/services/neopets_media_archive.rb +++ b/app/services/neopets_media_archive.rb @@ -1,4 +1,3 @@ -require "addressable/template" require "addressable/uri" require "httparty" require "json" @@ -15,12 +14,7 @@ module NeopetsMediaArchive include HTTParty base_uri "https://images.neopets.com/" - OLD_MANIFEST_PATH_TEMPLATE = Addressable::Template.new( - "https://images.neopets.com/cp/{short_type}/data/{id1}/{id2}/{id3}/{id}_{hash}/manifest.json" - ) - NEW_MANIFEST_PATH_TEMPLATE = Addressable::Template.new( - "https://images.neopets.com/cp/{short_type}/data/{id1}/{id2}/{id3}/{id}/manifest.json" - ) + ROOT_PATH = Pathname.new(Rails.configuration.neopets_media_archive_root) # Load the file from the given `images.neopets.com` URI, as JSON. def self.load_json(uri) @@ -102,7 +96,7 @@ module NeopetsMediaArchive end def self.local_file_path(uri) - Rails.configuration.neopets_media_archive_root + path_within_archive(uri) + ROOT_PATH + path_within_archive(uri) end class NotFound < StandardError; end diff --git a/lib/tasks/swf_assets.rake b/lib/tasks/swf_assets.rake index c01149d8..a5a39029 100644 --- a/lib/tasks/swf_assets.rake +++ b/lib/tasks/swf_assets.rake @@ -112,29 +112,29 @@ end SWF_URL_PATTERN = %r{^(?:https?:)?//images\.neopets\.com/cp/(bio|items)/swf/(.+?)_([a-z0-9]+)\.swf$} def infer_manifest_url(swf_url, internet) - url_match = swf_url.match(SWF_URL_PATTERN) - raise ArgumentError, "not a valid SWF URL: #{swf_url}" if url_match.nil? - - # Build the potential manifest URLs, from the two structures we know of. - type, folders, hash_str = url_match.captures - potential_manifest_urls = [ - "https://images.neopets.com/cp/#{type}/data/#{folders}/manifest.json", - "https://images.neopets.com/cp/#{type}/data/#{folders}_#{hash_str}/manifest.json", - ] + url_match = swf_url.match(SWF_URL_PATTERN) + raise ArgumentError, "not a valid SWF URL: #{swf_url}" if url_match.nil? + + # Build the potential manifest URLs, from the two structures we know of. + type, folders, hash_str = url_match.captures + potential_manifest_urls = [ + "https://images.neopets.com/cp/#{type}/data/#{folders}/manifest.json", + "https://images.neopets.com/cp/#{type}/data/#{folders}_#{hash_str}/manifest.json", + ] - # Send a HEAD request to test each manifest URL, without downloading its - # content. If it succeeds, we're done! - potential_manifest_urls.each do |potential_manifest_url| - res = internet.head potential_manifest_url - if res.ok? - return potential_manifest_url - elsif res.status == 404 - next # Ok, this was not the manifest! - else - raise "unexpected manifest response code: #{res.status}" - end - end + # Send a HEAD request to test each manifest URL, without downloading its + # content. If it succeeds, we're done! + potential_manifest_urls.each do |potential_manifest_url| + res = internet.head potential_manifest_url + if res.ok? + return potential_manifest_url + elsif res.status == 404 + next # Ok, this was not the manifest! + else + raise "unexpected manifest response code: #{res.status}" + end + end - # Otherwise, there's no valid manifest URL. - raise "all of the common manifest URL patterns returned HTTP 404" + # Otherwise, there's no valid manifest URL. + raise "all of the common manifest URL patterns returned HTTP 404" end