Mixonomer/music/tasks/create_playlist.py

38 lines
1.0 KiB
Python
Raw Normal View History

import logging
2021-03-24 10:06:54 +00:00
from typing import Optional
2019-10-19 17:14:11 +01:00
import music.db.database as database
from spotframework.net.network import SpotifyNetworkException
2021-03-24 10:06:54 +00:00
from spotframework.model.playlist import FullPlaylist
from music.model.user import User
logger = logging.getLogger(__name__)
2021-03-24 10:06:54 +00:00
def create_playlist(user: User, name: str) -> Optional[FullPlaylist]:
"""Create a new playlist on the user's Spotify account
For creating new playlists, create and return a new playlist object
Args:
user (User): Subject user
name (str): Name of new playlist
Returns:
Optional[FullPlaylist]: New playlist object if created
"""
if user is None:
logger.error(f'username not provided')
return
logger.info(f'creating spotify playlist for {user.username} / {name}')
net = database.get_authed_spotify_network(user)
2019-08-14 22:57:58 +01:00
try:
return net.create_playlist(net.user.user.display_name, name)
except SpotifyNetworkException:
logger.exception(f'error ocurred {user.username} / {name}')
return