2019-08-17 18:30:13 +01:00
|
|
|
import logging
|
2021-03-24 10:06:54 +00:00
|
|
|
from typing import Optional
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2019-10-19 17:14:11 +01:00
|
|
|
import music.db.database as database
|
2020-06-22 20:21:54 +01:00
|
|
|
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
|
2019-08-10 17:53:50 +01:00
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-02 12:54:18 +01:00
|
|
|
|
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
|
|
|
|
"""
|
2019-08-17 18:30:13 +01:00
|
|
|
|
2020-05-08 15:19:27 +01:00
|
|
|
if user is None:
|
2020-06-21 15:30:51 +01:00
|
|
|
logger.error(f'username not provided')
|
2020-05-08 15:19:27 +01:00
|
|
|
return
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2020-06-21 15:30:51 +01:00
|
|
|
logger.info(f'creating spotify playlist for {user.username} / {name}')
|
2020-05-08 15:19:27 +01:00
|
|
|
net = database.get_authed_spotify_network(user)
|
2019-08-14 22:57:58 +01:00
|
|
|
|
2020-06-22 20:21:54 +01:00
|
|
|
try:
|
|
|
|
return net.create_playlist(net.user.user.display_name, name)
|
2020-07-01 11:03:43 +01:00
|
|
|
except SpotifyNetworkException:
|
|
|
|
logger.exception(f'error ocurred {user.username} / {name}')
|
2020-05-08 15:19:27 +01:00
|
|
|
return
|