From b6e670043983f007c5bf8c5c9910490862bf2a4d Mon Sep 17 00:00:00 2001 From: Charley DAVID Date: Fri, 26 May 2017 11:12:03 -0300 Subject: [PATCH] Improve price retrieval --- lib/eshop.rb | 13 +++++++++---- lib/tasks/eshop.rake | 36 +++++++++++++++++++++++++----------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/eshop.rb b/lib/eshop.rb index d8942b0..a7b580d 100644 --- a/lib/eshop.rb +++ b/lib/eshop.rb @@ -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 diff --git a/lib/tasks/eshop.rake b/lib/tasks/eshop.rake index b757436..e04e8dd 100644 --- a/lib/tasks/eshop.rake +++ b/lib/tasks/eshop.rake @@ -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