async def put_artist(request: Request, id: str, body: PutTrack): profile_id = get_profile_id_from(request) user_data = user.get_user_data(profile_id) if body.mute and id not in user_data.blocking_artists: user_data.blocking_tracks.append(id) if not body.mute and id in user_data.blocking_artists: user_data.blocking_artists.append(id) user_data.save()
def replace_playlist(): client_credentials = config.get_client_credentials() user_data = user.get_user_data() assert user_data.token user_resource_api_client = api.UserResourceApiClient( client_credentials, user_data.token) api_client = api.ApiClient(client_credentials) ranking_client = data.RankingClient() ranking_service = service.RankingService(api_client, ranking_client) api_service = service.UserService(user_data, user_resource_api_client, ranking_service) api_service.replace_playlist()
async def login_callback(request: Request, code: str, origin: str): base_url = origin if not base_url: base_url = str(request.base_url) redirect_uri = f"{base_url.strip('/')}{REDIRECT_PATH}" logger.info("redirec_url: %s", redirect_uri) try: token = api.fetch_token(code, client_credentials, redirect_uri) except Exception as e: logger.error(str(e)) return RedirectResponse('/', 302) client = api.UserResourceApiClient(client_credentials, token) profile = client.get_current_user_profile() user_data = user.get_user_data(profile.id) user_data.profile = profile user_data.token = token user_data.save() return jwt.encode(dict(sub=profile.id, iss=int(time.time())), JWT_SECRET, JWT_ALG)
async def tracks_and_artists(request: Request): profile_id = get_profile_id_from(request) user_data = user.get_user_data(profile_id) tracks = ranking_service.get_tracks() blocking_tracks = set(user_data.blocking_tracks) blocking_artists = set(user_data.blocking_artists) user_tracks = [] user_artists = {} for track in tracks: artist_ids = [] for artist in track.artists: artist_ids.append(artist.id) if artist.id in user_artists: continue user_artists[artist.id] = data.UserArtist( artist.id, artist.name, artist.id in blocking_artists) user_tracks.append( data.UserTrack(track.id, track.name, artist_ids, track.id in blocking_tracks)) return dict(tracks=user_tracks, artists=list(user_artists.values()))
def get_api_service(profile_id: str) -> service.UserService: return service.UserService(user.get_user_data(profile_id), get_user_resource_api_client(profile_id), ranking_service)
def get_user_resource_api_client(profile_id: str) -> api.UserResourceApiClient: user_data = user.get_user_data(profile_id) if not user_data.token: raise Exception('tokenがありません') return api.UserResourceApiClient(client_credentials, user_data.token)
async def set_target_playlist(request: Request, playlist_id: str): profile_id = get_profile_id_from(request) user_data = user.get_user_data(profile_id) user_data.reload_playlist_id = playlist_id user_data.save()