Fix "couldn't download the associated Flash files" when modeling

So this was a slightly wrong error message, what was happening was:

1. Trying to load the image hash for this pet, by looking them up at
   https://pets.neopets.com/cpn/PET_NAME/1/1.png and seeing what URL it
   redirects to.
2. But pets.neopets.com was rejecting our User-Agent string, which
   would've been just "Ruby", since we hadn't set it otherwise. I guess
   that's an explicitly banned string?

I also found that the kind of more-helpful User-Agent string I like to
write was being rejected, and I could only get it to accept something
very simple? So that's what we're using now, I guess!!
This commit is contained in:
Emi Matchu 2024-01-15 01:15:35 -08:00
parent 03f38c6461
commit e337767954
5 changed files with 15 additions and 5 deletions

View file

@ -126,9 +126,11 @@ class PetType < ApplicationRecord
before_save do before_save do
if @origin_pet && @origin_pet.name =~ IMAGE_CPN_ACCEPTABLE_NAME if @origin_pet && @origin_pet.name =~ IMAGE_CPN_ACCEPTABLE_NAME
cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, CGI.escape(@origin_pet.name)); cpn_uri = URI.parse sprintf(IMAGE_CPN_FORMAT, CGI.escape(@origin_pet.name))
begin begin
res = Net::HTTP.get_response(cpn_uri) res = Net::HTTP.get_response(cpn_uri, {
'User-Agent' => Rails.configuration.user_agent_for_neopets
})
rescue Exception => e rescue Exception => e
raise DownloadError, e.message raise DownloadError, e.message
end end

View file

@ -60,6 +60,12 @@ module OpenneoImpressItems
config.assets.initialize_on_precompile = false config.assets.initialize_on_precompile = false
config.middleware.insert_after ActionDispatch::Flash, Rack::Attack config.middleware.insert_after ActionDispatch::Flash, Rack::Attack
# It seems like some Neopets servers reject any user agent containing
# symbols? So I can't provide anything helpful like a URL, email address,
# version number, etc. So let's only send this to Neopets systems, where it
# should hopefully be clear who we are from context!
config.user_agent_for_neopets = "Dress to Impress"
end end
end end

View file

@ -676,7 +676,8 @@ en-MEEP:
not_found: We couldn't meep a pet by that name. Is it meeped correctly? not_found: We couldn't meep a pet by that name. Is it meeped correctly?
asset_download_error: asset_download_error:
We meeped the pet and what it's wearing, but couldn't meep the We meeped the pet and what it's wearing, but couldn't meep the
associated Flash files. Maybe Neopets is down. Please meep again later! associated meepia files. Maybe Neopets is down, or changed their
firewall rules? Please meep again later!
pet_download_error: pet_download_error:
We couldn't meep to Neopets to meep up the pet. Maybe they're down. We couldn't meep to Neopets to meep up the pet. Maybe they're down.
Please try meep later! Please try meep later!

View file

@ -751,7 +751,8 @@ en:
not_found: We couldn't find a pet by that name. Is it spelled correctly? not_found: We couldn't find a pet by that name. Is it spelled correctly?
asset_download_error: asset_download_error:
We found the pet and what it's wearing, but couldn't download the We found the pet and what it's wearing, but couldn't download the
associated Flash files. Maybe Neopets is down. Please try again later! associated media files. Maybe Neopets is down, or changed their
firewall rules? Please try again later!
pet_download_error: pet_download_error:
We couldn't connect to Neopets to look up the pet. Maybe they're down. We couldn't connect to Neopets to look up the pet. Maybe they're down.
Please try again later! Please try again later!

View file

@ -533,7 +533,7 @@ es:
submission_success: "%{points} puntos" submission_success: "%{points} puntos"
load: load:
not_found: No hemos podido encontrar a un pet con ese nombre. ¿Lo has escrito correctamente? not_found: No hemos podido encontrar a un pet con ese nombre. ¿Lo has escrito correctamente?
asset_download_error: Hemos encontrado el pet que intentas vestir, pero no hemos podido descargar las imágenes flash que lo asocian. Posiblemente Neopets está caído. ¡Por favor inténtalo de nuevo más tarde! asset_download_error: Hemos encontrado el pet que intentas vestir, pero no hemos podido descargar las imágenes que lo asocian. Posiblemente Neopets está caído. ¡Por favor inténtalo de nuevo más tarde!
pet_download_error: No hemos podido conectar con Neopets para ver tu pet. Posiblemente el servidor de Neopets se ha caído. ¡Por favor inténtalo de nuevo más tarde! pet_download_error: No hemos podido conectar con Neopets para ver tu pet. Posiblemente el servidor de Neopets se ha caído. ¡Por favor inténtalo de nuevo más tarde!
users: users:
index: index: