major changes:

- resize images to 150x150
- convert them to webp
- use $set to update database
- remove comments in py code
- a whole lot more
This commit is contained in:
geoffrey45
2021-12-19 02:31:17 +03:00
parent 457180ecaf
commit 831b8e7ee2
11 changed files with 65 additions and 79 deletions
+20 -47
View File
@@ -57,22 +57,31 @@ def search_by_title():
songs_by_artists = all_songs_instance.find_songs_by_artist(query)
all_songs.append(convert_to_json(songs_by_artists))
# songs = remove_duplicates(all_songs)
return {'songs': all_songs}
@bp.route('/populate')
def populate():
sub_dirs, files = run_fast_scandir(home_dir, [".flac", ".mp3"])
'''
Populate the database with all songs in the music directory
checks if the song is in the database, if not, it adds it
also checks if the album art exists in the image path, if not tries to
extract it.
'''
files = run_fast_scandir(home_dir, [".flac", ".mp3"])[1]
bar = Bar('Processing', max=len(files))
for file in files:
file_in_db_obj = all_songs_instance.find_song_by_path(file)
song_obj = convert_one_to_json(file_in_db_obj)
try:
image = song_obj['image']
if not os.path.exists(os.path.join(app_dir, 'images', 'thumbnails', image)):
extract_thumb(file)
except:
image = None
@@ -82,48 +91,10 @@ def populate():
except MutagenError:
pass
if image is not None and not os.path.exists(image):
extract_thumb(file)
bar.next()
bar.finish()
# dirs = []
# files = []
# for dir in sub_dirs:
# files_in_dir = run_fast_scandir(dir, [".flac", ".mp3"])[1]
# if len(files_in_dir) != 0:
# dir_content = os.scandir(dir)
# for entry in dir_content:
# dirs = []
# files = []
# if entry.is_dir() and not entry.name.startswith('.'):
# print(dir)
# files_in_dir = run_fast_scandir(entry.path, [".flac", ".mp3"])[1]
# if len(files_in_dir) != 0:
# dir_data = {
# "name": entry.name,
# "count": len(files_in_dir),
# "path": entry.path.replace(home_dir, "")
# }
# dirs.append(dir_data)
# if entry.is_file():
# if isValidFile(entry.name) == True:
# files.append(entry.path)
# print(dirs)
# return {"info": ''}
return {'msg': 'updated everything'}
@bp.route('/file/<file_id>')
@@ -151,7 +122,7 @@ def get_folder_artists():
this_artists = song['artists'].split(', ')
for artist in this_artists:
if artist not in artists:
artists.append(artist)
@@ -277,7 +248,8 @@ def getFolderTree():
last_id = request.args.get('last_id')
if req_dir is not None:
requested_dir = home_dir + req_dir
requested_dir = os.path.join(home_dir, req_dir)
print(requested_dir)
else:
requested_dir = home_dir
@@ -289,7 +261,7 @@ def getFolderTree():
for entry in dir_content:
if entry.is_dir() and not entry.name.startswith('.'):
files_in_dir = run_fast_scandir(entry.path, [".flac", ".mp3"])[1]
if len(files_in_dir) != 0:
dir = {
"name": entry.name,
@@ -301,7 +273,8 @@ def getFolderTree():
if entry.is_file():
if isValidFile(entry.name) == True:
songs_array = all_songs_instance.find_songs_by_folder(req_dir, last_id)
songs_array = all_songs_instance.find_songs_by_folder(
req_dir, last_id)
songs = convert_to_json(songs_array)
for song in songs:
song['artists'] = song['artists'].split(', ')
@@ -309,7 +282,7 @@ def getFolderTree():
files = songs
for file in files:
del file['filepath']
file['filepath'] = file['filepath'].replace(home_dir, '')
dir_content.close()
end = time.time()
+20 -10
View File
@@ -20,8 +20,8 @@ all_songs_instance = AllSongs()
music_dir = os.environ.get("music_dir")
music_dirs = os.environ.get("music_dirs")
home_dir = os.path.expanduser('~')
app_dir = home_dir + '/.shit'
home_dir = os.path.expanduser('~') + "/"
app_dir = home_dir + '/.musicx'
PORT = os.environ.get("PORT")
@@ -47,10 +47,11 @@ def run_fast_scandir(dir, ext):
def extract_thumb(path):
img_path = app_dir + "/images/thumbnails/" + path.split('/')[-1] + '.jpg'
webp_path = path.split('/')[-1] + '.webp'
img_path = app_dir + "/images/thumbnails/" + webp_path
if os.path.exists(img_path):
return path.split('/')[-1] + '.jpg'
return webp_path
if path.endswith('.flac'):
audio = FLAC(path)
@@ -65,18 +66,27 @@ def extract_thumb(path):
except IndexError:
album_art = None
if album_art is not None:
if album_art is None:
return "null.webp"
else:
img = Image.open(BytesIO(album_art))
try:
img.save(img_path, 'JPEG')
small_img = img.resize((150, 150), Image.ANTIALIAS)
small_img.save(img_path, format="webp")
except OSError:
try:
img.convert('RGB'.save(img_path, 'JPEG'))
png = img.convert('RGB')
small_img = png.resize((150, 150), Image.ANTIALIAS)
small_img.save(img_path, format="webp")
print('{} :: was png'.format(
img_path
))
except:
img_path = None
return path.split('/')[-1] + '.jpg'
return webp_path
def getTags(full_path):
@@ -253,7 +263,7 @@ def getFolderContents(filepath, folder):
def create_config_dir():
home_dir = os.path.expanduser('~')
config_folder = home_dir + "/.shit"
config_folder = home_dir + app_dir
dirs = ["", "/images", "/images/artists", "/images/thumbnails"]
+3 -2
View File
@@ -50,8 +50,9 @@ class AllSongs(Mongo):
return self.collection.find_one({'_id': ObjectId(file_id)})
def insert_song(self, song_obj):
self.collection.update(
{'filepath': song_obj['filepath']}, song_obj, upsert=True)
self.collection.update_one(
{'filepath': song_obj['filepath']}, { "$set": song_obj}, upsert=True)
# self.collection.insert_one(song_obj)
def find_song_by_title(self, query):
self.collection.create_index([('title', pymongo.TEXT)])