From d8038f2fbfc763c919c1fc7a9990baed1820be05 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 5 Sep 2015 18:48:41 +0000 Subject: [PATCH] prefer scraped rainbow pool images over pet images --- app/models/pet_type.rb | 27 +++++++++------ ...81350_add_basic_image_hash_to_pet_types.rb | 5 +++ db/schema.rb | 13 ++++---- lib/tasks/pet_types.rake | 33 +++++++++++++++++++ 4 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20150905181350_add_basic_image_hash_to_pet_types.rb diff --git a/app/models/pet_type.rb b/app/models/pet_type.rb index bceedffb..29241c2e 100644 --- a/app/models/pet_type.rb +++ b/app/models/pet_type.rb @@ -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 @@ class PetType < ActiveRecord::Base 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 @@ class PetType < ActiveRecord::Base 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 @@ class PetType < ActiveRecord::Base 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 diff --git a/db/migrate/20150905181350_add_basic_image_hash_to_pet_types.rb b/db/migrate/20150905181350_add_basic_image_hash_to_pet_types.rb new file mode 100644 index 00000000..8777603a --- /dev/null +++ b/db/migrate/20150905181350_add_basic_image_hash_to_pet_types.rb @@ -0,0 +1,5 @@ +class AddBasicImageHashToPetTypes < ActiveRecord::Migration + def change + add_column :pet_types, :basic_image_hash, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 56a669ba..0ece6bf6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ ActiveRecord::Schema.define(:version => 20150802202909) do 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 diff --git a/lib/tasks/pet_types.rake b/lib/tasks/pet_types.rake index 464c2db5..e3c0a116 100644 --- a/lib/tasks/pet_types.rake +++ b/lib/tasks/pet_types.rake @@ -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 \ No newline at end of file