Permalink
Browse files

prefer scraped rainbow pool images over pet images

  • Loading branch information...
1 parent 7b17f70 commit d8038f2fbfc763c919c1fc7a9990baed1820be05 @matchu matchu committed Sep 5, 2015
Showing with 62 additions and 16 deletions.
  1. +17 −10 app/models/pet_type.rb
  2. +5 −0 db/migrate/20150905181350_add_basic_image_hash_to_pet_types.rb
  3. +7 −6 db/schema.rb
  4. +33 −0 lib/tasks/pet_types.rake
@@ -1,6 +1,6 @@
class PetType < ActiveRecord::Base
IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png';
- IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/1/1\.png$};
+ IMAGE_CP_LOCATION_REGEX = %r{^/cp/(.+?)/[0-9]+/[0-9]+\.png$};
IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/
belongs_to :species
@@ -47,6 +47,11 @@ def self.random_basic_per_species(species_ids)
random_pet_types
end
+ def self.get_hash_from_cp_path(path)
+ match = path.match(IMAGE_CP_LOCATION_REGEX)
+ match ? match[1] : nil
+ end
+
def as_json(options={})
if options[:for] == 'wardrobe'
{
@@ -60,14 +65,16 @@ def as_json(options={})
end
def image_hash
- self['image_hash'] || basic_image_hash
+ # If there's a known basic image hash (no clothes), use that.
+ # Otherwise, if we have some image of a pet that we've picked up, use that.
+ # Otherwise, refer to the fallback YAML file (though, if we have our
+ # basic image hashes set correctly, the fallbacks should just be an old
+ # subset of the basic image hashes in the database.)
+ basic_image_hash || self['image_hash'] || fallback_image_hash
end
- def basic_image_hash
+ def fallback_image_hash
I18n.with_locale(I18n.default_locale) do
- # Probably should move the basic hashes into the database someday.
- # Until then, access the hash using the English color/species names.
-
if species && color && BasicHashes[species.name] && BasicHashes[species.name][color.name]
BasicHashes[species.name][color.name]
else
@@ -134,10 +141,10 @@ def add_pet_state_from_biology!(biology)
end
end
new_url = res['location']
- match = new_url.match(IMAGE_CP_LOCATION_REGEX)
- if match
- self.image_hash = match[1]
- Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{match[1]}"
+ new_image_hash = PetType.get_hash_from_cp_path(new_url)
+ if new_image_hash
+ self.image_hash = new_image_hash
+ Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{new_image_hash}"
else
raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format"
end
@@ -0,0 +1,5 @@
+class AddBasicImageHashToPetTypes < ActiveRecord::Migration
+ def change
+ add_column :pet_types, :basic_image_hash, :string
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20150802202909) do
+ActiveRecord::Schema.define(:version => 20150905181350) do
create_table "auth_servers", :force => true do |t|
t.string "short_name", :limit => 10, :null => false
@@ -261,11 +261,12 @@
add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id"
create_table "pet_types", :force => true do |t|
- t.integer "color_id", :limit => 1, :null => false
- t.integer "species_id", :limit => 1, :null => false
- t.datetime "created_at", :null => false
- t.integer "body_id", :limit => 2, :null => false
- t.string "image_hash", :limit => 8
+ t.integer "color_id", :limit => 1, :null => false
+ t.integer "species_id", :limit => 1, :null => false
+ t.datetime "created_at", :null => false
+ t.integer "body_id", :limit => 2, :null => false
+ t.string "image_hash", :limit => 8
+ t.string "basic_image_hash"
end
add_index "pet_types", ["species_id", "color_id"], :name => "pet_types_species_color", :unique => true
@@ -46,4 +46,37 @@ namespace :pet_types do
end
end
end
+
+ desc "Download the Rainbow Pool data for the given locale"
+ task :download_basic_image_hashes => :environment do
+ Species.find_each do |species|
+ pool_url = "http://www.neopets.com/pool/all_pb.phtml"
+ pool_options = {
+ :cookies => {:neologin => URI.encode(ENV['NEOLOGIN'])},
+ :params => {:lang => 'en', :f_species_id => species.id}
+ }
+ pool_response = RestClient.get(pool_url, pool_options)
+ pool_doc = Nokogiri::HTML(pool_response)
+
+ counts = {saved: 0, skipped: 0}
+ PetType.transaction do
+ pool_doc.css('a[onclick^="set_pet_img("]').each do |link|
+ color = Color.find_by_name link.text
+ pet_type = PetType.find_by_species_id_and_color_id species, color
+ if pet_type
+ image_hash = PetType.get_hash_from_cp_path(link['onclick'][36..55])
+ pet_type.basic_image_hash = image_hash
+ pet_type.save!
+ counts[:saved] += 1
+ puts "* #{pet_type.human_name}: #{pet_type.basic_image_hash}"
+ else
+ dummy_pet_type = PetType.new color: color, species: species
+ counts[:skipped] += 1
+ puts " #{dummy_pet_type.human_name}: skip: not yet modeled"
+ end
+ end
+ end
+ puts "- #{species.human_name}: saved #{counts[:saved]}, skipped #{counts[:skipped]}"
+ end
+ end
end

0 comments on commit d8038f2

Please sign in to comment.