2019-08-26 18:31:21 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
import logging
|
|
|
|
|
|
|
|
db = firestore.Client()
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_query_stream(user):
|
|
|
|
|
|
|
|
users = [i for i in db.collection(u'spotify_users').where(u'username', u'==', user).stream()]
|
|
|
|
|
|
|
|
if len(users) > 0:
|
|
|
|
return users
|
|
|
|
else:
|
|
|
|
logger.warning(f'{user} not found')
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_doc_ref(user):
|
|
|
|
|
|
|
|
users = get_user_query_stream(user)
|
|
|
|
|
|
|
|
if len(users) > 0:
|
|
|
|
if len(users) == 1:
|
2019-08-27 00:13:42 +01:00
|
|
|
return users[0].reference
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
logger.error(f"multiple {user}'s found")
|
|
|
|
return None
|
|
|
|
|
|
|
|
else:
|
|
|
|
logger.error(f'{user} not found')
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_playlists_collection(user_id):
|
|
|
|
|
|
|
|
playlists = db.document(u'spotify_users/{}'.format(user_id)).collection(u'playlists')
|
|
|
|
|
|
|
|
return playlists
|
|
|
|
|
|
|
|
|
2019-08-27 00:13:42 +01:00
|
|
|
def get_user_playlist_ref_by_username(user, playlist):
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
user_ref = get_user_doc_ref(user)
|
|
|
|
|
|
|
|
if user_ref:
|
|
|
|
|
2019-08-27 00:13:42 +01:00
|
|
|
return get_user_playlist_ref_by_user_ref(user_ref, playlist)
|
2019-08-26 18:31:21 +01:00
|
|
|
|
2019-08-27 00:13:42 +01:00
|
|
|
else:
|
|
|
|
logger.error(f'{user} not found, looking up {playlist}')
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_user_playlist_ref_by_user_ref(user_ref, playlist):
|
2019-08-26 18:31:21 +01:00
|
|
|
|
2019-08-27 00:13:42 +01:00
|
|
|
playlist_collection = get_user_playlists_collection(user_ref.id)
|
|
|
|
|
|
|
|
username = user_ref.get().to_dict()['username']
|
|
|
|
|
|
|
|
if playlist_collection:
|
|
|
|
query = [i for i in playlist_collection.where(u'name', u'==', playlist).stream()]
|
|
|
|
|
|
|
|
if len(query) > 0:
|
|
|
|
if len(query) == 1:
|
|
|
|
if query[0].exists:
|
|
|
|
return query[0].reference
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
else:
|
2019-08-27 00:13:42 +01:00
|
|
|
logger.error(f'{playlist} for {username} does not exist')
|
|
|
|
return query[0]
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
else:
|
2019-08-27 00:13:42 +01:00
|
|
|
logger.error(f'{username} multiple response playlists found for {playlist}')
|
|
|
|
return query
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
else:
|
2019-08-27 00:13:42 +01:00
|
|
|
logger.error(f'{username} no playlist found for {playlist}')
|
2019-08-26 18:31:21 +01:00
|
|
|
return None
|
|
|
|
|
|
|
|
else:
|
2019-08-27 00:13:42 +01:00
|
|
|
logger.error(f'{username} playlist collection not found, looking up {playlist}')
|
2019-08-26 18:31:21 +01:00
|
|
|
return None
|