2019-08-26 18:31:21 +01:00
|
|
|
from google.cloud import firestore
|
|
|
|
import logging
|
2019-09-27 10:32:42 +01:00
|
|
|
from datetime import timedelta, datetime, timezone
|
2019-09-16 02:22:58 +01:00
|
|
|
from typing import List, Optional
|
2019-09-25 19:28:38 +01:00
|
|
|
from werkzeug.security import check_password_hash
|
2019-08-26 18:31:21 +01:00
|
|
|
|
2019-10-07 12:21:26 +01:00
|
|
|
from spotframework.net.network import Network as SpotifyNetwork
|
|
|
|
from fmframework.net.network import Network as FmNetwork
|
2019-09-27 10:32:42 +01:00
|
|
|
from spotify.db.user import DatabaseUser
|
|
|
|
|
2019-08-26 18:31:21 +01:00
|
|
|
db = firestore.Client()
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-09-27 10:32:42 +01:00
|
|
|
def refresh_token_database_callback(user):
|
|
|
|
if isinstance(user, DatabaseUser):
|
|
|
|
user_ref = get_user_doc_ref(user.user_id)
|
|
|
|
|
|
|
|
user_ref.update({
|
2019-10-01 19:20:22 +01:00
|
|
|
'access_token': user.access_token,
|
|
|
|
'refresh_token': user.refresh_token,
|
2019-09-27 10:32:42 +01:00
|
|
|
'last_refreshed': user.last_refreshed,
|
|
|
|
'token_expiry': user.token_expiry
|
|
|
|
})
|
|
|
|
logger.debug(f'{user.user_id} database entry updated')
|
|
|
|
else:
|
|
|
|
logger.error('user has no attached id')
|
|
|
|
|
|
|
|
|
2019-10-07 12:21:26 +01:00
|
|
|
def get_authed_spotify_network(username):
|
2019-09-27 10:32:42 +01:00
|
|
|
|
|
|
|
user = get_user_doc_ref(username)
|
|
|
|
if user:
|
|
|
|
user_dict = user.get().to_dict()
|
|
|
|
|
|
|
|
if user_dict.get('spotify_linked', None):
|
|
|
|
spotify_keys = db.document('key/spotify').get().to_dict()
|
|
|
|
|
|
|
|
user_obj = DatabaseUser(client_id=spotify_keys['clientid'],
|
|
|
|
client_secret=spotify_keys['clientsecret'],
|
|
|
|
refresh_token=user_dict['refresh_token'],
|
|
|
|
user_id=username,
|
|
|
|
access_token=user_dict['access_token'])
|
|
|
|
user_obj.on_refresh.append(refresh_token_database_callback)
|
|
|
|
|
|
|
|
if user_dict['last_refreshed'] + timedelta(seconds=user_dict['token_expiry'] - 1) \
|
|
|
|
< datetime.now(timezone.utc):
|
2019-10-01 19:20:22 +01:00
|
|
|
user_obj.refresh_access_token()
|
2019-09-27 10:32:42 +01:00
|
|
|
|
|
|
|
user_obj.refresh_info()
|
2019-10-07 12:21:26 +01:00
|
|
|
return SpotifyNetwork(user_obj)
|
2019-09-27 10:32:42 +01:00
|
|
|
else:
|
|
|
|
logger.error('user spotify not linked')
|
|
|
|
else:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
|
2019-10-07 12:21:26 +01:00
|
|
|
def get_authed_lastfm_network(username):
|
|
|
|
|
|
|
|
user = get_user_doc_ref(username)
|
|
|
|
if user:
|
|
|
|
user_dict = user.get().to_dict()
|
|
|
|
|
|
|
|
if user_dict.get('lastfm_username', None):
|
|
|
|
fm_keys = db.document('key/fm').get().to_dict()
|
|
|
|
|
|
|
|
return FmNetwork(username=user_dict['lastfm_username'], api_key=fm_keys['clientid'])
|
|
|
|
else:
|
|
|
|
logger.error(f'{username} has no last.fm username')
|
|
|
|
else:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
|
2019-09-25 19:28:38 +01:00
|
|
|
def check_user_password(username, password):
|
|
|
|
|
|
|
|
user = get_user_doc_ref(user=username)
|
|
|
|
if user:
|
|
|
|
user_dict = user.get().to_dict()
|
|
|
|
|
|
|
|
if check_password_hash(user_dict['password'], password):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
logger.error(f'password mismatch {username}')
|
|
|
|
else:
|
|
|
|
logger.error(f'user {username} not found')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
def get_user_query_stream(user: str) -> List[firestore.DocumentSnapshot]:
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
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 []
|
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
def get_user_doc_ref(user: str) -> Optional[firestore.DocumentReference]:
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
def get_user_playlists_collection(user_id: str) -> firestore.CollectionReference:
|
2019-08-26 18:31:21 +01:00
|
|
|
|
|
|
|
playlists = db.document(u'spotify_users/{}'.format(user_id)).collection(u'playlists')
|
|
|
|
|
|
|
|
return playlists
|
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
def get_user_playlist_ref_by_username(user: str, playlist: str) -> Optional[firestore.CollectionReference]:
|
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
|
|
|
|
|
|
|
|
|
2019-09-16 02:22:58 +01:00
|
|
|
def get_user_playlist_ref_by_user_ref(user_ref: firestore.DocumentReference,
|
|
|
|
playlist: str) -> Optional[firestore.CollectionReference]:
|
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}')
|
2019-09-27 10:32:42 +01:00
|
|
|
return query[0]
|
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
|