def handle(self, request: GetLibraryRequest) -> GetLibraryResponse: try: library = self._library_repo.find_by_user_id(request.user_id) except Exception as e: logging.exception(e) raise UnexpectedError("Failed to get library.") return GetLibraryResponse(library)
def handle(self, request: CreateLibraryRequest) -> CreateLibraryResponse: try: user_id = self._library_repo.insert(request.user_id) except Exception as e: logging.exception(e) raise UnexpectedError("Failed to create library.") return CreateLibraryResponse(user_id)
def handle(self, request: GetTrendingRequest) -> GetTrendingResponse: results = self._spotify.new_releases(limit=request.limit, offset=request.offset)["albums"]["items"] if not results: raise UnexpectedError("Not found trending.") trending = [ Trending(title=r["name"], external_url=r["artists"][0]["external_urls"]["spotify"]) for r in results ] return GetTrendingResponse(trending)
def handle(self, request: TitleSearchRequest) -> TitleSearchResponse: song = self._genius.search_song(request.title, get_full_info=False) if not song: raise UnexpectedError(f"Not found {request.title}.") songs = [ Song(artist=song.artist, title=song.title, lyrics=song.lyrics, jacket_image_url=song.song_art_image_url) ] return TitleSearchResponse(songs)
def handle(self, request: GetRecommendedRequest) -> GetRecommendedResponse: results = self._spotify.new_releases( limit=request.limit, offset=request.offset)["albums"]["items"] if not results: raise UnexpectedError("Not found recommended.") recommended = [ Recommended( artist=r["artists"][0]["name"], title=r["name"], jacket_image_url=r["images"][0]["url"], external_url=r["external_urls"]["spotify"], ) for r in results ] return GetRecommendedResponse(recommended)
def handle(self, request: ArtistSearchRequest) -> ArtistSearchResponse: artist = self._genius.search_artist(request.artist, max_songs=4, get_full_info=False) if not artist: raise UnexpectedError(f"Not found songs of {artist}.") songs = [ Song(artist=song.artist, title=song.title, lyrics=song.lyrics, jacket_image_url=song.song_art_image_url) for song in artist.songs ] return ArtistSearchResponse(songs)
def handle(self, request: AddSongRequest) -> AddSongResponse: try: user_id = self._library_repo.update( Library( user_id=request.user_id, songs=[ Song( artist=request.artist, title=request.title, lyrics=request.lyrics, jacket_image_url=request.jacket_image_url, ) ], )) except Exception as e: logging.exception(e) raise UnexpectedError("Failed to create library.") return AddSongResponse(user_id)