def update_switch_us_free_tag(): print('Updating Switch US games "Free To Play" tag...') url = 'http://www.nintendo.com/json/content/get/filter/game' params = { 'system': 'switch', 'sort': 'title', 'direction': 'asc', 'limit': 200, 'offset': 0 } tag_group_characteristics, tag_group_created = \ TagGroup.objects.get_or_create(name='Characteristics') for offset in range(0, 3000, 200): # Make the request, and skip current offset if there's any problem params['offset'] = offset req = treated_request(url, params, 'US Switch games') if req is None: print('Request for us games failed at offset {}, skipping.'.format( offset)) continue # If offset hasn't gone beyond the last game yet if 'game' in req.json()['games']: print('{} games found at offset {}'.format( len(req.json()['games']['game']), offset)) # Iterate over every game already in the database for game in req.json()['games']['game']: game_code = re.sub(r'[\-\. ]+', '', game['game_code']) # If unique code is empty (unreleased games), skip game if game_code[4:9] is '': print('Empty unique code found for {}'.format( game['title'])) continue # If game not yet in DB, skip it if not SwitchGameUS.objects \ .filter(game_code_unique=game_code[4:9]) \ .exists(): continue switch_game_us = SwitchGameUS.objects \ .get(game_code_unique=game_code[4:9]) if game['free_to_start'] == 'true': create_tag_if_not_exists('Free to Play', tag_group_characteristics, switch_game_us.switchgame) # If offset went beyond the last game, break the for loop else: break
def update_switch_eu_age_tag(): print('Updating Switch EU games age rating...') url = 'http://search.nintendo-europe.com/en/select' params = { 'fq': 'type:GAME AND system_type:nintendoswitch* AND product_code_txt:*', 'q': '*', 'rows': 9999, 'sort': 'sorting_title asc', 'start': 0, 'wt': 'json', } # Make the request, and stop the task if there's any problem req = treated_request(url, params, 'EU Switch games') if req is None: return # Create/ Get the 'Age Rating' Tag Group tag_group_age, tag_group_created = \ TagGroup.objects.get_or_create(name='Age Rating') # Adds age rating tags for every game already on the database print('{} games found'.format(len(req.json()['response']['docs']))) for game in req.json()['response']['docs']: if not SwitchGameEU.objects.filter( game_code_unique=game['product_code_txt'][0].strip() [4:9]).exists(): continue serializer = SwitchGameEUSerializer(data=game) if serializer.is_valid(): switch_game_eu = SwitchGameEU.objects.get( game_code_unique=game['product_code_txt'][0].strip()[4:9]) # If game has an age rating defined, add it as a tag if 'age_rating_sorting_i' in game and game[ 'age_rating_sorting_i'] != 0: create_tag_if_not_exists( 'PEGI ' + str(game['age_rating_sorting_i']), tag_group_age, switch_game_eu.switchgame)
def update_switch_eu(): print('Updating Switch EU games...') url = 'http://search.nintendo-europe.com/en/select' params = { 'fq': 'type:GAME AND system_type:nintendoswitch* AND product_code_txt:*', 'q': '*', 'rows': 9999, 'sort': 'sorting_title asc', 'start': 0, 'wt': 'json', } # Make the request, and stop the task if there's any problem req = treated_request(url, params, 'EU Switch games') if req is None: return tag_group_publisher, tag_group_pub_created = \ TagGroup.objects.get_or_create(name='Publisher') tag_group_developer, tag_group_dev_created = \ TagGroup.objects.get_or_create(name='Developer') tag_group_age, tag_group_age_created = \ TagGroup.objects.get_or_create(name='Age Rating') tag_group_characteristics, tag_group_created = \ TagGroup.objects.get_or_create(name='Characteristics') # Add every game to the database print('{} games found'.format(len(req.json()['response']['docs']))) for game in req.json()['response']['docs']: if SwitchGameEU.objects.filter( game_code_unique=game['product_code_txt'][0].strip() [4:9]).exists(): continue serializer = SwitchGameEUSerializer(data=game) if serializer.is_valid(): # print('Added: {}'.format(game['title'])) switch_game_eu = serializer.save() # If game has a publisher defined, add it as a tag if 'developer' in game: create_tag_if_not_exists(game['developer'], tag_group_developer, switch_game_eu.switchgame) # If game has a publisher defined, add it as a tag if 'publisher' in game: create_tag_if_not_exists(game['publisher'], tag_group_publisher, switch_game_eu.switchgame) # If game has an age rating defined, add it as a tag if 'age_rating_sorting_i' in game and game[ 'age_rating_sorting_i'] != 0: create_tag_if_not_exists( 'PEGI ' + str(game['age_rating_sorting_i']), tag_group_age, switch_game_eu.switchgame) # If game has physical version set to true if 'physical_version_b' in game and game[ 'physical_version_b'] == True: create_tag_if_not_exists('Physical Release', tag_group_characteristics, switch_game_eu.switchgame) else: print('[ERROR] ({}): {}'.format(game['title'], serializer.errors))
def update_switch_us(): print('Updating Switch US games...') url = 'http://www.nintendo.com/json/content/get/filter/game' params = { 'system': 'switch', 'sort': 'title', 'direction': 'asc', 'limit': 200, 'offset': 0 } tag_group_genre, tag_group_created = \ TagGroup.objects.get_or_create(name='Genre') tag_group_characteristics, tag_group_created = \ TagGroup.objects.get_or_create(name='Characteristics') for offset in range(0, 3000, 200): # Make the request, and skip current offset if there's any problem params['offset'] = offset req = treated_request(url, params, 'US Switch games') if req is None: print('Request for us games failed at offset {}, skipping.'.format( offset)) continue # If offset hasn't gone beyond the last game yet if 'game' in req.json()['games']: print('{} games found at offset {}'.format( len(req.json()['games']['game']), offset)) # Add every game to the database for game in req.json()['games']['game']: game_code = re.sub(r'[\-\. ]+', '', game['game_code']) # If unique code is empty (usually unreleased games), skip game if game_code[4:9] is '': print('Empty unique code found for {}'.format( game['title'])) continue # If game already in DB, skip it if SwitchGameUS.objects \ .filter(game_code_unique=game_code[4:9]) \ .exists(): # print('Game {} already in DB.'.format(game['title'])) continue # If game not yet in DB, save it else: serializer = SwitchGameUSSerializer(data=game) if serializer.is_valid(): # print('Added: {}'.format(game['title'])) switch_game_us = serializer.save() else: print('[ERROR] ({}): {}'.format( game['title'], serializer.errors)) continue # For each tag, create if it doesn't exist yet and assign it to # the game if game['free_to_start'] == 'true': create_tag_if_not_exists('Free to Play', tag_group_characteristics, switch_game_us.switchgame) if isinstance(game['categories']['category'], str): # Checking if string is necessary for games with a single # category (multiple categories come in an array) create_tag_if_not_exists(game['categories']['category'], tag_group_genre, switch_game_us.switchgame) else: for tag_name in game['categories']['category']: create_tag_if_not_exists(tag_name, tag_group_genre, switch_game_us.switchgame) # If offset went beyond the last game, break the for loop else: break
def update_country(country, model): url = 'https://api.ec.nintendo.com/v1/price' count = model.objects.count() found_price = 0 found_sales = 0 for offset in range(0, count, 50): print('Updating {}\'s price offset {}'.format(country, offset)) games = model.objects.all()[offset:offset + 50].values('nsuid') games = list(map(lambda game: game['nsuid'], games)) games = ','.join([nsuid for nsuid in games if nsuid != None]) params = {'lang': 'en', 'country': country, 'ids': games} req = treated_request(url, params, 'US Switch price') data = req.json()['prices'] for price_info in data: if model.objects.filter(nsuid=price_info['title_id']).count() > 1: print('Multiple games found for nsuid {}'.format( price_info['title_id'])) game = model.objects.filter(nsuid=price_info['title_id'])[0] if 'regular_price' in price_info: found_price = found_price + 1 if SwitchGamePrice.objects.filter(game=game.switchgame, country=country).exists(): price = SwitchGamePrice.objects.get(game=game.switchgame, country=country) serialized = SwitchGamePriceSerializer( data=price_info['regular_price'], context={ 'game': game.switchgame, 'country': country }, instance=price) else: serialized = SwitchGamePriceSerializer( data=price_info['regular_price'], context={ 'game': game.switchgame, 'country': country }) if serialized.is_valid(): serialized.save() if 'discount_price' in price_info: found_sales = found_sales + 1 if SwitchGameSale.objects.filter(game=game.switchgame, country=country).exists(): price = SwitchGameSale.objects.get(game=game.switchgame, country=country) serialized = SwitchGameSaleSerializer( data=price_info['discount_price'], instance=price, context={ 'game': game.switchgame, 'country': country }) else: serialized = SwitchGameSaleSerializer( data=price_info['discount_price'], context={ 'game': game.switchgame, 'country': country }) if serialized.is_valid(): serialized.save() else: SwitchGameSale.objects \ .filter(game=game.switchgame, country=country).delete() print('Found {} prices and {} sales for country {}'.format( found_price, found_sales, country))