diff --git a/Gemfile b/Gemfile index 69d1a9f4..15fea8d6 100644 --- a/Gemfile +++ b/Gemfile @@ -33,12 +33,14 @@ gem 'right_aws', '~> 2.1.0' gem "character-encodings", "~> 0.4.1", :platforms => :ruby_18 -gem "nokogiri", "~> 1.5.0" +gem "nokogiri", "~> 1.5.2" gem 'sanitize', '~> 2.0.3' gem 'newrelic_rpm' +gem 'neopets', :git => 'git://github.com/matchu/neopets.git' + group :development_async do # async wrappers gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git' diff --git a/Gemfile.lock b/Gemfile.lock index adeaa16d..899be4bb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,6 +29,13 @@ GIT em-synchrony (1.0.0) eventmachine (>= 1.0.0.beta.1) +GIT + remote: git://github.com/matchu/neopets.git + revision: d0127de12eae650fd85c56b45bc0b52e64e34ffe + specs: + neopets (0.0.1) + nokogiri (~> 1.5.2) + GIT remote: git://github.com/oldmoe/mysqlplus.git revision: 3dbaa7c00ff0bb75ad9538cdef176c72de35d231 @@ -118,7 +125,7 @@ GEM msgpack (0.4.6) mysql2 (0.2.6) newrelic_rpm (3.3.3) - nokogiri (1.5.0) + nokogiri (1.5.2) open4 (1.3.0) openneo-auth-signatory (0.1.0) ruby-hmac @@ -221,8 +228,9 @@ DEPENDENCIES msgpack (~> 0.4.3) mysql2 (< 0.3) mysqlplus! + neopets! newrelic_rpm - nokogiri (~> 1.5.0) + nokogiri (~> 1.5.2) openneo-auth-signatory (~> 0.1.0) rack-fiber_pool rails (= 3.0.5) diff --git a/app/models/neopets_user.rb b/app/models/neopets_user.rb index e7df0789..d0fc7fb2 100644 --- a/app/models/neopets_user.rb +++ b/app/models/neopets_user.rb @@ -12,17 +12,15 @@ class NeopetsUser end def load! - doc = Nokogiri::HTML(open(url)) - - unless pets_wrapper = doc.at('#userneopets') - raise NotFound, "Could not find user #{username}" - end - - pets = pets_wrapper.css('a[href^="/petlookup.phtml"]').map do |link| - name = link['href'].split('=').last - Pet.find_or_initialize_by_name(name) + user = Neopets::User.new(@username) + + begin + pets = user.pets + rescue Neopets::User::Error => e + raise NotFound, e.message end + pets = pets.map { |pet| Pet.find_or_initialize_by_name(pet.name) } items = pets.each(&:load!).map(&:items).flatten item_ids = items.map(&:id) @@ -45,14 +43,7 @@ class NeopetsUser def persisted? false end - - protected - - URL_PREFIX = 'http://www.neopets.com/userlookup.phtml?user=' - def url - URL_PREFIX + @username - end - - class NotFound < RuntimeError;end + + class NotFound < RuntimeError; end end diff --git a/app/models/pet.rb b/app/models/pet.rb index 799479bf..142f52c6 100644 --- a/app/models/pet.rb +++ b/app/models/pet.rb @@ -39,6 +39,7 @@ class Pet < ActiveRecord::Base biology = pet_data.biology_by_zone biology[0] = nil # remove effects if present @pet_state = self.pet_type.add_pet_state_from_biology! biology + @pet_state.label_by_pet(self, pet_data.owner) @items = Item.collection_from_pet_type_and_registries(self.pet_type, contents.object_info_registry, contents.object_asset_registry) true diff --git a/app/models/pet_state.rb b/app/models/pet_state.rb index 39e549ab..93eea67e 100644 --- a/app/models/pet_state.rb +++ b/app/models/pet_state.rb @@ -61,6 +61,26 @@ class PetState < ActiveRecord::Base rel.save! end end + + def label_by_pet(pet, username) + # If this pet is already labeled with a gender/mood, or it's unconverted + # and therefore has none, skip. + return false if self.labeled? || self.unconverted? + + # Find this pet on the owner's userlookup, where we can get both its gender + # and its mood. + user_pet = Neopets::User.new(username).pets. + find { |user_pet| user_pet.name.downcase == pet.name.downcase } + self.female = user_pet.female? + self.mood_id = user_pet.mood.id + self.labeled = true + + true + end + + def mood + Neopets::Pet::Mood.find(self.mood_id) + end def self.from_pet_type_and_biology_info(pet_type, info) swf_asset_ids = [] @@ -112,6 +132,7 @@ class PetState < ActiveRecord::Base end end pet_state.parent_swf_asset_relationships_to_update = relationships + pet_state.unconverted = (relationships.size == 1) pet_state end diff --git a/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb b/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb new file mode 100644 index 00000000..3dcabac1 --- /dev/null +++ b/db/migrate/20120521164652_add_female_and_mood_and_unconverted_and_labeled_to_pet_states.rb @@ -0,0 +1,15 @@ +class AddFemaleAndMoodAndUnconvertedAndLabeledToPetStates < ActiveRecord::Migration + def self.up + add_column :pet_states, :female, :boolean + add_column :pet_states, :mood_id, :integer + add_column :pet_states, :unconverted, :boolean + add_column :pet_states, :labeled, :boolean, :null => false, :default => false + end + + def self.down + remove_column :pet_states, :labeled + remove_column :pet_states, :unconverted + remove_column :pet_states, :mood_id + remove_column :pet_states, :female + end +end diff --git a/db/schema.rb b/db/schema.rb index 0500a942..5d866571 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120321231507) do +ActiveRecord::Schema.define(:version => 20120521164652) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -140,8 +140,12 @@ ActiveRecord::Schema.define(:version => 20120321231507) do end create_table "pet_states", :force => true do |t| - t.integer "pet_type_id", :limit => 3, :null => false - t.text "swf_asset_ids", :null => false + t.integer "pet_type_id", :limit => 3, :null => false + t.text "swf_asset_ids", :null => false + t.boolean "female" + t.integer "mood_id" + t.boolean "unconverted" + t.boolean "labeled", :default => false, :null => false end add_index "pet_states", ["pet_type_id"], :name => "pet_states_pet_type_id" diff --git a/vendor/cache/nokogiri-1.5.0.gem b/vendor/cache/nokogiri-1.5.0.gem deleted file mode 100644 index 47c37a67..00000000 Binary files a/vendor/cache/nokogiri-1.5.0.gem and /dev/null differ diff --git a/vendor/cache/nokogiri-1.5.2.gem b/vendor/cache/nokogiri-1.5.2.gem new file mode 100644 index 00000000..b4bec970 Binary files /dev/null and b/vendor/cache/nokogiri-1.5.2.gem differ