Speed updatabase generation. Generate and use a certificate by default.
This commit is contained in:
+2
-68
@@ -1,15 +1,12 @@
|
||||
#
|
||||
#
|
||||
# Bragi - A Mumble music bot
|
||||
# Forked from botamusique by azlux (https://github.com/azlux/botamusque)
|
||||
#
|
||||
|
||||
import os
|
||||
import re
|
||||
from io import BytesIO
|
||||
import base64
|
||||
import hashlib
|
||||
import mutagen
|
||||
from PIL import Image
|
||||
|
||||
import util
|
||||
import variables as var
|
||||
@@ -23,7 +20,6 @@ type : file
|
||||
title
|
||||
artist
|
||||
duration
|
||||
thumbnail
|
||||
user
|
||||
'''
|
||||
|
||||
@@ -52,7 +48,6 @@ class FileItem(BaseItem):
|
||||
self.path = path
|
||||
self.title = ""
|
||||
self.artist = ""
|
||||
self.thumbnail = None
|
||||
self.id = hashlib.md5(path.encode()).hexdigest()
|
||||
if os.path.exists(self.uri()):
|
||||
self._get_info_from_tag()
|
||||
@@ -62,7 +57,6 @@ class FileItem(BaseItem):
|
||||
else:
|
||||
super().__init__(from_dict)
|
||||
self.artist = from_dict['artist']
|
||||
self.thumbnail = from_dict['thumbnail']
|
||||
try:
|
||||
self.validate()
|
||||
except ValidationFailedError:
|
||||
@@ -95,112 +89,58 @@ class FileItem(BaseItem):
|
||||
assert path is not None and file_name is not None
|
||||
|
||||
try:
|
||||
im = None
|
||||
path_thumbnail = os.path.join(path, file_name + ".jpg")
|
||||
|
||||
if os.path.isfile(path_thumbnail):
|
||||
im = Image.open(path_thumbnail)
|
||||
else:
|
||||
path_thumbnail = os.path.join(path, "cover.jpg")
|
||||
if os.path.isfile(path_thumbnail):
|
||||
im = Image.open(path_thumbnail)
|
||||
|
||||
if ext == ".mp3":
|
||||
# title: TIT2
|
||||
# artist: TPE1, TPE2
|
||||
# album: TALB
|
||||
# cover artwork: APIC:
|
||||
tags = mutagen.File(self.uri())
|
||||
if 'TIT2' in tags:
|
||||
self.title = tags['TIT2'].text[0]
|
||||
if 'TPE1' in tags: # artist
|
||||
self.artist = tags['TPE1'].text[0]
|
||||
|
||||
if im is None:
|
||||
if "APIC:" in tags:
|
||||
im = Image.open(BytesIO(tags["APIC:"].data))
|
||||
|
||||
elif ext == ".m4a" or ext == ".m4b" or ext == ".mp4" or ext == ".m4p":
|
||||
# title: ©nam (\xa9nam)
|
||||
# artist: ©ART
|
||||
# album: ©alb
|
||||
# cover artwork: covr
|
||||
tags = mutagen.File(self.uri())
|
||||
if '©nam' in tags:
|
||||
self.title = tags['©nam'][0]
|
||||
if '©ART' in tags: # artist
|
||||
self.artist = tags['©ART'][0]
|
||||
|
||||
if im is None:
|
||||
if "covr" in tags:
|
||||
im = Image.open(BytesIO(tags["covr"][0]))
|
||||
|
||||
elif ext == ".opus":
|
||||
# title: 'title'
|
||||
# artist: 'artist'
|
||||
# album: 'album'
|
||||
# cover artwork: 'metadata_block_picture', and then:
|
||||
## |
|
||||
## |
|
||||
## v
|
||||
## Decode string as base64 binary
|
||||
## |
|
||||
## v
|
||||
## Open that binary as a mutagen.flac.Picture
|
||||
## |
|
||||
## v
|
||||
## Extract binary image data
|
||||
tags = mutagen.File(self.uri())
|
||||
if 'title' in tags:
|
||||
self.title = tags['title'][0]
|
||||
if 'artist' in tags:
|
||||
self.artist = tags['artist'][0]
|
||||
|
||||
if im is None:
|
||||
if 'metadata_block_picture' in tags:
|
||||
pic_as_base64 = tags['metadata_block_picture'][0]
|
||||
as_flac_picture = mutagen.flac.Picture(base64.b64decode(pic_as_base64))
|
||||
im = Image.open(BytesIO(as_flac_picture.data))
|
||||
|
||||
elif ext == ".flac":
|
||||
# title: 'title'
|
||||
# artist: 'artist'
|
||||
# album: 'album'
|
||||
# cover artwork: tags.pictures
|
||||
tags = mutagen.File(self.uri())
|
||||
if 'title' in tags:
|
||||
self.title = tags['title'][0]
|
||||
if 'artist' in tags:
|
||||
self.artist = tags['artist'][0]
|
||||
|
||||
if im is None:
|
||||
for flac_picture in tags.pictures:
|
||||
if flac_picture.type == 3:
|
||||
im = Image.open(BytesIO(flac_picture.data))
|
||||
|
||||
if im:
|
||||
self.thumbnail = self._prepare_thumbnail(im)
|
||||
except:
|
||||
pass
|
||||
|
||||
if not self.title:
|
||||
self.title = file_name
|
||||
|
||||
@staticmethod
|
||||
def _prepare_thumbnail(im):
|
||||
im.thumbnail((100, 100), Image.LANCZOS)
|
||||
buffer = BytesIO()
|
||||
im = im.convert('RGB')
|
||||
im.save(buffer, format="JPEG")
|
||||
return base64.b64encode(buffer.getvalue()).decode('utf-8')
|
||||
|
||||
def to_dict(self):
|
||||
dict = super().to_dict()
|
||||
dict['type'] = 'file'
|
||||
dict['path'] = self.path
|
||||
dict['title'] = self.title
|
||||
dict['artist'] = self.artist
|
||||
dict['thumbnail'] = self.thumbnail
|
||||
return dict
|
||||
|
||||
def format_debug_string(self):
|
||||
@@ -217,13 +157,7 @@ class FileItem(BaseItem):
|
||||
)
|
||||
|
||||
def format_current_playing(self, user):
|
||||
display = tr("now_playing", item=self.format_song_string(user))
|
||||
if self.thumbnail:
|
||||
thumbnail_html = '<img width="80" src="data:image/jpge;base64,' + \
|
||||
self.thumbnail + '"/>'
|
||||
display += "<br />" + thumbnail_html
|
||||
|
||||
return display
|
||||
return tr("now_playing", item=self.format_song_string(user))
|
||||
|
||||
def format_title(self):
|
||||
title = self.title if self.title else self.path
|
||||
|
||||
Reference in New Issue
Block a user