def track_from_upload(name, **kwargs) -> Optional[Track]: plugin_description = memcache.get("upload.plugins").get(name) if plugin_description is None: return Response.INVALID_PLUGIN plugin = __load_plugin(name, plugin_description["path"]) track_record = plugin.handle(**kwargs) # type: TrackRecord if track_record.backend is None or track_record.mrl is None: return Response.PLUGIN_ERROR if tracks.exists(track_record.title, track_record.artist, track_record.album): __cleanup(track_record.mrl) return Response.TRACK_EXISTS track = None with persistance(): track = tracks.get_or_create(track_record.title, track_record.artist, track_record.album) track.backend = track_record.backend track.mrl = track_record.mrl # TODO: tag info db_session.add(track) return track
def queue_add(track_id=None): track = tracks.get(track_id) if track is None: return json.dumps({ 'success': False, 'reason': Response.INVALID_OBJECT_ID }) if not track.status.imported: return json.dumps({ 'success': False, 'reason': Response.TRACK_UNAVAILABLE }) with persistance(): track.status.available = player.available(track.mrl, track.backend) if not track.status.available: return json.dumps({ 'success': False, 'reason': Response.TRACK_UNAVAILABLE }) queue.add(track_id, g.user_token) return json.dumps({ 'success': True })
def register(username: str, password: str, privilege_level: int) -> bool: """ Registers a new user. Checks, if the user already exists and only registers a new one, if the username isn't already taken. :param username: The new user's username :param password: The new user's password :param privilege_level: The new user's privilege level :return: Whether the new user was registered or not """ password_salt = os.urandom(512) password_hashed = argon2.argon2_hash(password, password_salt) del password if users.get(username) is not None: return False user = users.create(username, password_hashed, password_salt) with persistance(): user.privilege_level = privilege_level user_token = os.urandom(64).hex() while user_cache.exists(user_token): user_token = os.urandom(64).hex() pub('user_authenticated', user_token, username, user.privilege_level, config.get('user_ttl')) return True
def get_or_create(name: str) -> Artist: artist = db_session.query(Artist).filter(Artist.name == name).first() if artist is None: with persistance(): artist = Artist(name=name) db_session.add(artist) return artist
def create(username: str, playlist_name: str) -> Playlist: """ :param username: String :param playlist_name: String :param track_id: :return: """ with persistance(): playlist = Playlist(user_id=username, name=playlist_name) db_session.add(playlist) return playlist
def get_or_create(title: str, artist: str, album: str) -> Track: a = albums.get_or_create(album, artist) t = db_session.query(Track)\ .filter(Track.title == title)\ .filter(Track.album == a)\ .filter(Track.artist == a.artist)\ .first() if t is None: with persistance(): t = Track(title=title) t.album = a t.artist = t.album.artist db_session.add(t) if t.status is None: with persistance(): s = Status(t) db_session.add(s) return t
def remove(playlist_id: int) -> bool: """ removes Playlist by given playlist_id :param playlist_id: unique int of Playlist to remove :return: succeed? """ playlist = db_session.query(Playlist).filter(Playlist.id == playlist_id) if playlist is None: return False else: with persistance(): db_session.query(Playlist).filter( Playlist.id == playlist_id).delete() return True
def add_track(playlist_id: int, track_id: int) -> bool: """ Add Track by id in Playlist by id :param playlist_id: :param track_id: :return: """ # TODO: Check if get returns None playlist = get(playlist_id) with persistance(): playlist_tracks = PlaylistTrack(track_id=track_id) playlist_tracks.playlist = playlist db_session.add(playlist_tracks) return True
def get_or_create(title: str, artist: str) -> Album: a = artists.get_or_create(artist) album = db_session.query(Album)\ .filter(Album.title == title)\ .filter(Album.artist == a) \ .first() if album is None: with persistance(): album = Album(title=title) album.artist = a db_session.add(album) return album
def remove_track(playlist_id: int, track_id: int) -> bool: """ removes Track by id in Playlist by id :param playlist_id: unique int of Playlist to remove :param track_id: unique int of track to remove :return: succeed? """ playlist_track = db_session.query(PlaylistTrack). \ filter(PlaylistTrack.playlist_id == playlist_id). \ filter(PlaylistTrack.track_id == track_id).first() if playlist_track is None: return False with persistance(): db_session.query(PlaylistTrack).filter(PlaylistTrack.id == playlist_track.id).\ filter(PlaylistTrack.track_id == track_id).delete() return True
def generate_sample_tracks(n: int = 10000): import Pynitus.model.db.database init_db() for i in range(n): random_title = os.urandom(32).hex() random_album_title = os.urandom(32).hex() random_artist_name = os.urandom(32).hex() t = tracks.get_or_create(random_title, random_artist_name, random_album_title) with persistance(): t.status.available = True t.status.imported = True t.backend = "vlc_backend" t.mrl = "test.mp3" db_session.close() db_session.remove()
def create(username: str, password_hashed: bytes, password_salt: bytes) -> User: with persistance(): u = User(username=username, password_hash=password_hashed, password_salt=password_salt) db_session.add(u) return u