2019-08-02 12:54:18 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
import logging
|
|
|
|
|
2019-09-04 17:45:25 +01:00
|
|
|
from spotframework.net.user import NetworkUser
|
2019-08-10 17:53:50 +01:00
|
|
|
from spotframework.net.network import Network
|
|
|
|
|
2019-08-02 12:54:18 +01:00
|
|
|
db = firestore.Client()
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-08-02 12:54:18 +01:00
|
|
|
|
|
|
|
def create_playlist(username, name):
|
|
|
|
|
2019-08-17 18:30:13 +01:00
|
|
|
logger.info(f'creating {username} / {name}')
|
|
|
|
|
2019-08-02 12:54:18 +01:00
|
|
|
users = [i for i in db.collection(u'spotify_users').where(u'username', u'==', username).stream()]
|
|
|
|
|
|
|
|
if len(users) == 1:
|
|
|
|
|
|
|
|
user_dict = users[0].to_dict()
|
|
|
|
spotify_keys = db.document('key/spotify').get().to_dict()
|
|
|
|
|
2019-09-04 17:45:25 +01:00
|
|
|
net = Network(NetworkUser(spotify_keys['clientid'],
|
|
|
|
spotify_keys['clientsecret'],
|
2019-09-15 03:34:00 +01:00
|
|
|
user_dict['refresh_token'],
|
|
|
|
user_dict['access_token']))
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-09-15 15:33:29 +01:00
|
|
|
playlist = net.create_playlist(net.user.username, name)
|
2019-08-14 22:57:58 +01:00
|
|
|
|
2019-09-15 15:33:29 +01:00
|
|
|
if playlist is not None:
|
|
|
|
return playlist
|
2019-08-14 22:57:58 +01:00
|
|
|
else:
|
2019-08-17 18:30:13 +01:00
|
|
|
logger.error(f'no response received {username} / {name}')
|
|
|
|
return None
|
2019-08-02 12:54:18 +01:00
|
|
|
|
2019-08-10 17:53:50 +01:00
|
|
|
else:
|
2019-08-17 18:30:13 +01:00
|
|
|
logger.error(f'{len(users)} users found')
|
|
|
|
return None
|