forked from OpenNeo/impress
prefer scraped rainbow pool images over pet images
This commit is contained in:
parent
7b17f70635
commit
d8038f2fbf
4 changed files with 62 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
||||||
class PetType < ActiveRecord::Base
|
class PetType < ActiveRecord::Base
|
||||||
IMAGE_CPN_FORMAT = 'http://pets.neopets.com/cpn/%s/1/1.png';
|
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_]+$/
|
IMAGE_CPN_ACCEPTABLE_NAME = /^[a-z0-9_]+$/
|
||||||
|
|
||||||
belongs_to :species
|
belongs_to :species
|
||||||
|
@ -47,6 +47,11 @@ class PetType < ActiveRecord::Base
|
||||||
random_pet_types
|
random_pet_types
|
||||||
end
|
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={})
|
def as_json(options={})
|
||||||
if options[:for] == 'wardrobe'
|
if options[:for] == 'wardrobe'
|
||||||
{
|
{
|
||||||
|
@ -60,14 +65,16 @@ class PetType < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_hash
|
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
|
end
|
||||||
|
|
||||||
def basic_image_hash
|
def fallback_image_hash
|
||||||
I18n.with_locale(I18n.default_locale) do
|
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]
|
if species && color && BasicHashes[species.name] && BasicHashes[species.name][color.name]
|
||||||
BasicHashes[species.name][color.name]
|
BasicHashes[species.name][color.name]
|
||||||
else
|
else
|
||||||
|
@ -134,10 +141,10 @@ class PetType < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
new_url = res['location']
|
new_url = res['location']
|
||||||
match = new_url.match(IMAGE_CP_LOCATION_REGEX)
|
new_image_hash = PetType.get_hash_from_cp_path(new_url)
|
||||||
if match
|
if new_image_hash
|
||||||
self.image_hash = match[1]
|
self.image_hash = new_image_hash
|
||||||
Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{match[1]}"
|
Rails.logger.info "Successfully loaded #{cpn_uri}, saved image hash #{new_image_hash}"
|
||||||
else
|
else
|
||||||
raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format"
|
raise DownloadError, "CPN image pointed to #{new_url}, which does not match CP image format"
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddBasicImageHashToPetTypes < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :pet_types, :basic_image_hash, :string
|
||||||
|
end
|
||||||
|
end
|
13
db/schema.rb
13
db/schema.rb
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# 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|
|
create_table "auth_servers", :force => true do |t|
|
||||||
t.string "short_name", :limit => 10, :null => false
|
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"
|
add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id"
|
||||||
|
|
||||||
create_table "pet_types", :force => true do |t|
|
create_table "pet_types", :force => true do |t|
|
||||||
t.integer "color_id", :limit => 1, :null => false
|
t.integer "color_id", :limit => 1, :null => false
|
||||||
t.integer "species_id", :limit => 1, :null => false
|
t.integer "species_id", :limit => 1, :null => false
|
||||||
t.datetime "created_at", :null => false
|
t.datetime "created_at", :null => false
|
||||||
t.integer "body_id", :limit => 2, :null => false
|
t.integer "body_id", :limit => 2, :null => false
|
||||||
t.string "image_hash", :limit => 8
|
t.string "image_hash", :limit => 8
|
||||||
|
t.string "basic_image_hash"
|
||||||
end
|
end
|
||||||
|
|
||||||
add_index "pet_types", ["species_id", "color_id"], :name => "pet_types_species_color", :unique => true
|
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
|
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
|
end
|
Loading…
Reference in a new issue