def get_friends(self, queue_friends_of_friends=False, relationship='friend'): cache_key = repr(self) + ':get_friends' f_ids = ext.flask_redis.get(cache_key) if f_ids: f_ids = msgpack.loads(f_ids) # todo: this seems always empty log.info("Found cached friends of %r: %s", self, f_ids) else: log.info("Fetching friends of %r", self) r = steam_api.get_json( "ISteamUser/GetFriendList", steamid=self.steamid, ) f_ids = [] if r: for friends_data in r['friendslist']['friends']: f_ids.append(friends_data['steamid']) log.debug("Found friends of %r: %s", self, f_ids) # todo: this seems tonot work ext.flask_redis.set(cache_key, msgpack.dumps(f_ids), ex=DEFAULT_TTL) else: log.warning("Failed fetching friends of %r", self) return self.get_users( f_ids, queue_friends_of_friends=queue_friends_of_friends)
def get_friends(self, queue_friends_of_friends=False, relationship='friend'): cache_key = repr(self) + ':get_friends' f_ids = ext.flask_redis.get(cache_key) if f_ids: f_ids = msgpack.loads(f_ids) # todo: this seems always empty log.info("Found cached friends of %r: %s", self, f_ids) else: log.info("Fetching friends of %r", self) r = steam_api.get_json( "ISteamUser/GetFriendList", steamid=self.steamid, ) f_ids = [] if r: for friends_data in r['friendslist']['friends']: f_ids.append(friends_data['steamid']) log.debug("Found friends of %r: %s", self, f_ids) # todo: this seems tonot work ext.flask_redis.set(cache_key, msgpack.dumps(f_ids), ex=DEFAULT_TTL) else: log.warning("Failed fetching friends of %r", self) return self.get_users(f_ids, queue_friends_of_friends=queue_friends_of_friends)
def get_games(self, include_appinfo=1, include_played_free_games=1, queue_details=0): cache_key = "{!r}:get_games:{}:{}".format(self, include_appinfo, include_played_free_games) # todo: just cache the game ids and let the icon hashes be cached elsewhere game_datas = ext.flask_redis.get(cache_key) if game_datas: game_datas = msgpack.loads(game_datas) else: game_datas = [] log.info("Fetching games of %r", self) # this seem to be the onl place we can get the icon and logo hashes games_json = steam_api.get_json( "IPlayerService/GetOwnedGames", include_appinfo=include_appinfo, include_played_free_games=include_played_free_games, steamid=self.steamid, ) if games_json and 'response' in games_json and 'games' in games_json[ 'response']: for game_data in games_json['response']['games']: # todo: sometimes string, sometimes int... if str(game_data['appid']) in SteamApp.skipped_appids: continue game_datas.append({ 'appid': game_data['appid'], 'name': ''.join(s for s in game_data['name'] if s in string.printable), # todo: support non-ascii? 'img_icon_hash': game_data['img_icon_url'], 'img_logo_hash': game_data['img_logo_url'], }) ext.flask_redis.set(cache_key, msgpack.dumps(game_datas), ex=DEFAULT_TTL) else: log.warning("Bad response fetching games for %r: %s", self, games_json) games = [] for game_data in game_datas: games.append(SteamApp(queue_details=queue_details, **game_data)) return games
def id_to_id64(cls, steamid): cache_key = cls.__name__ + ':id_to_id64:' + steamid result = ext.flask_redis.get(cache_key) if not result: log.info("Fetching steam64id of %s", steamid) r = steam_api.get_json( "ISteamUser/ResolveVanityURL", vanityurl=steamid, ) if r: try: result = r['response']['steamid'] except (KeyError, TypeError): result = None else: ext.flask_redis.set(cache_key, result, ex=DEFAULT_TTL) return result
def get_games(self, include_appinfo=1, include_played_free_games=1, queue_details=0): cache_key = "{!r}:get_games:{}:{}".format(self, include_appinfo, include_played_free_games) # todo: just cache the game ids and let the icon hashes be cached elsewhere game_datas = ext.flask_redis.get(cache_key) if game_datas: game_datas = msgpack.loads(game_datas) else: game_datas = [] log.info("Fetching games of %r", self) # this seem to be the onl place we can get the icon and logo hashes games_json = steam_api.get_json( "IPlayerService/GetOwnedGames", include_appinfo=include_appinfo, include_played_free_games=include_played_free_games, steamid=self.steamid, ) if games_json and 'response' in games_json and 'games' in games_json['response']: for game_data in games_json['response']['games']: # todo: sometimes string, sometimes int... if str(game_data['appid']) in SteamApp.skipped_appids: continue game_datas.append({ 'appid': game_data['appid'], 'name': ''.join(s for s in game_data['name'] if s in string.printable), # todo: support non-ascii? 'img_icon_hash': game_data['img_icon_url'], 'img_logo_hash': game_data['img_logo_url'], }) ext.flask_redis.set(cache_key, msgpack.dumps(game_datas), ex=DEFAULT_TTL) else: log.warning("Bad response fetching games for %r: %s", self, games_json) games = [] for game_data in game_datas: games.append(SteamApp(queue_details=queue_details, **game_data)) return games
def get_users(cls, steamid64s, queue_friends_of_friends=False): users = [] if not steamid64s: return users if isinstance(steamid64s, basestring): raise ValueError cache_key_template = cls.__name__ + ':{}' # fetch any users in the cache cached_user_keys = [cache_key_template.format(i) for i in steamid64s] cached_users = ext.flask_redis.mget(*cached_user_keys) # todo: izip didn't work for us... # for steamid, user_data in itertools.izip(steamid64s, cached_users): for user_data in cached_users: if not user_data: # user wasn't cached, we will query any cache misses with one query later continue user_data = msgpack.loads(user_data) if user_data: if 'error' in user_data and user_data['error']: # we cached a missing user. dont query them again until data expires steamid64s.remove(user_data['steamid']) else: # we cached good user data u = cls(queue_friends_of_friends=queue_friends_of_friends, **user_data) users.append(u) # don't query this user later (even if we cached empty data for them) steamid64s.remove(u.steamid) log.debug("Retrieved users from cache: %s", users) if steamid64s: # todo: split this into multiple queries if its a lot of ids? # fetch any users not in the cache fetched_ids = [] r = steam_api.get_json( "ISteamUser/GetPlayerSummaries", version=2, steamids=steamid64s, ) if r: p = ext.flask_redis.pipeline() for user_data in r['response']['players']: u = cls(queue_friends_of_friends=queue_friends_of_friends, **user_data) users.append(u) fetched_ids.append(str( u.steamid)) # this is getting annoying cache_key = cache_key_template.format(u.steamid) p.set(cache_key, msgpack.dumps(user_data), ex=DEFAULT_TTL) log.debug("Fetched users from steam: %s", fetched_ids) """ if fetched_ids != steamid64s: missed_ids = set(steamid64s) - set(fetched_ids) log.warning("Unable to query all users. Missed: %s", missed_ids) for steamid in missed_ids: cache_key = cache_key_template.format(steamid) p.set( cache_key, msgpack.dumps({ 'steamid': steamid, 'error': True, }), ex=DEFAULT_TTL, ) """ p.execute() return users
def get_users(cls, steamid64s, queue_friends_of_friends=False): users = [] if not steamid64s: return users if isinstance(steamid64s, basestring): raise ValueError cache_key_template = cls.__name__ + ':{}' # fetch any users in the cache cached_user_keys = [cache_key_template.format(i) for i in steamid64s] cached_users = ext.flask_redis.mget(*cached_user_keys) # todo: izip didn't work for us... # for steamid, user_data in itertools.izip(steamid64s, cached_users): for user_data in cached_users: if not user_data: # user wasn't cached, we will query any cache misses with one query later continue user_data = msgpack.loads(user_data) if user_data: if 'error' in user_data and user_data['error']: # we cached a missing user. dont query them again until data expires steamid64s.remove(user_data['steamid']) else: # we cached good user data u = cls(queue_friends_of_friends=queue_friends_of_friends, **user_data) users.append(u) # don't query this user later (even if we cached empty data for them) steamid64s.remove(u.steamid) log.debug("Retrieved users from cache: %s", users) if steamid64s: # todo: split this into multiple queries if its a lot of ids? # fetch any users not in the cache fetched_ids = [] r = steam_api.get_json( "ISteamUser/GetPlayerSummaries", version=2, steamids=steamid64s, ) if r: p = ext.flask_redis.pipeline() for user_data in r['response']['players']: u = cls(queue_friends_of_friends=queue_friends_of_friends, **user_data) users.append(u) fetched_ids.append(str(u.steamid)) # this is getting annoying cache_key = cache_key_template.format(u.steamid) p.set(cache_key, msgpack.dumps(user_data), ex=DEFAULT_TTL) log.debug("Fetched users from steam: %s", fetched_ids) """ if fetched_ids != steamid64s: missed_ids = set(steamid64s) - set(fetched_ids) log.warning("Unable to query all users. Missed: %s", missed_ids) for steamid in missed_ids: cache_key = cache_key_template.format(steamid) p.set( cache_key, msgpack.dumps({ 'steamid': steamid, 'error': True, }), ex=DEFAULT_TTL, ) """ p.execute() return users