pysonic/pysonic/library.py

100 lines
2.9 KiB
Python
Raw Normal View History

2017-08-13 18:56:13 -07:00
import os
2017-08-13 21:13:46 -07:00
import logging
2017-08-13 18:56:13 -07:00
from pysonic.scanner import PysonicFilesystemScanner
2017-08-16 00:05:26 -07:00
from pysonic.types import MUSIC_TYPES
2017-08-13 18:56:13 -07:00
LETTER_GROUPS = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
2017-08-13 21:13:46 -07:00
"u", "v", "w", "xyz", "0123456789"]
logging = logging.getLogger("library")
2017-08-13 18:56:13 -07:00
def memoize(function):
memo = {}
def wrapper(*args):
if args in memo:
return memo[args]
else:
rv = function(*args)
memo[args] = rv
return rv
return wrapper
class NoDataException(Exception):
pass
class PysonicLibrary(object):
def __init__(self, database):
self.db = database
2018-04-05 19:02:17 -07:00
self.get_libraries = self.db.get_libraries
self.get_artists = self.db.get_artists
self.get_albums = self.db.get_albums
# self.get_song = self.db.get_song
# self.get_cover = self.db.get_cover
2018-04-07 16:26:27 -07:00
self.get_podcasts = self.db.get_podcasts
2018-04-05 19:02:17 -07:00
2017-08-13 18:56:13 -07:00
self.scanner = PysonicFilesystemScanner(self)
2017-08-13 21:13:46 -07:00
logging.info("library ready")
2017-08-13 18:56:13 -07:00
def update(self):
2018-04-05 19:02:17 -07:00
"""
Start the library media scanner ands
"""
2017-08-13 18:56:13 -07:00
self.scanner.init_scan()
2018-04-05 19:02:17 -07:00
def add_root_dir(self, path):
2017-08-13 18:56:13 -07:00
"""
2018-04-05 19:02:17 -07:00
The music library consists of a number of root dirs. This adds a new root
2017-08-13 18:56:13 -07:00
"""
2018-04-05 19:02:17 -07:00
path = os.path.abspath(os.path.normpath(path))
self.db.add_root(path)
2017-08-13 18:56:13 -07:00
2018-04-05 19:02:17 -07:00
# def get_artists(self, *args, **kwargs):
# artists = self.db.get_artists(*args, **kwargs)
# for item in artists:
# item["parent"] = item["libraryid"]
# return artists
2017-08-13 18:56:13 -07:00
2018-04-05 19:02:17 -07:00
# def get_albums(self, *args, **kwargs):
# albums = self.db.get_albums(*args, **kwargs)
# for item in albums:
# item["parent"] = item["artistid"]
# return albums
2017-08-13 22:08:40 -07:00
2017-08-13 18:56:13 -07:00
def get_artist_info(self, item_id):
2018-04-05 19:02:17 -07:00
#TODO
2017-08-13 18:56:13 -07:00
return {"biography": "placeholder biography",
"musicBrainzId": "playerholder",
"lastFmUrl": "https://www.last.fm/music/Placeholder",
"smallImageUrl": "",
"mediumImageUrl": "",
"largeImageUrl": "",
"similarArtists": []}
2017-08-15 21:40:38 -07:00
2018-04-05 19:02:17 -07:00
def get_cover(self, cover_id):
cover = self.db.get_cover(cover_id)
library = self.db.get_libraries(cover["library"])[0]
cover['_fullpath'] = os.path.join(library["path"], cover["path"])
return cover
def get_song(self, song_id):
song = self.db.get_songs(id=song_id)[0]
library = self.db.get_libraries(song["library"])[0]
song['_fullpath'] = os.path.join(library["path"], song["file"])
return song
def get_playlist(self, playlist_id):
playlist_info = self.db.get_playlist(playlist_id)
songs = self.db.get_playlist_songs(playlist_id)
return (playlist_info, songs)
def delete_playlist(self, playlist_id):
self.db.empty_playlist(playlist_id)
self.db.delete_playlist(playlist_id)