mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
server: better structure album tracks endpoint
- client: add now-playing and recommendations to home right-sidebar
This commit is contained in:
+9
-14
@@ -15,7 +15,7 @@ def initialize() -> None:
|
||||
Runs all the necessary setup functions.
|
||||
"""
|
||||
helpers.create_config_dir()
|
||||
helpers.check_for_new_songs()
|
||||
# helpers.check_for_new_songs()
|
||||
|
||||
|
||||
initialize()
|
||||
@@ -28,12 +28,10 @@ def adutsfsd():
|
||||
|
||||
@bp.route('/search')
|
||||
def search_by_title():
|
||||
query:str = ""
|
||||
|
||||
if not request.args.get('q'):
|
||||
query = "mexican girl"
|
||||
query:str = "mexican girl"
|
||||
else:
|
||||
query = request.args.get('q')
|
||||
query:str = str(request.args.get('q'))
|
||||
|
||||
albums = []
|
||||
artists = []
|
||||
@@ -209,13 +207,13 @@ def getFolderTree(folder: str):
|
||||
entry.path, [".flac", ".mp3"])[1]
|
||||
|
||||
if len(files_in_dir) != 0:
|
||||
dir = {
|
||||
_dir = {
|
||||
"name": entry.name,
|
||||
"count": len(files_in_dir),
|
||||
"path": entry.path.replace(home_dir, ""),
|
||||
}
|
||||
|
||||
folders.append(dir)
|
||||
folders.append(_dir)
|
||||
|
||||
songs = []
|
||||
|
||||
@@ -245,22 +243,19 @@ def getAlbums():
|
||||
return {'albums': albums}
|
||||
|
||||
|
||||
@bp.route('/albums/<query>')
|
||||
@bp.route('/album/<title>/<artist>/tracks')
|
||||
@cache.cached()
|
||||
def getAlbumSongs(query: str):
|
||||
album = query.split('::')[0].replace('|', '/')
|
||||
artist = query.split('::')[1].replace('|', '/')
|
||||
|
||||
def get_album_tracks(title:str, artist:str):
|
||||
songs = []
|
||||
|
||||
for track in all_the_f_music:
|
||||
if track.album == album and track.album_artist == artist:
|
||||
if track.album_artist == artist and track.album == title:
|
||||
songs.append(track)
|
||||
|
||||
songs = helpers.remove_duplicates(songs)
|
||||
|
||||
album_obj = {
|
||||
"name": album,
|
||||
"name": title,
|
||||
"count": len(songs),
|
||||
"duration": "56 Minutes",
|
||||
"image": songs[0].image,
|
||||
|
||||
+65
-62
@@ -23,21 +23,21 @@ from app import models
|
||||
|
||||
|
||||
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.
|
||||
'''
|
||||
print('\nchecking for new tracks')
|
||||
"""
|
||||
print("\nchecking for new tracks")
|
||||
files = helpers.run_fast_scandir(helpers.home_dir, [".flac", ".mp3"])[1]
|
||||
|
||||
for file in files:
|
||||
getTags(file)
|
||||
|
||||
api.all_the_f_music = helpers.getAllSongs()
|
||||
print('\ncheck done')
|
||||
print("\ncheck done")
|
||||
|
||||
|
||||
def populate_images():
|
||||
@@ -46,25 +46,27 @@ def populate_images():
|
||||
artists = []
|
||||
|
||||
for song in all_songs:
|
||||
this_artists = song['artists'].split(', ')
|
||||
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))
|
||||
bar = Bar("Processing images", max=len(artists))
|
||||
for artist in artists:
|
||||
file_path = helpers.app_dir + '/images/artists/' + \
|
||||
artist.replace('/', '::') + '.webp'
|
||||
file_path = (
|
||||
helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp"
|
||||
)
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
|
||||
def try_save_image():
|
||||
url = 'https://api.deezer.com/search/artist?q={}'.format(artist)
|
||||
url = "https://api.deezer.com/search/artist?q={}".format(artist)
|
||||
response = requests.get(url)
|
||||
data = response.json()
|
||||
|
||||
try:
|
||||
img_path = data['data'][0]['picture_medium']
|
||||
img_path = data["data"][0]["picture_medium"]
|
||||
except:
|
||||
img_path = None
|
||||
|
||||
@@ -72,8 +74,9 @@ def populate_images():
|
||||
# save image as webp
|
||||
img = Image.open(BytesIO(requests.get(img_path).content))
|
||||
img.save(file_path, format="webp")
|
||||
|
||||
try:
|
||||
try_save_image()
|
||||
try_save_image()
|
||||
except requests.exceptions.ConnectionError:
|
||||
time.sleep(5)
|
||||
try_save_image()
|
||||
@@ -94,31 +97,29 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
"""
|
||||
Returns a path to a random image in the defaults directory.
|
||||
"""
|
||||
path = str(random.randint(0, 10)) + '.webp'
|
||||
path = str(random.randint(0, 10)) + ".webp"
|
||||
return path
|
||||
|
||||
webp_path = audio_file_path.split('/')[-1] + '.webp'
|
||||
webp_path = audio_file_path.split("/")[-1] + ".webp"
|
||||
img_path = os.path.join(helpers.app_dir, "images", "thumbnails", webp_path)
|
||||
|
||||
if os.path.exists(img_path):
|
||||
return webp_path
|
||||
|
||||
if audio_file_path.endswith('.flac'):
|
||||
if audio_file_path.endswith(".flac"):
|
||||
try:
|
||||
audio = FLAC(audio_file_path)
|
||||
album_art = audio.pictures[0].data
|
||||
except:
|
||||
album_art = None
|
||||
elif audio_file_path.endswith('.mp3'):
|
||||
elif audio_file_path.endswith(".mp3"):
|
||||
try:
|
||||
audio = ID3(audio_file_path)
|
||||
album_art = audio.getall('APIC')[0].data
|
||||
album_art = audio.getall("APIC")[0].data
|
||||
except:
|
||||
album_art = None
|
||||
|
||||
if album_art is None:
|
||||
return use_defaults()
|
||||
else:
|
||||
if album_art is not None:
|
||||
img = Image.open(BytesIO(album_art))
|
||||
|
||||
try:
|
||||
@@ -126,13 +127,15 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
small_img.save(img_path, format="webp")
|
||||
except OSError:
|
||||
try:
|
||||
png = img.convert('RGB')
|
||||
png = img.convert("RGB")
|
||||
small_img = png.resize((250, 250), Image.ANTIALIAS)
|
||||
small_img.save(img_path, format="webp")
|
||||
except:
|
||||
return use_defaults()
|
||||
|
||||
return webp_path
|
||||
else:
|
||||
return use_defaults()
|
||||
|
||||
|
||||
def getTags(full_path: str) -> dict:
|
||||
@@ -140,72 +143,72 @@ def getTags(full_path: str) -> dict:
|
||||
Returns a dictionary of tags for a given file.
|
||||
"""
|
||||
|
||||
if full_path.endswith('.flac'):
|
||||
if full_path.endswith(".flac"):
|
||||
try:
|
||||
audio = FLAC(full_path)
|
||||
except:
|
||||
except MutagenError:
|
||||
return
|
||||
elif full_path.endswith('.mp3'):
|
||||
elif full_path.endswith(".mp3"):
|
||||
try:
|
||||
audio = MP3(full_path)
|
||||
except:
|
||||
except MutagenError:
|
||||
return
|
||||
|
||||
try:
|
||||
artists = audio['artist'][0]
|
||||
artists = audio["artist"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
artists = audio['TPE1'][0]
|
||||
artists = audio["TPE1"][0]
|
||||
except:
|
||||
artists = 'Unknown'
|
||||
artists = "Unknown"
|
||||
except IndexError:
|
||||
artists = 'Unknown'
|
||||
artists = "Unknown"
|
||||
|
||||
try:
|
||||
album_artist = audio['albumartist'][0]
|
||||
album_artist = audio["albumartist"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
album_artist = audio['TPE2'][0]
|
||||
album_artist = audio["TPE2"][0]
|
||||
except:
|
||||
album_artist = 'Unknown'
|
||||
album_artist = "Unknown"
|
||||
except IndexError:
|
||||
album_artist = 'Unknown'
|
||||
album_artist = "Unknown"
|
||||
|
||||
try:
|
||||
title = audio['title'][0]
|
||||
title = audio["title"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
title = audio['TIT2'][0]
|
||||
title = audio["TIT2"][0]
|
||||
except:
|
||||
title = full_path.split('/')[-1]
|
||||
title = full_path.split("/")[-1]
|
||||
except:
|
||||
title = full_path.split('/')[-1]
|
||||
title = full_path.split("/")[-1]
|
||||
|
||||
try:
|
||||
album = audio['album'][0]
|
||||
album = audio["album"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
album = audio['TALB'][0]
|
||||
album = audio["TALB"][0]
|
||||
except:
|
||||
album = "Unknown"
|
||||
except IndexError:
|
||||
album = "Unknown"
|
||||
|
||||
try:
|
||||
genre = audio['genre'][0]
|
||||
genre = audio["genre"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
genre = audio['TCON'][0]
|
||||
genre = audio["TCON"][0]
|
||||
except:
|
||||
genre = "Unknown"
|
||||
except IndexError:
|
||||
genre = "Unknown"
|
||||
|
||||
try:
|
||||
date = audio['date'][0]
|
||||
date = audio["date"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
date = audio['TDRC'][0]
|
||||
date = audio["TDRC"][0]
|
||||
except:
|
||||
date = "Unknown"
|
||||
except IndexError:
|
||||
@@ -216,10 +219,10 @@ def getTags(full_path: str) -> dict:
|
||||
length = str(datetime.timedelta(seconds=round(audio.info.length)))
|
||||
|
||||
if length[:2] == "0:":
|
||||
length = length.replace('0:', '')
|
||||
length = length.replace("0:", "")
|
||||
|
||||
tags = {
|
||||
"filepath": full_path.replace(helpers.home_dir, ''),
|
||||
"filepath": full_path.replace(helpers.home_dir, ""),
|
||||
"folder": os.path.dirname(full_path).replace(helpers.home_dir, ""),
|
||||
"title": title,
|
||||
"artists": artists,
|
||||
@@ -227,7 +230,7 @@ def getTags(full_path: str) -> dict:
|
||||
"album": album,
|
||||
"genre": genre,
|
||||
"length": length,
|
||||
"bitrate": round(int(audio.info.bitrate)/1000),
|
||||
"bitrate": round(int(audio.info.bitrate) / 1000),
|
||||
"date": str(date)[:4],
|
||||
"image": img_path,
|
||||
}
|
||||
@@ -236,9 +239,10 @@ def getTags(full_path: str) -> dict:
|
||||
return tags
|
||||
|
||||
|
||||
def getAlbumBio(title: str, album_artist: str) -> dict:
|
||||
last_fm_url = 'http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={}&artist={}&album={}&format=json'.format(
|
||||
helpers.last_fm_api_key, album_artist, title)
|
||||
def getAlbumBio(title: str, album_artist: str):
|
||||
last_fm_url = "http://ws.audioscrobbler.com/2.0/?method=album.getinfo&api_key={}&artist={}&album={}&format=json".format(
|
||||
helpers.LAST_FM_API_KEY, album_artist, title
|
||||
)
|
||||
|
||||
try:
|
||||
response = requests.get(last_fm_url)
|
||||
@@ -247,8 +251,7 @@ def getAlbumBio(title: str, album_artist: str) -> dict:
|
||||
return "None"
|
||||
|
||||
try:
|
||||
bio = data['album']['wiki']['summary'].split(
|
||||
'<a href="https://www.last.fm/')[0]
|
||||
bio = data["album"]["wiki"]["summary"].split('<a href="https://www.last.fm/')[0]
|
||||
except KeyError:
|
||||
bio = None
|
||||
|
||||
@@ -260,16 +263,16 @@ def getAlbumBio(title: str, album_artist: str) -> dict:
|
||||
|
||||
def create_track_class(tags):
|
||||
return models.Track(
|
||||
tags['_id']["$oid"],
|
||||
tags['title'],
|
||||
tags['artists'],
|
||||
tags['album_artist'],
|
||||
tags['album'],
|
||||
tags['filepath'],
|
||||
tags['folder'],
|
||||
tags['length'],
|
||||
tags['date'],
|
||||
tags['genre'],
|
||||
tags['bitrate'],
|
||||
tags['image']
|
||||
tags["_id"]["$oid"],
|
||||
tags["title"],
|
||||
tags["artists"],
|
||||
tags["album_artist"],
|
||||
tags["album"],
|
||||
tags["filepath"],
|
||||
tags["folder"],
|
||||
tags["length"],
|
||||
tags["date"],
|
||||
tags["genre"],
|
||||
tags["bitrate"],
|
||||
tags["image"],
|
||||
)
|
||||
|
||||
@@ -16,7 +16,7 @@ from app import functions
|
||||
|
||||
home_dir = os.path.expanduser('~') + '/'
|
||||
app_dir = os.path.join(home_dir, '.musicx')
|
||||
last_fm_api_key = "762db7a44a9e6fb5585661f5f2bdf23a"
|
||||
LAST_FM_API_KEY = "762db7a44a9e6fb5585661f5f2bdf23a"
|
||||
|
||||
|
||||
def background(f):
|
||||
@@ -42,7 +42,7 @@ def check_for_new_songs():
|
||||
time.sleep(300)
|
||||
|
||||
|
||||
def run_fast_scandir(dir: str, ext: str):
|
||||
def run_fast_scandir(_dir:str, ext: list):
|
||||
"""
|
||||
Scans a directory for files with a specific extension. Returns a list of files and folders in the directory.
|
||||
"""
|
||||
@@ -50,15 +50,15 @@ def run_fast_scandir(dir: str, ext: str):
|
||||
subfolders = []
|
||||
files = []
|
||||
|
||||
for f in os.scandir(dir):
|
||||
for f in os.scandir(_dir):
|
||||
if f.is_dir() and not f.name.startswith('.'):
|
||||
subfolders.append(f.path)
|
||||
if f.is_file():
|
||||
if os.path.splitext(f.name)[1].lower() in ext:
|
||||
files.append(f.path)
|
||||
|
||||
for dir in list(subfolders):
|
||||
sf, f = run_fast_scandir(dir, ext)
|
||||
for _dir in list(subfolders):
|
||||
sf, f = run_fast_scandir(_dir, ext)
|
||||
subfolders.extend(sf)
|
||||
files.extend(f)
|
||||
|
||||
@@ -111,14 +111,14 @@ def create_config_dir() -> None:
|
||||
Creates the config directory if it doesn't exist.
|
||||
"""
|
||||
|
||||
home_dir = os.path.expanduser('~')
|
||||
config_folder = os.path.join(home_dir, app_dir)
|
||||
_home_dir = os.path.expanduser('~')
|
||||
config_folder = os.path.join(_home_dir, app_dir)
|
||||
|
||||
dirs = ["", "images", "images/defaults",
|
||||
"images/artists", "images/thumbnails"]
|
||||
|
||||
for dir in dirs:
|
||||
path = os.path.join(config_folder, dir)
|
||||
for _dir in dirs:
|
||||
path = os.path.join(config_folder, _dir)
|
||||
|
||||
try:
|
||||
os.makedirs(path)
|
||||
|
||||
Reference in New Issue
Block a user