Skip to content

Commit

Permalink
Improve price retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Calyhre committed May 26, 2017
1 parent 1bef8fc commit b6e6700
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
13 changes: 9 additions & 4 deletions lib/eshop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
module Eshop
# Nintendo split countries this way. So yes, africa and oceania are in europe...
REGIONS = {
asia: %w[AE AZ CY HK IN JP KR MY SA SG TR TW],
asia: %w[AE AZ HK IN JP KR MY SA SG TR TW],
europe: %w[
AD AL AT AU BA BE BG BW CH CZ DE DJ DK EE ER ES FI FR GB GG GI GR HR HU IE IM IS IT JE LI LS
LT LU LV MC ME MK ML MR MT MZ NA NE NL NO NZ PL PT RO RS RU SD SE SI SK SM SO SZ TD VA ZA ZM
ZW
AD AL AT AU BA BE BG BW CH CY CZ DE DJ DK EE ER ES FI FR GB GG GI GR HR HU IE IM IS IT JE LI
LS LT LU LV MC ME MK ML MR MT MZ NA NE NL NO NZ PL PT RO RS RU SD SE SI SK SM SO SZ TD VA ZA
ZM ZW
],
americas: %w[
AG AI AR AW BB BM BO BR BS BZ CA CL CO CR DM DO EC GD GF GP GT GY HN HT JM KN KY LC MQ MS MX
NI PA PE PY SR SV TC TT US UY VC VE VG VI
],
}.freeze

COUNTRIES = %w[
AT AU BE BG CA CH CY CZ DE DK EE ES FI FR GB GR HR HU IE IT JP LT LU LV MT MX NL NO NZ PL PT RO
RU SE SI SK US ZA
].freeze
end
36 changes: 25 additions & 11 deletions lib/tasks/eshop.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ namespace :eshop do

desc 'Get all prices from eShop API'
task retrieve_prices: :environment do
Eshop::REGIONS.map do |region, countries|
ids = Game.where(region: region).pluck(:nsuid)
puts "Retieving #{ids.count} prices for #{region} with #{countries.count} countries..."
countries.each do |country|
print " #{ISO3166::Country[country]}"
Eshop::Prices.list(country: country, ids: ids).map do |price|
price[:game] = Game.find_by(region: region, nsuid: price[:nsuid])
Price.find_or_initialize_by(nsuid: price[:nsuid], country: country)
.update_attributes!(price)
end
print " OK\n"
ids = Game.distinct.pluck(:nsuid).compact
puts "Retrieving #{ids.count} prices..."
Eshop::COUNTRIES.map do |country|
printf ' %-20s', ISO3166::Country[country].translation('en')
prices = Eshop::Prices.list(country: country, ids: ids).map do |price|
price[:game] = Game.find_by(nsuid: price[:nsuid])
Price.find_or_initialize_by(nsuid: price[:nsuid], country: country)
.update_attributes!(price)
end
printf ": %-3s\n", prices.count
end
end

desc 'Get all prices from eShop API'
task retrieve_all: %i[retrieve_games retrieve_prices]

desc 'Detect new shops that were not selling games before'
task detect_new_shops: :environment do
ids = Game.distinct.pluck(:nsuid).compact.sample(50)
new_shops = []
puts "Retrieving #{ids.count} prices..."
countries = Eshop::REGIONS.values.flatten.uniq.sort - Eshop::COUNTRIES
countries.map do |country|
printf ' %-20s', ISO3166::Country[country].translation('en')
prices = Eshop::Prices.list(country: country, ids: ids)
printf ": %-3s\n", prices.count
new_shops << country unless prices.empty?
end

raise "New shops detected: #{new_shops.join(', ')}" unless new_shops.empty?
end
end

0 comments on commit b6e6700

Please sign in to comment.