def get_full_trending_playlists(request, args, strategy): offset, limit = format_offset(args), format_limit(args, TRENDING_LIMIT) current_user_id, time = args.get("user_id"), args.get("time", "week") time = "week" if time not in ["week", "month", "year"] else time # If we have a user_id, we call into `get_trending_playlist` # which fetches the cached unpopulated tracks and then # populates metadata. Otherwise, just # retrieve the last cached value. # # If current_user_id, # apply limit + offset inside the cached calculation. # Otherwise, apply it here. if current_user_id: args = { 'time': time, 'with_tracks': True, 'limit': limit, 'offset': offset } decoded = decode_string_id(current_user_id) args["current_user_id"] = decoded playlists = get_trending_playlists(args, strategy) else: args = { 'time': time, 'with_tracks': True, } key = get_trending_cache_key(to_dict(request.args), request.path) playlists = use_redis_cache( key, TRENDING_TTL_SEC, lambda: get_trending_playlists(args, strategy)) playlists = playlists[offset:limit + offset] return playlists
def get_trending(args): """Get Trending, shared between full and regular endpoints.""" # construct args time = args.get("time") if args.get("time") is not None else 'week' current_user_id = args.get("user_id") args = { 'time': time, 'genre': args.get("genre", None), 'with_users': True, 'limit': TRENDING_LIMIT, 'offset': 0 } # decode and add user_id if necessary if current_user_id: decoded_id = decode_string_id(current_user_id) args["current_user_id"] = decoded_id tracks = get_trending_tracks(args) return list(map(extend_track, tracks))
def get_underground_trending(request, args, strategy): offset, limit = format_offset(args), format_limit(args, TRENDING_LIMIT) current_user_id = args.get("user_id") args = {'limit': limit, 'offset': offset} # If user ID, let _get_underground_trending # handle caching + limit + offset if current_user_id: decoded = decode_string_id(current_user_id) args["current_user_id"] = decoded trending = _get_underground_trending(args, strategy) else: # If no user ID, fetch all cached tracks # and perform pagination here, passing # no args so we get the full list of tracks. key = get_trending_cache_key(to_dict(request.args), request.path) trending = use_redis_cache( key, TRENDING_TTL_SEC, lambda: _get_underground_trending({}, strategy)) trending = trending[offset:limit + offset] return trending
def get_random_tracks(args): """Gets random tracks from trending by getting the currently cached tracks and then populating them.""" exclusion_list = args.get("exclusion_list") or [] time = args.get("time") if args.get("time") is not None else 'week' current_user_id = args.get("user_id") args = { 'time': time, 'genre': args.get("genre", None), 'with_users': True, 'limit': args.get("limit"), 'offset': 0 } # decode and add user_id if necessary if current_user_id: args["current_user_id"] = decode_string_id(current_user_id) tracks = get_trending_tracks(args) filtered_tracks = list( filter(lambda track: track['track_id'] not in exclusion_list, tracks)) random.shuffle(filtered_tracks) return list(map(extend_track, filtered_tracks))