def remove(message, game_id): username = message.author.name games_db = GamesDatabase() wishlist_db = WishlistDatabase() game = games_db.load(game_id) if not game: return build_response(INVALID_GAME_ID.format(game_id), username) wishlist = wishlist_db.load(username) if not wishlist: return build_response(NO_WISHLIST, username) if game_id in wishlist.games: del wishlist.games[game_id] wishlist_db.save(wishlist) LOG.info(f'{username} removed {game.title} to the wishlist') return build_response(GAME_REMOVED.format(game.title), username) else: return build_response(INVALID_WISHLISTED_GAME.format(game.title), username)
def get_games_on_sale(system=None): games_db = GamesDatabase() prices_db = PricesDatabase() filter = {} if not system else {SYSTEM: system} deals = {price.id: price for price in prices_db.load_all(filter=filter) if any([price.prices[country].active for country in COUNTRIES if price.prices.get(country)])} games = {game.id: game for game in games_db.load_all(filter=filter) if any([nsuid for nsuid in game.nsuids.values() if nsuid in deals])} games = dict(sorted(games.items(), key=lambda kv: kv[1].title.lower())) return games, deals
def build_wishlist(username): games_db = GamesDatabase() wishlist_db = WishlistDatabase() wishlist = wishlist_db.load(username) if not wishlist: return NO_WISHLIST if not len(wishlist.games): return WISHLIST_EMPTY games = { game.id: game for game in games_db.load_all(filter={ID: {'$in': list(wishlist.games.keys())}}) } rows = [] for wishlisted_game in wishlist.games.values(): game = games.get(wishlisted_game.id) if not game: continue country_list = [ COUNTRIES[country][FLAG] for country in wishlisted_game.countries if country in COUNTRIES ] rows.append(f'{game.title}|{" ".join(country_list)}|[{MINUS}]({REMOVE_URL.format(game.id)})') rows.sort() content = [ '', 'Title | Countries | Actions', '--- | --- | :---: ' ] content.extend(rows) return '\n'.join(content)
def add(message, game_id): now = datetime.utcnow() username = message.author.name games_db = GamesDatabase() wishlist_db = WishlistDatabase() game = games_db.load(game_id) if not game: return build_response(INVALID_GAME_ID.format(game_id), username) countries = sorted(message.body.split(' ')) invalid_countries = [ country for country in countries if country not in COUNTRIES ] if len(invalid_countries) == 1: return build_response(INVALID_COUNTRY.format(invalid_countries), username) elif len(invalid_countries) > 1: return build_response(INVALID_COUNTRIES.format(invalid_countries), username) wishlist = wishlist_db.load(username) if not wishlist: wishlist = Wishlist(_id=username) if len(wishlist.games) > LIMIT: return build_response(WISHLIST_FULL.format(len(wishlist.games), LIMIT), username) wishlist.games[game_id] = WishlistedGame( _id=game_id, countries={country: now for country in countries}) wishlist_db.save(wishlist) LOG.info(f'{username} added {game.title} to the wishlist') return build_response(GAME_ADDED.format(game.title), username)
def get_games_and_prices(system): games = GamesDatabase().load_all( filter={ 'system': system, 'free_to_play': False } ) prices = PricesDatabase().load_all( filter={ 'system': system } ) return {game.id: game for game in games}, prices
def top_wishlist(system, limit=50): if MAINTENANCE: return render_template('maintenance.html') system, _ = validate(system) games = GamesDatabase().load_all(filter={ 'system': system[ID], 'free_to_play': False }, sort=[('wishlisted', -1)]) return render_template('top/wishlist.html', games=games, limit=limit, add_url=ADD_URL, show_url=SHOW_URL, emoji_plus=PLUS, emoji_star=STAR)
def wishlist(system, country): if MAINTENANCE: return render_template('maintenance.html') try: system, country = validate(system, country) except: return None region = country[REGION] games = list(GamesDatabase().load_all(filter={ SYSTEM: system[ID], f'nsuids.{region}': { '$ne': None }, 'free_to_play': False }, sort=[(f'release_dates.{region}', -1) ])) for game in games: game.countries = [] for c, d in COUNTRIES.items(): if d[REGION] in game.nsuids: game.countries.append(c) game.countries = ' '.join(game.countries) return render_template('wishlist.html', system=system[NAME], country=country[ID], flag=country[FLAG], region=region, games=games, add_url=ADD_URL, delete_url=DELETE_URL, show_url=SHOW_URL, now=datetime.utcnow(), emoji_nintendo=NINTENDO, emoji_plus=PLUS, emoji_star=STAR, emoji_warning=WARNING)
def update_games(system, wishlist_counts): now = datetime.utcnow() games_db = GamesDatabase() prices_db = PricesDatabase() games = { game.id: game for game in games_db.load_all(filter={SYSTEM: system}) } new_games_found = {} for region in REGIONS: saved_games = [ game.nsuids[region] for game in games.values() if game.nsuids.get(region) ] for nsuid, new_game in new_game_finders[region](system, saved_games): new_games_found[region] = new_games_found.get(region, 0) + 1 game = games.get(new_game.id, Game(_id=new_game.id, system=system)) game.nsuids[region] = new_game.nsuids.get(region) title = new_game.titles.get(region) for lookup, replacement in TITLE_FIXES.items(): if lookup in title: title = title.replace(lookup, replacement) game.titles[region] = title game.release_dates[region] = new_game.release_dates.get(region) game.categories += new_game.categories game.categories = list(set(game.categories)) game.number_of_players = max( [game.number_of_players, new_game.number_of_players]) game.published_by_nintendo = any( [game.published_by_nintendo, new_game.published_by_nintendo]) game.free_to_play = any([game.free_to_play, new_game.free_to_play]) for country, website in new_game.websites.items(): if website: game.websites[country] = website games[game.id] = game prices = {price.id: price for price in prices_db.load_all()} for game_id, game in games.items(): week = str(int(now.strftime("%V"))) game.wishlisted_history[week] = wishlist_counts.get(game_id, 0) game.wishlisted = game.wishlisted_average if game.scores.next_update < now: game.scores = metacritic.get_scores(system, game.titles.values()) try: GamesDatabase().save(game) except Exception as e: LOG.error(f'Error saving {game}: {str(e)}') continue for region, nsuid in game.nsuids.items(): if not nsuid: continue save = False price = prices.get(nsuid) if not price: save = True price = Price(_id=nsuid, game_id=game.id, system=system, region=region) for country, details in COUNTRIES.items(): if details[REGION] == region and country not in price.prices: price.prices[country] = None save = True if save: LOG.info(f'Saving {game.title} ({nsuid}) into prices') prices_db.save(price) return f'{system} games found: {new_games_found}'