pysonic/pysonic/api.py

595 lines
24 KiB
Python
Raw Normal View History

2020-10-05 23:30:01 -07:00
import os
2017-08-13 21:13:46 -07:00
import logging
2017-08-13 22:08:40 -07:00
import subprocess
from time import time
2017-08-16 21:36:15 -07:00
from threading import Thread
2020-10-05 23:06:07 -07:00
from pysonic.database import LETTER_GROUPS
2017-08-13 22:08:40 -07:00
from pysonic.types import MUSIC_TYPES
2018-04-05 19:02:17 -07:00
from pysonic.apilib import formatresponse, ApiResponse
import cherrypy
2017-08-13 22:08:40 -07:00
2017-08-13 21:13:46 -07:00
logging = logging.getLogger("api")
2017-08-13 18:56:13 -07:00
2018-04-07 15:04:41 -07:00
class PysonicSubsonicApi(object):
2020-10-05 23:06:07 -07:00
def __init__(self, db, options):
2017-08-13 18:56:13 -07:00
self.db = db
2017-08-13 22:08:40 -07:00
self.options = options
2017-08-13 18:56:13 -07:00
2018-04-07 15:04:41 -07:00
@cherrypy.expose
@formatresponse
2018-04-07 15:04:41 -07:00
def index(self):
response = ApiResponse()
2020-10-05 23:06:07 -07:00
response.add_child("totals", **self.db.get_stats())
return response
2018-04-07 15:04:41 -07:00
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def ping_view(self, **kwargs):
# Called when the app hits the "test connection" server option
2017-08-20 14:54:13 -07:00
return ApiResponse()
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def getLicense_view(self, **kwargs):
# Called after ping.view
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("license",
valid="true",
email="admin@localhost",
licenseExpires="2100-01-01T00:00:00.000Z",
trialExpires="2100-01-01T01:01:00.000Z")
return response
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def getMusicFolders_view(self, **kwargs):
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("musicFolders")
2020-10-05 23:06:07 -07:00
for folder in self.db.get_libraries():
2017-08-20 14:54:13 -07:00
response.add_child("musicFolder", _parent="musicFolders", id=folder["id"], name=folder["name"])
return response
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def getIndexes_view(self, **kwargs):
# Get listing of top-level dir
2017-08-20 14:54:13 -07:00
response = ApiResponse()
2018-04-05 19:02:17 -07:00
# TODO real lastmodified date
# TODO deal with ignoredArticles
2017-08-20 14:54:13 -07:00
response.add_child("indexes", lastModified="1502310831000", ignoredArticles="The El La Los Las Le Les")
2020-10-05 23:06:07 -07:00
artists = self.db.get_artists(sortby="name", order="asc")
2017-08-13 18:56:13 -07:00
for letter in LETTER_GROUPS:
2017-08-20 14:54:13 -07:00
index = response.add_child("index", _parent="indexes", name=letter.upper())
2018-04-05 19:02:17 -07:00
for artist in artists:
2017-08-13 21:13:46 -07:00
if artist["name"][0].lower() in letter:
2018-04-05 19:02:17 -07:00
response.add_child("artist", _real_parent=index, id=artist["dir"], name=artist["name"])
2017-08-20 14:54:13 -07:00
return response
2017-08-13 18:56:13 -07:00
2017-08-13 23:54:37 -07:00
@cherrypy.expose
2017-08-19 23:01:47 -07:00
@formatresponse
2018-04-07 15:04:41 -07:00
def getAlbumList_view(self, type, size=250, offset=0, **kwargs):
2018-04-05 19:02:17 -07:00
qargs = {}
2017-08-13 23:54:37 -07:00
if type == "random":
2018-04-05 19:02:17 -07:00
qargs.update(sortby="random")
2017-08-13 23:54:37 -07:00
elif type == "alphabeticalByName":
2018-04-05 19:02:17 -07:00
qargs.update(sortby="name", order="asc")
elif type == "newest":
qargs.update(sortby="added", order="desc")
elif type == "recent":
qargs.update(sortby="played", order="desc")
elif type == "frequent":
qargs.update(sortby="plays", order="desc")
2018-04-05 19:02:17 -07:00
qargs.update(limit=(offset, size))
2020-10-05 23:06:07 -07:00
albums = self.db.get_albums(**qargs)
2017-08-13 23:54:37 -07:00
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("albumList")
2017-08-13 23:54:37 -07:00
2018-04-05 19:02:17 -07:00
for album in albums:
album_kw = dict(id=album["dir"],
parent=album["artistdir"],
isDir="true",
title=album["name"],
album=album["name"],
artist=album["artistname"],
2020-10-05 22:12:40 -07:00
coverArt=album["coverid"],
playCount=album["plays"],
2018-04-05 19:02:17 -07:00
#year=TODO
2020-10-05 22:12:40 -07:00
#created="2016-05-08T05:31:31.000Z"/>)
2017-08-19 23:01:47 -07:00
)
2017-08-20 14:54:13 -07:00
response.add_child("album", _parent="albumList", **album_kw)
2017-08-19 23:01:47 -07:00
return response
2017-08-13 23:54:37 -07:00
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def getMusicDirectory_view(self, id, **kwargs):
"""
2020-10-05 22:12:40 -07:00
List either and artist or album dir
2017-08-13 18:56:13 -07:00
"""
dir_id = int(id)
2020-10-05 23:06:07 -07:00
dirtype, dirinfo, entity = self.db.get_subsonic_musicdir(dirid=dir_id)
2017-08-13 18:56:13 -07:00
2017-08-20 14:54:13 -07:00
response = ApiResponse()
2017-08-13 18:56:13 -07:00
2020-10-05 22:12:40 -07:00
# artists just need this
response.add_child("directory",
name=entity['name'],
2020-10-05 23:24:11 -07:00
id=entity['dir'])
2020-10-05 22:12:40 -07:00
if dirtype == "album":
# albums can also have
# - parent (album dir id)
# - playcount
response.set_attrs(_path="directory",
parent=dirinfo["parent"],
playCount=entity["plays"])
#TODO Meeeeee
2018-04-05 19:02:17 -07:00
for childtype, child in entity["children"]:
2017-08-13 22:08:40 -07:00
# omit not dirs and media in browser
2018-04-05 19:02:17 -07:00
# if not item["isdir"] and item["type"] not in MUSIC_TYPES:
# continue
# item_meta = item['metadata']
moreargs = {}
if childtype == "album":
moreargs.update(name=child["name"],
isDir="true", # TODO song files in artist dir
2020-10-05 23:24:11 -07:00
parent=entity["dir"],
2018-04-05 19:02:17 -07:00
id=child["dir"])
if child["coverid"]:
moreargs.update(coverArt=child["coverid"])
# album=item["name"],
# title=item["name"], # TODO dupe?
# artist=artist["name"],
# coverArt=item["coverid"],
elif childtype == "song":
moreargs.update(name=child["title"],
artist=child["_artist"]["name"],
contentType=child["format"],
id=child["id"],
duration=child["length"],
isDir="false",
parent=entity["dir"],
2020-09-23 22:57:26 -07:00
track=child["track"],
2018-04-05 19:02:17 -07:00
# title=xxx
)
if entity["coverid"]:
moreargs.update(coverArt=entity["coverid"])
# duration="230" size="8409237" suffix="mp3" track="2" year="2005"/>
response.add_child("child", _parent="directory",
size="4096",
type="music",
**moreargs)
2017-08-13 18:56:13 -07:00
2018-04-05 19:02:17 -07:00
cherrypy.response.headers['Content-Type'] = 'text/xml; charset=utf-8'
2017-08-20 14:54:13 -07:00
return response
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-13 22:08:40 -07:00
def stream_view(self, id, maxBitRate="256", **kwargs):
maxBitRate = int(maxBitRate)
assert maxBitRate >= 32 and maxBitRate <= 320
2020-10-05 23:06:07 -07:00
song = self.db.get_songs(id=int(id))[0]
2020-10-05 23:30:01 -07:00
fpath = os.path.join(song["root"], song["file"])
2018-04-05 19:02:17 -07:00
media_bitrate = song.get("bitrate") / 1024 if song.get("bitrate") else 320
to_bitrate = min(maxBitRate,
self.options.max_bitrate,
media_bitrate)
2017-08-13 18:56:13 -07:00
cherrypy.response.headers['Content-Type'] = 'audio/mpeg'
2018-04-05 19:02:17 -07:00
#if "media_length" in meta:
# cherrypy.response.headers['X-Content-Duration'] = str(int(meta['media_length']))
2017-08-19 22:03:09 -07:00
cherrypy.response.headers['X-Content-Kbitrate'] = str(to_bitrate)
2018-04-05 19:02:17 -07:00
if (self.options.skip_transcode or (song.get("bitrate") and media_bitrate == to_bitrate)) \
and song["format"] == "audio/mpeg":
2017-08-13 22:08:40 -07:00
def content():
with open(fpath, "rb") as f:
while True:
data = f.read(16 * 1024)
if not data:
break
yield data
2017-08-19 22:03:09 -07:00
return content()
2017-08-13 22:08:40 -07:00
else:
2018-04-05 19:02:17 -07:00
# transcode_meta = "transcoded_{}_size".format(to_bitrate)
# if transcode_meta in meta:
# cherrypy.response.headers['Content-Length'] = str(int(meta[transcode_meta]))
2017-08-16 21:36:15 -07:00
transcode_args = ["ffmpeg", "-i", fpath, "-map", "0:0", "-b:a",
2017-08-19 22:03:09 -07:00
"{}k".format(to_bitrate),
2017-08-16 21:36:15 -07:00
"-v", "0", "-f", "mp3", "-"]
logging.info(' '.join(transcode_args))
proc = subprocess.Popen(transcode_args, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
def content(proc):
2017-08-19 22:03:09 -07:00
length = 0
2018-04-05 19:02:17 -07:00
# completed = False
2017-08-13 22:08:40 -07:00
start = time()
2017-08-16 21:36:15 -07:00
try:
while True:
data = proc.stdout.read(16 * 1024)
if not data:
2018-04-05 19:02:17 -07:00
# completed = True
2017-08-16 21:36:15 -07:00
break
yield data
2017-08-19 22:03:09 -07:00
length += len(data)
2017-08-16 21:36:15 -07:00
finally:
proc.poll()
2017-08-19 22:03:09 -07:00
if proc.returncode is None or proc.returncode == 0:
2017-08-16 21:36:15 -07:00
logging.warning("transcoded {} in {}s".format(id, int(time() - start)))
2018-04-05 19:02:17 -07:00
# if completed:
2020-10-05 23:06:07 -07:00
# self.db.report_transcode(id, to_bitrate, length)
2017-08-16 21:36:15 -07:00
else:
logging.error("transcode of {} exited with code {} after {}s".format(id, proc.returncode,
int(time() - start)))
def stopit(proc):
try:
proc.wait(timeout=90)
except subprocess.TimeoutExpired:
logging.warning("killing timed-out transcoder")
proc.kill()
proc.wait()
Thread(target=stopit, args=(proc, )).start()
2017-08-19 22:03:09 -07:00
return content(proc)
2017-08-13 18:56:13 -07:00
stream_view._cp_config = {'response.stream': True}
@cherrypy.expose
def getCoverArt_view(self, id, **kwargs):
2020-10-05 22:12:40 -07:00
"""
id is a string and if it's a number it's the album at for a...?? could be song or album either by id or directory id lol
it could also be:
pl-1234 - playlist
for now, if the first character isn't a number, we error
"""
if id.startswith("pl-"): # get art from first track in playlist
playlist_id = int(id[len("pl-"):])
2020-10-05 23:06:07 -07:00
songs = self.db.get_playlist_songs(playlist_id)
2020-10-05 22:12:40 -07:00
for song in songs:
if song["albumcoverid"]:
id = song["albumcoverid"]
break
else:
raise cherrypy.HTTPError(404, message=f"no art for any of the {len(songs)} tracks in playlist {playlist_id}")
elif id[0] not in "0123456789":
#TODO
print("TODO support getCoverArt id format", repr(id))
raise cherrypy.HTTPError(500, message=f"coverid format {repr(id)} not supported")
else:
id = int(id)
2020-10-05 23:06:07 -07:00
fpath = self.db.get_cover_path(id)
2017-08-13 21:13:46 -07:00
type2ct = {
'jpg': 'image/jpeg',
2017-08-20 14:54:13 -07:00
'png': 'image/png',
'gif': 'image/gif'
2017-08-13 21:13:46 -07:00
}
cherrypy.response.headers['Content-Type'] = type2ct[fpath[-3:]]
2017-08-13 18:56:13 -07:00
def content():
total = 0
with open(fpath, "rb") as f:
while True:
data = f.read(8192)
if not data:
break
total += len(data)
yield data
2020-10-05 23:06:07 -07:00
logging.info("sent {} bytes for {}".format(total, fpath))
2017-08-13 18:56:13 -07:00
return content()
getCoverArt_view._cp_config = {'response.stream': True}
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-13 18:56:13 -07:00
def getArtistInfo_view(self, id, includeNotPresent="true", **kwargs):
2020-10-05 23:06:07 -07:00
info = self.db.get_artist_info(id)
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("artistInfo")
response.set_attrs("artistInfo", **info)
return response
2017-08-13 18:56:13 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-20 15:57:45 -07:00
def getUser_view(self, username, **kwargs):
2020-10-05 23:06:07 -07:00
user = {} if self.options.disable_auth else self.db.get_user(cherrypy.request.login)
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("user",
username=user["username"],
email=user["email"],
scrobblingEnabled="false",
adminRole="true" if user["admin"] else "false",
settingsRole="false",
downloadRole="true",
uploadRole="false",
playlistRole="true",
coverArtRole="false",
commentRole="false",
podcastRole="false",
streamRole="true",
jukeboxRole="false",
shareRole="true",
videoConversionRole="false",
avatarLastChanged="2017-08-07T20:16:24.596Z",
folder=0)
return response
2017-08-15 21:40:38 -07:00
2017-08-15 21:41:02 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-15 21:41:02 -07:00
def star_view(self, id, **kwargs):
2020-10-05 23:06:07 -07:00
self.db.set_starred(cherrypy.request.login, int(id), starred=True)
2017-08-20 14:54:13 -07:00
return ApiResponse()
2017-08-15 21:41:02 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-15 21:41:02 -07:00
def unstar_view(self, id, **kwargs):
2020-10-05 23:06:07 -07:00
self.db.set_starred(cherrypy.request.login, int(id), starred=False)
2017-08-20 14:54:13 -07:00
return ApiResponse()
2017-08-15 22:16:48 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-15 22:16:48 -07:00
def getStarred_view(self, **kwargs):
2020-10-05 23:06:07 -07:00
children = self.db.get_starred(cherrypy.request.login)
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("starred")
2017-08-16 00:05:26 -07:00
for item in children:
# omit not dirs and media in browser
if not item["isdir"] and item["type"] not in MUSIC_TYPES:
continue
item_meta = item['metadata']
itemtype = "song" if item["type"] in MUSIC_TYPES else "album"
2017-08-20 15:57:45 -07:00
response.add_child(itemtype, _parent="starred", **self.render_node(item, item_meta, {}, {}))
2017-08-20 14:54:13 -07:00
return response
2017-08-15 22:16:48 -07:00
2017-08-16 00:05:26 -07:00
@cherrypy.expose
2017-08-20 14:54:13 -07:00
@formatresponse
2017-08-16 00:05:26 -07:00
def getRandomSongs_view(self, size=50, genre=None, fromYear=0, toYear=0, **kwargs):
2017-08-20 15:57:45 -07:00
"""
Get a playlist of random songs
:param genre: genre name to find songs under
:type genre: str
"""
2017-08-20 14:54:13 -07:00
response = ApiResponse()
response.add_child("randomSongs")
2020-10-05 23:06:07 -07:00
children = self.db.get_songs(limit=size, sortby="random")
2018-04-05 19:02:17 -07:00
for song in children:
moreargs = {}
if song["format"]:
moreargs.update(contentType=song["format"])
if song["albumcoverid"]:
moreargs.update(coverArt=song["albumcoverid"])
if song["length"]:
moreargs.update(duration=song["length"])
if song["track"]:
moreargs.update(track=song["track"])
if song["year"]:
moreargs.update(year=song["year"])
file_extension = song["file"].split(".")[-1]
response.add_child("song",
_parent="randomSongs",
title=song["title"],
album=song["albumname"],
artist=song["artistname"],
id=song["id"],
isDir="false",
parent=song["albumid"],
size=song["size"],
suffix=file_extension,
type="music",
**moreargs)
2017-08-20 15:57:45 -07:00
return response
@cherrypy.expose
@formatresponse
def getGenres_view(self, **kwargs):
response = ApiResponse()
response.add_child("genres")
2020-10-05 23:06:07 -07:00
for row in self.db.get_genres():
2018-04-05 19:02:17 -07:00
response.add_child("genre", _parent="genres", value=row["name"], songCount=420, albumCount=69)
2017-08-20 14:54:13 -07:00
return response
2017-08-20 15:57:45 -07:00
@cherrypy.expose
@formatresponse
def scrobble_view(self, id, submission, **kwargs):
"""
:param id: song id being played
:param submission: True if end of song reached. False on start of track.
"""
submission = True if submission == "true" else False
2018-04-05 19:02:17 -07:00
# TODO save played track stats and/or do last.fm bullshit
2017-08-20 15:57:45 -07:00
return ApiResponse()
2017-08-20 16:09:17 -07:00
@cherrypy.expose
@formatresponse
def search2_view(self, query, artistCount, albumCount, songCount, **kwargs):
response = ApiResponse()
response.add_child("searchResult2")
artistCount = int(artistCount)
albumCount = int(albumCount)
songCount = int(songCount)
query = query.replace("*", "") # TODO handle this
artists = 0
2020-10-05 23:06:07 -07:00
for item in self.db.get_artists(name_contains=query):
2020-10-05 23:24:11 -07:00
response.add_child("artist", _parent="searchResult2", id=item["dir"], name=item["name"])
2020-10-05 22:19:48 -07:00
artists += 1
if artists >= artistCount:
break
2017-08-20 16:09:17 -07:00
# TODO make this more efficient
albums = 0
2020-10-05 23:06:07 -07:00
for album in self.db.get_albums(name_contains=query):
2020-10-05 20:11:58 -07:00
response.add_child("album", _parent="searchResult2",
id=album["dir"],
parent=album["artistdir"],
isDir="true",
title=album["name"],
album=album["name"],
artist=album["artistname"],
2020-10-05 22:12:40 -07:00
coverArt=album["coverid"],
playCount=album["plays"],
2020-10-05 20:11:58 -07:00
#year=TODO
2020-10-05 22:12:40 -07:00
#created="2016-05-08T05:31:31.000Z"/>)
2020-10-05 20:11:58 -07:00
)
albums += 1
if albums >= albumCount:
break
2017-08-20 16:09:17 -07:00
# TODO make this more efficient
songs = 0
2020-10-05 23:06:07 -07:00
for song in self.db.get_songs(title_contains=query):
2020-10-05 20:11:58 -07:00
response.add_child("song", _parent="searchResult2",
id=song["id"],
parent=song["albumdir"],
isDir="false",
title=song["title"],
album=song["albumname"],
artist=song["artistname"],
track=song["track"],
year=song["year"],
genre=song["genrename"],
coverArt=song["albumcoverid"],
size=song["size"],
contentType=song["format"],
duration=song["length"],
bitRate=song["bitrate"],
path=song["file"],
2020-10-05 22:12:40 -07:00
playCount=song["plays"],
2020-10-05 20:11:58 -07:00
albumId=song["albumid"],
type="music"
# suffix="mp3"
# created="2012-09-17T22:35:19.000Z"
)
songs += 1
if songs > songCount:
break
2017-08-20 16:09:17 -07:00
return response
2017-08-20 16:12:11 -07:00
@cherrypy.expose
@formatresponse
def setRating_view(self, id, rating):
# rating is 1-5
pass
2018-04-05 19:02:17 -07:00
@cherrypy.expose
def savePlayQueue_view(self, id, current, position, **kwargs):
2020-10-05 22:12:40 -07:00
print("TODO save playqueue with items {} current {} position {}".format(id, repr(current), repr(position)))
current = int(current)
2020-10-05 23:06:07 -07:00
song = self.db.get_songs(id=current)[0]
self.db.update_album_played(song['albumid'], time())
self.db.increment_album_plays(song['albumid'])
2020-10-05 22:12:40 -07:00
if int(position) == 0:
2020-10-05 23:06:07 -07:00
self.db.increment_track_plays(current)
2018-04-05 19:02:17 -07:00
# TODO save playlist with items ['378', '386', '384', '380', '383'] current 383 position 4471
# id entries are strings!
@cherrypy.expose
@formatresponse
def createPlaylist_view(self, name, songId, **kwargs):
if type(songId) != list:
songId = [songId]
2020-10-05 23:06:07 -07:00
user = self.db.get_user(cherrypy.request.login)
self.db.add_playlist(user["id"], name, songId)
2018-04-05 19:02:17 -07:00
return ApiResponse()
#TODO the response should be the new playlist, check the cap
@cherrypy.expose
@formatresponse
def getPlaylists_view(self, **kwargs):
2020-10-05 23:06:07 -07:00
user = self.db.get_user(cherrypy.request.login)
2018-04-05 19:02:17 -07:00
response = ApiResponse()
response.add_child("playlists")
2020-10-05 23:06:07 -07:00
for playlist in self.db.get_playlists(user["id"]):
2018-04-05 19:02:17 -07:00
response.add_child("playlist",
_parent="playlists",
id=playlist["id"],
name=playlist["name"],
owner=user["username"],
public=playlist["public"],
songCount=69,
duration=420,
# changed="2018-04-05T23:23:38.263Z"
# created="2018-04-05T23:23:38.252Z"
2020-10-05 22:12:40 -07:00
coverArt="pl-{}".format(playlist["id"])
2018-04-05 19:02:17 -07:00
)
return response
@cherrypy.expose
@formatresponse
def getPlaylist_view(self, id, **kwargs):
2020-10-05 23:06:07 -07:00
id = int(id)
user = self.db.get_user(cherrypy.request.login)
plinfo = self.db.get_playlist(id)
songs = self.db.get_playlist_songs(id)
2018-04-05 19:02:17 -07:00
response = ApiResponse()
response.add_child("playlist",
id=plinfo["id"],
name=plinfo["name"], # TODO this element should match getPlaylists_view
owner=user["username"], # TODO translate id to name
public=plinfo["public"],
songCount=69,
duration=420)
for song in songs:
response.add_child("entry",
_parent="playlist",
id=song["id"],
parent=song["albumid"], # albumid seems wrong? should be dir parent?
isDir="false",
title=song["title"],
album=song["albumname"],
artist=song["artistname"],
track=song["track"],
year=song["year"],
genre=song["genrename"],
coverArt=song["albumcoverid"],
size=song["size"],
contentType=song["format"],
# suffix="mp3"
duration=song["length"],
2020-10-05 22:12:40 -07:00
bitRate=song["bitrate"] / 1024 if song["bitrate"] else None, #TODO macro for this sort of logic
2018-04-05 19:02:17 -07:00
path=song["file"],
2020-10-05 22:12:40 -07:00
playCount=song["plays"],
2018-04-05 19:02:17 -07:00
# created="2015-06-09T15:26:01.000Z"
albumId=song["albumid"],
artistId=song["artistid"],
type="music")
return response
@cherrypy.expose
@formatresponse
def updatePlaylist_view(self, playlistId, songIndexToRemove=None, songIdToAdd=None, **kwargs):
2020-10-05 23:06:07 -07:00
playlistId = int(playlistId)
user = self.db.get_user(cherrypy.request.login)
plinfo = self.db.get_playlist(playlistId)
2018-04-05 19:02:17 -07:00
assert plinfo["ownerid"] == user["id"]
if songIndexToRemove:
2020-10-05 23:06:07 -07:00
self.db.remove_index_from_playlist(playlistId, songIndexToRemove)
2018-04-05 19:02:17 -07:00
elif songIdToAdd:
2020-10-05 23:06:07 -07:00
self.db.add_to_playlist(playlistId, songIdToAdd)
2018-04-05 19:02:17 -07:00
#TODO there are more modification methods
return ApiResponse()
@cherrypy.expose
@formatresponse
def deletePlaylist_view(self, id, **kwargs):
2020-10-05 23:06:07 -07:00
user = self.db.get_user(cherrypy.request.login)
plinfo = self.db.get_playlist(int(id))
2018-04-05 19:02:17 -07:00
assert plinfo["ownerid"] == user["id"]
2020-10-05 23:06:07 -07:00
self.db.delete_playlist(plinfo["id"])
2018-04-05 19:02:17 -07:00
return ApiResponse()