|
| 1 | +from logging import Logger |
| 2 | +from flask import Blueprint, jsonify, make_response, request |
| 3 | +from src.database.crud.playlist import get_user_playlists |
| 4 | +from src.dataclasses.playback_info import PlaybackInfo |
| 5 | +from src.dataclasses.playback_request import StartPlaybackRequest |
| 6 | +from src.dataclasses.playlist import Playlist |
| 7 | +from src.spotify import SpotifyClient |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +def music_controller(spotify: SpotifyClient): |
| 12 | + music_controller = Blueprint( |
| 13 | + name="music_controller", import_name=__name__, url_prefix="/music" |
| 14 | + ) |
| 15 | + |
| 16 | + @music_controller.route("playlists") |
| 17 | + def index(): |
| 18 | + user_id = request.cookies.get("user_id") |
| 19 | + limit = request.args.get("limit", type=int) |
| 20 | + offset = request.args.get("offset", type=int) |
| 21 | + search = request.args.get("search") |
| 22 | + sort_by = request.args.get("sort_by") |
| 23 | + desc = request.args.get("desc") == "True" |
| 24 | + return jsonify( |
| 25 | + get_user_playlists( |
| 26 | + user_id=user_id, |
| 27 | + limit=limit, |
| 28 | + offset=offset, |
| 29 | + search=search, |
| 30 | + sort_by=sort_by, |
| 31 | + desc=desc, |
| 32 | + as_dicts=True, |
| 33 | + ) |
| 34 | + ) |
| 35 | + |
| 36 | + @music_controller.route("playlist/<id>", methods=["GET"]) |
| 37 | + def get_playlist(id): |
| 38 | + access_token = request.cookies.get("spotify_access_token") |
| 39 | + playlist = spotify.get_playlist(access_token=access_token, id=id) |
| 40 | + return playlist.model_dump() |
| 41 | + |
| 42 | + @music_controller.route("playlist/<id>", methods=["POST"]) |
| 43 | + def post_edit_playlist(id): |
| 44 | + access_token = request.cookies.get("spotify_access_token") |
| 45 | + name = request.json.get("name") |
| 46 | + description = request.json.get("description") |
| 47 | + spotify.update_playlist( |
| 48 | + access_token=access_token, |
| 49 | + id=id, |
| 50 | + name=name, |
| 51 | + description=description, |
| 52 | + ) |
| 53 | + return make_response("playlist updated", 204) |
| 54 | + |
| 55 | + @music_controller.route("playlist/<id>/albums", methods=["GET"]) |
| 56 | + def get_playlist_album_info(id): |
| 57 | + access_token = request.cookies.get("spotify_access_token") |
| 58 | + return [ |
| 59 | + album.model_dump() |
| 60 | + for album in spotify.get_playlist_album_info( |
| 61 | + access_token=access_token, id=id |
| 62 | + ) |
| 63 | + ] |
| 64 | + |
| 65 | + @music_controller.route("find_associated_playlists", methods=["POST"]) |
| 66 | + def find_associated_playlists(): |
| 67 | + access_token = request.cookies.get("spotify_access_token") |
| 68 | + user_id = request.cookies.get("user_id") |
| 69 | + playlist = Playlist.model_validate(request.json) |
| 70 | + associated_playlists = spotify.find_associated_playlists( |
| 71 | + user_id=user_id, access_token=access_token, playlist=playlist |
| 72 | + ) |
| 73 | + return [ |
| 74 | + associated_playlist.model_dump() |
| 75 | + for associated_playlist in associated_playlists |
| 76 | + ] |
| 77 | + |
| 78 | + return music_controller |
0 commit comments