Mixonomer/music/db/part_generator.py

70 lines
2.0 KiB
Python
Raw Normal View History

from google.cloud import firestore
2019-10-19 17:14:11 +01:00
import music.db.database as database
2019-10-23 14:44:17 +01:00
from music.model.user import User
import logging
db = firestore.Client()
logger = logging.getLogger(__name__)
class PartGenerator:
2019-10-29 12:25:49 +00:00
def __init__(self, user: User = None, username: str = None):
self.queried_playlists = []
self.parts = []
2019-10-23 14:44:17 +01:00
if user:
self.user = user
elif username:
2019-10-23 14:44:17 +01:00
pulled_user = database.get_user(username)
if pulled_user:
self.user = pulled_user
else:
raise LookupError(f'{username} not found')
else:
raise NameError('no user info provided')
def reset(self):
self.queried_playlists = []
self.parts = []
def get_recursive_parts(self, name):
2019-10-23 14:44:17 +01:00
logger.info(f'getting part from {name} for {self.user.username}')
self.reset()
self.process_reference_by_name(name)
return [i for i in {i for i in self.parts}]
def process_reference_by_name(self, name):
2019-10-23 14:44:17 +01:00
playlist = database.get_playlist(username=self.user.username, name=name)
2019-10-23 14:44:17 +01:00
if playlist is not None:
2019-10-23 14:44:17 +01:00
if playlist.db_ref.id not in self.queried_playlists:
2019-10-23 14:44:17 +01:00
self.parts += playlist.parts
2019-10-23 14:44:17 +01:00
for i in playlist.playlist_references:
if i.id not in self.queried_playlists:
self.process_reference_by_reference(i)
else:
2019-10-23 14:44:17 +01:00
logger.warning(f'playlist reference {name} already queried')
else:
logger.warning(f'playlist reference {name} not found')
def process_reference_by_reference(self, ref):
if ref.id not in self.queried_playlists:
playlist_reference_object = ref.get().to_dict()
self.parts += playlist_reference_object['parts']
for i in playlist_reference_object['playlist_references']:
self.process_reference_by_reference(i)
else:
logger.warning(f'playlist reference {ref.get().to_dict()["name"]} already queried')