mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
major refactoring
- move instances to new file - import functions as modules - add docstrings to helper functions - add threaded populate() function - remove unused functions and files - add typing info to helper functions - other unremembered changes to the client
This commit is contained in:
+36
-148
@@ -1,60 +1,25 @@
|
||||
from app.models import Artists
|
||||
|
||||
from app.helpers import (
|
||||
all_songs_instance,
|
||||
getTags,
|
||||
remove_duplicates,
|
||||
save_image,
|
||||
create_config_dir,
|
||||
extract_thumb,
|
||||
run_fast_scandir,
|
||||
convert_one_to_json,
|
||||
convert_to_json,
|
||||
home_dir, app_dir,
|
||||
)
|
||||
|
||||
from app import cache
|
||||
|
||||
import os
|
||||
import requests
|
||||
import urllib
|
||||
|
||||
from progress.bar import Bar
|
||||
from mutagen.flac import MutagenError
|
||||
|
||||
from flask import Blueprint, request, send_from_directory
|
||||
|
||||
from flask import Blueprint, request
|
||||
from app import functions, instances, helpers, cache
|
||||
|
||||
bp = Blueprint('api', __name__, url_prefix='')
|
||||
|
||||
artist_instance = Artists()
|
||||
img_path = "http://127.0.0.1:8900/images/thumbnails/"
|
||||
|
||||
|
||||
all_the_f_music = []
|
||||
home_dir = helpers.home_dir
|
||||
|
||||
|
||||
def getAllSongs():
|
||||
all_the_f_music.clear()
|
||||
all_the_f_music.extend(all_songs_instance.get_all_songs())
|
||||
all_the_f_music = helpers.getAllSongs()
|
||||
|
||||
def initialize() -> None:
|
||||
helpers.create_config_dir()
|
||||
helpers.check_for_new_songs()
|
||||
|
||||
def main_whatever():
|
||||
create_config_dir()
|
||||
# populate()
|
||||
getAllSongs()
|
||||
|
||||
initialize()
|
||||
|
||||
@bp.route('/')
|
||||
def adutsfsd():
|
||||
for song in all_the_f_music:
|
||||
print(os.path.join(home_dir, song['filepath']))
|
||||
os.chmod(os.path.join(home_dir, song['filepath']), 0o755)
|
||||
|
||||
return "Done"
|
||||
|
||||
|
||||
main_whatever()
|
||||
return "^ _ ^"
|
||||
|
||||
|
||||
@bp.route('/search')
|
||||
@@ -67,9 +32,9 @@ def search_by_title():
|
||||
albums = []
|
||||
artists = []
|
||||
|
||||
s = all_songs_instance.find_song_by_title(query)
|
||||
al = all_songs_instance.search_songs_by_album(query)
|
||||
ar = all_songs_instance.search_songs_by_artist(query)
|
||||
s = instances.songs_instance.find_song_by_title(query)
|
||||
al = instances.songs_instance.search_songs_by_album(query)
|
||||
ar = instances.songs_instance.search_songs_by_artist(query)
|
||||
|
||||
for song in al:
|
||||
album_obj = {
|
||||
@@ -82,7 +47,7 @@ def search_by_title():
|
||||
|
||||
for album in albums:
|
||||
# try:
|
||||
# image = convert_one_to_json(all_songs_instance.get_song_by_album(album['name'], album['artists']))['image']
|
||||
# image = convert_one_to_json(instances.songs_instance.get_song_by_album(album['name'], album['artists']))['image']
|
||||
# except:
|
||||
# image: None
|
||||
|
||||
@@ -101,52 +66,21 @@ def search_by_title():
|
||||
if artist_obj not in artists:
|
||||
artists.append(artist_obj)
|
||||
|
||||
return {'songs': remove_duplicates(s), 'albums': albums, 'artists': artists}
|
||||
return {'songs': helpers.remove_duplicates(s), 'albums': albums, 'artists': artists}
|
||||
|
||||
|
||||
@bp.route('/populate')
|
||||
def populate():
|
||||
'''
|
||||
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)
|
||||
|
||||
try:
|
||||
image = file_in_db_obj['image']
|
||||
|
||||
if not os.path.exists(os.path.join(app_dir, 'images', 'thumbnails', image)):
|
||||
extract_thumb(file)
|
||||
except:
|
||||
image = None
|
||||
|
||||
if image is None:
|
||||
try:
|
||||
getTags(file)
|
||||
except MutagenError:
|
||||
pass
|
||||
|
||||
bar.next()
|
||||
|
||||
bar.finish()
|
||||
|
||||
return {'msg': 'updated everything'}
|
||||
def x():
|
||||
functions.populate()
|
||||
return "🎸"
|
||||
|
||||
|
||||
@bp.route("/folder/artists")
|
||||
def get_folder_artists():
|
||||
dir = request.args.get('dir')
|
||||
|
||||
songs = all_songs_instance.find_songs_by_folder(dir)
|
||||
without_duplicates = remove_duplicates(songs)
|
||||
songs = instances.songs_instance.find_songs_by_folder(dir)
|
||||
without_duplicates = helpers.remove_duplicates(songs)
|
||||
|
||||
artists = []
|
||||
|
||||
@@ -161,7 +95,7 @@ def get_folder_artists():
|
||||
final_artists = []
|
||||
|
||||
for artist in artists[:15]:
|
||||
artist_obj = artist_instance.find_artists_by_name(artist)
|
||||
artist_obj = instances.artist_instance.find_artists_by_name(artist)
|
||||
|
||||
if artist_obj != []:
|
||||
final_artists.append(artist_obj)
|
||||
@@ -171,51 +105,7 @@ def get_folder_artists():
|
||||
|
||||
@bp.route("/populate/images")
|
||||
def populate_images():
|
||||
all_songs = all_songs_instance.get_all_songs()
|
||||
|
||||
artists = []
|
||||
|
||||
for song in all_songs:
|
||||
this_artists = song['artists'].split(', ')
|
||||
|
||||
for artist in this_artists:
|
||||
if artist not in artists:
|
||||
artists.append(artist)
|
||||
|
||||
bar = Bar('Processing images', max=len(artists))
|
||||
for artist in artists:
|
||||
file_path = app_dir + '/images/artists/' + artist + '.jpg'
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
url = 'https://api.deezer.com/search/artist?q={}'.format(artist)
|
||||
response = requests.get(url)
|
||||
data = response.json()
|
||||
|
||||
try:
|
||||
image_path = data['data'][0]['picture_xl']
|
||||
except:
|
||||
image_path = None
|
||||
|
||||
if image_path is not None:
|
||||
try:
|
||||
save_image(image_path, file_path)
|
||||
artist_obj = {
|
||||
'name': artist
|
||||
}
|
||||
|
||||
artist_instance.insert_artist(artist_obj)
|
||||
except:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
|
||||
bar.next()
|
||||
|
||||
bar.finish()
|
||||
|
||||
artists_in_db = artist_instance.get_all_artists()
|
||||
|
||||
return {'sample': artists_in_db[:25]}
|
||||
functions.populate_images()
|
||||
|
||||
|
||||
@bp.route("/artist/<artist>")
|
||||
@@ -223,21 +113,21 @@ def populate_images():
|
||||
def getArtistData(artist: str):
|
||||
print(artist)
|
||||
artist = urllib.parse.unquote(artist)
|
||||
artist_obj = artist_instance.get_artists_by_name(artist)
|
||||
artist_obj = instances.artist_instance.get_artists_by_name(artist)
|
||||
|
||||
def getArtistSongs():
|
||||
songs = all_songs_instance.find_songs_by_artist(artist)
|
||||
songs = instances.songs_instance.find_songs_by_artist(artist)
|
||||
|
||||
return songs
|
||||
|
||||
artist_songs = getArtistSongs()
|
||||
songs = remove_duplicates(artist_songs)
|
||||
songs = helpers.remove_duplicates(artist_songs)
|
||||
|
||||
def getArtistAlbums():
|
||||
artist_albums = []
|
||||
albums_with_count = []
|
||||
|
||||
albums = all_songs_instance.find_songs_by_album_artist(artist)
|
||||
albums = instances.songs_instance.find_songs_by_album_artist(artist)
|
||||
|
||||
for song in songs:
|
||||
song['artists'] = song['artists'].split(', ')
|
||||
@@ -282,7 +172,8 @@ def getFolderTree(folder: str = None):
|
||||
|
||||
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]
|
||||
files_in_dir = helpers.run_fast_scandir(
|
||||
entry.path, [".flac", ".mp3"])[1]
|
||||
|
||||
if len(files_in_dir) != 0:
|
||||
dir = {
|
||||
@@ -295,12 +186,12 @@ def getFolderTree(folder: str = None):
|
||||
|
||||
# if entry.is_file():
|
||||
# if isValidFile(entry.name) == True:
|
||||
# file = all_songs_instance.find_song_by_path(entry.path)
|
||||
# file = instances.songs_instance.find_song_by_path(entry.path)
|
||||
|
||||
# if not file:
|
||||
# getTags(entry.path)
|
||||
|
||||
# songs_array = all_songs_instance.find_songs_by_folder(
|
||||
# songs_array = instances.songs_instance.find_songs_by_folder(
|
||||
# req_dir)
|
||||
|
||||
songs = []
|
||||
@@ -311,19 +202,16 @@ def getFolderTree(folder: str = None):
|
||||
|
||||
for song in songs:
|
||||
try:
|
||||
song['artists'] = song['artists'].split(', ') or None
|
||||
song['artists'] = song['artists'].split(', ')
|
||||
except:
|
||||
pass
|
||||
if song['image'] is not None:
|
||||
print(song['image'])
|
||||
song['image'] = img_path + song['image']
|
||||
|
||||
return {"files": remove_duplicates(songs), "folders": sorted(folders, key=lambda i: i['name'])}
|
||||
return {"files": helpers.remove_duplicates(songs), "folders": sorted(folders, key=lambda i: i['name'])}
|
||||
|
||||
|
||||
@bp.route('/qwerty')
|
||||
def populateArtists():
|
||||
all_songs = all_songs_instance.get_all_songs()
|
||||
all_songs = instances.songs_instance.get_all_songs()
|
||||
|
||||
artists = []
|
||||
|
||||
@@ -338,14 +226,14 @@ def populateArtists():
|
||||
if a_obj not in artists:
|
||||
artists.append(a_obj)
|
||||
|
||||
artist_instance.insert_artist(a_obj)
|
||||
instances.artist_instance.insert_artist(a_obj)
|
||||
|
||||
return {'songs': artists}
|
||||
|
||||
|
||||
@bp.route('/albums')
|
||||
def getAlbums():
|
||||
s = all_songs_instance.get_all_songs()
|
||||
s = instances.songs_instance.get_all_songs()
|
||||
|
||||
albums = []
|
||||
|
||||
@@ -366,13 +254,13 @@ def getAlbumSongs(query: str):
|
||||
album = query.split('::')[0].replace('|', '/')
|
||||
artist = query.split('::')[1].replace('|', '/')
|
||||
|
||||
songs = all_songs_instance.find_songs_by_album(album, artist)
|
||||
songs = instances.songs_instance.find_songs_by_album(album, artist)
|
||||
|
||||
print(artist)
|
||||
|
||||
for song in songs:
|
||||
song['artists'] = song['artists'].split(', ')
|
||||
song['image'] = img_path + song['image']
|
||||
song['image'] = "http://127.0.0.1:8900/images/thumbnails/" + song['image']
|
||||
|
||||
album_obj = {
|
||||
"name": album,
|
||||
|
||||
Reference in New Issue
Block a user