fix suffix field
This commit is contained in:
parent
0d2f9a9587
commit
5e0e541cf9
|
@ -4,13 +4,19 @@ import subprocess
|
||||||
from time import time
|
from time import time
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from pysonic.database import LETTER_GROUPS
|
from pysonic.database import LETTER_GROUPS
|
||||||
from pysonic.types import MUSIC_TYPES
|
from pysonic.types import MUSIC_TYPES, TYPE_TO_EXTENSION
|
||||||
from pysonic.apilib import formatresponse, ApiResponse
|
from pysonic.apilib import formatresponse, ApiResponse
|
||||||
import cherrypy
|
import cherrypy
|
||||||
|
|
||||||
logging = logging.getLogger("api")
|
logging = logging.getLogger("api")
|
||||||
|
|
||||||
|
|
||||||
|
def extension(mime):
|
||||||
|
r = TYPE_TO_EXTENSION.get(mime)
|
||||||
|
print(f"getting extension for {mime}: {r}")
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
class PysonicSubsonicApi(object):
|
class PysonicSubsonicApi(object):
|
||||||
def __init__(self, db, options):
|
def __init__(self, db, options):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
@ -127,7 +133,7 @@ class PysonicSubsonicApi(object):
|
||||||
response.set_attrs(_path="directory",
|
response.set_attrs(_path="directory",
|
||||||
parent=dirinfo["parent"],
|
parent=dirinfo["parent"],
|
||||||
playCount=entity["plays"])
|
playCount=entity["plays"])
|
||||||
#TODO Meeeeee
|
#TODO refactor meeeeee
|
||||||
for childtype, child in entity["children"]:
|
for childtype, child in entity["children"]:
|
||||||
# omit not dirs and media in browser
|
# omit not dirs and media in browser
|
||||||
# if not item["isdir"] and item["type"] not in MUSIC_TYPES:
|
# if not item["isdir"] and item["type"] not in MUSIC_TYPES:
|
||||||
|
@ -146,7 +152,10 @@ class PysonicSubsonicApi(object):
|
||||||
# artist=artist["name"],
|
# artist=artist["name"],
|
||||||
# coverArt=item["coverid"],
|
# coverArt=item["coverid"],
|
||||||
elif childtype == "song":
|
elif childtype == "song":
|
||||||
moreargs.update(name=child["title"],
|
moreargs.update(title=child["title"],
|
||||||
|
albumId=entity["dir"],
|
||||||
|
album=entity["name"],
|
||||||
|
artistId=child["_artist"]["dir"],
|
||||||
artist=child["_artist"]["name"],
|
artist=child["_artist"]["name"],
|
||||||
contentType=child["format"],
|
contentType=child["format"],
|
||||||
id=child["id"],
|
id=child["id"],
|
||||||
|
@ -154,11 +163,20 @@ class PysonicSubsonicApi(object):
|
||||||
isDir="false",
|
isDir="false",
|
||||||
parent=entity["dir"],
|
parent=entity["dir"],
|
||||||
track=child["track"],
|
track=child["track"],
|
||||||
# title=xxx
|
playCount=child["plays"],
|
||||||
|
#TODO suffix can be null/omitted, which causes the client to cache files wrong, while
|
||||||
|
# this isn't ideal, fixing it properly would require significant changes to the scanner.
|
||||||
|
suffix=extension(child["format"]),
|
||||||
|
path=child["file"],
|
||||||
|
# bitRate
|
||||||
|
# discNumber
|
||||||
|
# created=
|
||||||
|
# year=1999
|
||||||
|
# genre="Alternative & Punk"
|
||||||
)
|
)
|
||||||
if entity["coverid"]:
|
if entity["coverid"]:
|
||||||
moreargs.update(coverArt=entity["coverid"])
|
moreargs.update(coverArt=entity["coverid"])
|
||||||
# duration="230" size="8409237" suffix="mp3" track="2" year="2005"/>
|
|
||||||
response.add_child("child", _parent="directory",
|
response.add_child("child", _parent="directory",
|
||||||
size="4096",
|
size="4096",
|
||||||
type="music",
|
type="music",
|
||||||
|
@ -368,8 +386,6 @@ class PysonicSubsonicApi(object):
|
||||||
if song["year"]:
|
if song["year"]:
|
||||||
moreargs.update(year=song["year"])
|
moreargs.update(year=song["year"])
|
||||||
|
|
||||||
file_extension = song["file"].split(".")[-1]
|
|
||||||
|
|
||||||
response.add_child("song",
|
response.add_child("song",
|
||||||
_parent="randomSongs",
|
_parent="randomSongs",
|
||||||
title=song["title"],
|
title=song["title"],
|
||||||
|
@ -379,7 +395,7 @@ class PysonicSubsonicApi(object):
|
||||||
isDir="false",
|
isDir="false",
|
||||||
parent=song["albumid"],
|
parent=song["albumid"],
|
||||||
size=song["size"],
|
size=song["size"],
|
||||||
suffix=file_extension,
|
suffix=extension(song["format"]),
|
||||||
type="music",
|
type="music",
|
||||||
**moreargs)
|
**moreargs)
|
||||||
return response
|
return response
|
||||||
|
@ -463,8 +479,8 @@ class PysonicSubsonicApi(object):
|
||||||
path=song["file"],
|
path=song["file"],
|
||||||
playCount=song["plays"],
|
playCount=song["plays"],
|
||||||
albumId=song["albumid"],
|
albumId=song["albumid"],
|
||||||
type="music"
|
type="music",
|
||||||
# suffix="mp3"
|
suffix=extension(song["format"]),
|
||||||
# created="2012-09-17T22:35:19.000Z"
|
# created="2012-09-17T22:35:19.000Z"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -555,7 +571,7 @@ class PysonicSubsonicApi(object):
|
||||||
coverArt=song["albumcoverid"],
|
coverArt=song["albumcoverid"],
|
||||||
size=song["size"],
|
size=song["size"],
|
||||||
contentType=song["format"],
|
contentType=song["format"],
|
||||||
# suffix="mp3"
|
suffix=extension(song["format"]),
|
||||||
duration=song["length"],
|
duration=song["length"],
|
||||||
bitRate=song["bitrate"] / 1024 if song["bitrate"] else None, #TODO macro for this sort of logic
|
bitRate=song["bitrate"] / 1024 if song["bitrate"] else None, #TODO macro for this sort of logic
|
||||||
path=song["file"],
|
path=song["file"],
|
||||||
|
|
|
@ -5,7 +5,8 @@ from contextlib import closing
|
||||||
import mimetypes
|
import mimetypes
|
||||||
from time import time
|
from time import time
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from pysonic.types import KNOWN_MIMES, MUSIC_TYPES, MPX_TYPES, FLAC_TYPES, WAV_TYPES, MUSIC_EXTENSIONS, IMAGE_EXTENSIONS, IMAGE_TYPES
|
from pysonic.types import MUSIC_TYPES, MPX_TYPES, FLAC_TYPES, MUSIC_EXTENSIONS, IMAGE_EXTENSIONS, \
|
||||||
|
TYPE_TO_EXTENSION, UNKNOWN_MIME
|
||||||
from mutagen.id3 import ID3
|
from mutagen.id3 import ID3
|
||||||
from mutagen import MutagenError
|
from mutagen import MutagenError
|
||||||
from mutagen.id3._util import ID3NoHeaderError
|
from mutagen.id3._util import ID3NoHeaderError
|
||||||
|
@ -17,6 +18,11 @@ logging = logging.getLogger("scanner")
|
||||||
RE_NUMBERS = re.compile(r'^([0-9]+)')
|
RE_NUMBERS = re.compile(r'^([0-9]+)')
|
||||||
|
|
||||||
|
|
||||||
|
def guess_format(fname):
|
||||||
|
ext = fname.split(".")[-1].lower()
|
||||||
|
return TYPE_TO_EXTENSION.get(ext, UNKNOWN_MIME)
|
||||||
|
|
||||||
|
|
||||||
class PysonicFilesystemScanner(object):
|
class PysonicFilesystemScanner(object):
|
||||||
def __init__(self, db):
|
def __init__(self, db):
|
||||||
self.db = db
|
self.db = db
|
||||||
|
@ -147,13 +153,14 @@ class PysonicFilesystemScanner(object):
|
||||||
if not cursor.fetchall():
|
if not cursor.fetchall():
|
||||||
# We leave most fields blank now and return later
|
# We leave most fields blank now and return later
|
||||||
# TODO probably not here but track file sizes and mark them for rescan on change
|
# TODO probably not here but track file sizes and mark them for rescan on change
|
||||||
cursor.execute("INSERT INTO songs (library, albumid, file, size, title) "
|
cursor.execute("INSERT INTO songs (library, albumid, file, size, title, format) "
|
||||||
"VALUES (?, ?, ?, ?, ?)",
|
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
(pid,
|
(pid,
|
||||||
album_id,
|
album_id,
|
||||||
fpath,
|
fpath,
|
||||||
os.stat(os.path.join(root_dir, fpath)).st_size,
|
os.stat(os.path.join(root_dir, fpath)).st_size,
|
||||||
fname, ))
|
fname,
|
||||||
|
guess_format(fpath)))
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
|
@ -14,3 +14,22 @@ IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif"]
|
||||||
IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif"]
|
IMAGE_EXTENSIONS = ["jpg", "jpeg", "png", "gif"]
|
||||||
|
|
||||||
MUSIC_EXTENSIONS = ["mp3", "flac", "wav"]
|
MUSIC_EXTENSIONS = ["mp3", "flac", "wav"]
|
||||||
|
|
||||||
|
TYPE_TO_EXTENSION = {
|
||||||
|
"audio/mpeg": "mp3",
|
||||||
|
"audio/flac": "flac",
|
||||||
|
"audio/x-wav": "wav",
|
||||||
|
"image/jpeg": "jpg",
|
||||||
|
"image/png": "png",
|
||||||
|
}
|
||||||
|
|
||||||
|
EXTENSION_TO_TYPE = {
|
||||||
|
"mp3": "audio/mpeg",
|
||||||
|
"flac": "audio/flac",
|
||||||
|
"wav": "audio/x-wav",
|
||||||
|
"jpg": "image/jpeg",
|
||||||
|
"png": "image/png",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
UNKNOWN_MIME = None
|
||||||
|
|
Loading…
Reference in New Issue