mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 20:43:04 +00:00
add docstrings to python code
This commit is contained in:
+155
-108
@@ -4,17 +4,17 @@ This module contains larger functions for the server
|
||||
|
||||
import time
|
||||
import os
|
||||
import requests
|
||||
from io import BytesIO
|
||||
import random
|
||||
import datetime
|
||||
import mutagen
|
||||
|
||||
import requests
|
||||
from mutagen.flac import MutagenError
|
||||
from mutagen.mp3 import MP3
|
||||
from mutagen.id3 import ID3
|
||||
from mutagen.flac import FLAC
|
||||
from progress.bar import Bar
|
||||
from PIL import Image
|
||||
from io import BytesIO
|
||||
|
||||
from app import helpers
|
||||
from app import instances
|
||||
@@ -30,17 +30,30 @@ def populate():
|
||||
also checks if the album art exists in the image path, if not tries to
|
||||
extract it.
|
||||
"""
|
||||
start = time.time()
|
||||
print("\nchecking for new tracks")
|
||||
files = helpers.run_fast_scandir(helpers.home_dir, [".flac", ".mp3"])[1]
|
||||
|
||||
for file in files:
|
||||
getTags(file)
|
||||
tags = get_tags(file)
|
||||
|
||||
api.all_the_f_music = helpers.getAllSongs()
|
||||
print("\ncheck done")
|
||||
if tags is not None:
|
||||
instances.songs_instance.insert_song(tags)
|
||||
|
||||
api.all_the_f_music = helpers.get_all_songs()
|
||||
print("\n check done")
|
||||
end = time.time()
|
||||
|
||||
print(
|
||||
str(datetime.timedelta(seconds=round(end - start)))
|
||||
+ " elapsed for "
|
||||
+ str(len(files))
|
||||
+ " files"
|
||||
)
|
||||
|
||||
|
||||
def populate_images():
|
||||
"""populates the artists images"""
|
||||
all_songs = instances.songs_instance.get_all_songs()
|
||||
|
||||
artists = []
|
||||
@@ -52,7 +65,7 @@ def populate_images():
|
||||
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"
|
||||
@@ -81,9 +94,17 @@ def populate_images():
|
||||
time.sleep(5)
|
||||
try_save_image()
|
||||
|
||||
bar.next()
|
||||
_bar.next()
|
||||
|
||||
bar.finish()
|
||||
_bar.finish()
|
||||
|
||||
|
||||
def use_defaults() -> str:
|
||||
"""
|
||||
Returns a path to a random image in the defaults directory.
|
||||
"""
|
||||
path = str(random.randint(0, 10)) + ".webp"
|
||||
return path
|
||||
|
||||
|
||||
def extract_thumb(audio_file_path: str) -> str:
|
||||
@@ -93,13 +114,6 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
|
||||
album_art = None
|
||||
|
||||
def use_defaults() -> str:
|
||||
"""
|
||||
Returns a path to a random image in the defaults directory.
|
||||
"""
|
||||
path = str(random.randint(0, 10)) + ".webp"
|
||||
return path
|
||||
|
||||
webp_path = audio_file_path.split("/")[-1] + ".webp"
|
||||
img_path = os.path.join(helpers.app_dir, "images", "thumbnails", webp_path)
|
||||
|
||||
@@ -138,110 +152,138 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
return use_defaults()
|
||||
|
||||
|
||||
def getTags(full_path: str) -> dict:
|
||||
def parse_artist_tag(audio):
|
||||
"""
|
||||
Parses the artist tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
artists = audio["artist"][0]
|
||||
except (KeyError, IndexError):
|
||||
artists = "Unknown"
|
||||
|
||||
return artists
|
||||
|
||||
|
||||
def parse_title_tag(audio, full_path: str):
|
||||
"""
|
||||
Parses the title tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
title = audio["title"][0]
|
||||
except (KeyError, IndexError):
|
||||
title = full_path.split("/")[-1]
|
||||
|
||||
return title
|
||||
|
||||
|
||||
def parse_album_artist_tag(audio):
|
||||
"""
|
||||
Parses the album artist tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
albumartist = audio["albumartist"][0]
|
||||
except (KeyError, IndexError):
|
||||
albumartist = "Unknown"
|
||||
|
||||
return albumartist
|
||||
|
||||
|
||||
def parse_album_tag(audio):
|
||||
"""
|
||||
Parses the album tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
album = audio["album"][0]
|
||||
except (KeyError, IndexError):
|
||||
album = "Unknown"
|
||||
|
||||
return album
|
||||
|
||||
|
||||
def parse_genre_tag(audio):
|
||||
"""
|
||||
Parses the genre tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
genre = audio["genre"][0]
|
||||
except (KeyError, IndexError):
|
||||
genre = "Unknown"
|
||||
|
||||
return genre
|
||||
|
||||
|
||||
def parse_date_tag(audio):
|
||||
"""
|
||||
Parses the date tag from an audio file.
|
||||
"""
|
||||
try:
|
||||
date = audio["date"][0]
|
||||
except (KeyError, IndexError):
|
||||
date = "Unknown"
|
||||
|
||||
return date
|
||||
|
||||
|
||||
def parse_track_number(audio):
|
||||
"""
|
||||
Parses the track number from an audio file.
|
||||
"""
|
||||
try:
|
||||
track_number = audio["tracknumber"][0]
|
||||
except (KeyError, IndexError):
|
||||
track_number = "Unknown"
|
||||
|
||||
return track_number
|
||||
|
||||
|
||||
def parse_disk_number(audio):
|
||||
"""
|
||||
Parses the disk number from an audio file.
|
||||
"""
|
||||
try:
|
||||
disk_number = audio["discnumber"][0]
|
||||
except (KeyError, IndexError):
|
||||
disk_number = "Unknown"
|
||||
|
||||
return disk_number
|
||||
|
||||
|
||||
def get_tags(full_path: str) -> dict:
|
||||
"""
|
||||
Returns a dictionary of tags for a given file.
|
||||
"""
|
||||
|
||||
if full_path.endswith(".flac"):
|
||||
try:
|
||||
audio = FLAC(full_path)
|
||||
except MutagenError:
|
||||
return
|
||||
elif full_path.endswith(".mp3"):
|
||||
try:
|
||||
audio = MP3(full_path)
|
||||
except MutagenError:
|
||||
return
|
||||
|
||||
try:
|
||||
artists = audio["artist"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
artists = audio["TPE1"][0]
|
||||
except:
|
||||
artists = "Unknown"
|
||||
except IndexError:
|
||||
artists = "Unknown"
|
||||
|
||||
try:
|
||||
album_artist = audio["albumartist"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
album_artist = audio["TPE2"][0]
|
||||
except:
|
||||
album_artist = "Unknown"
|
||||
except IndexError:
|
||||
album_artist = "Unknown"
|
||||
|
||||
try:
|
||||
title = audio["title"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
title = audio["TIT2"][0]
|
||||
except:
|
||||
title = full_path.split("/")[-1]
|
||||
except:
|
||||
title = full_path.split("/")[-1]
|
||||
|
||||
try:
|
||||
album = audio["album"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
album = audio["TALB"][0]
|
||||
except:
|
||||
album = "Unknown"
|
||||
except IndexError:
|
||||
album = "Unknown"
|
||||
|
||||
try:
|
||||
genre = audio["genre"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
genre = audio["TCON"][0]
|
||||
except:
|
||||
genre = "Unknown"
|
||||
except IndexError:
|
||||
genre = "Unknown"
|
||||
|
||||
try:
|
||||
date = audio["date"][0]
|
||||
except KeyError:
|
||||
try:
|
||||
date = audio["TDRC"][0]
|
||||
except:
|
||||
date = "Unknown"
|
||||
except IndexError:
|
||||
date = "Unknown"
|
||||
|
||||
img_path = extract_thumb(full_path)
|
||||
|
||||
length = str(datetime.timedelta(seconds=round(audio.info.length)))
|
||||
|
||||
if length[:2] == "0:":
|
||||
length = length.replace("0:", "")
|
||||
audio = mutagen.File(full_path, easy=True)
|
||||
except MutagenError:
|
||||
return None
|
||||
|
||||
tags = {
|
||||
"filepath": full_path.replace(helpers.home_dir, ""),
|
||||
"folder": os.path.dirname(full_path).replace(helpers.home_dir, ""),
|
||||
"title": title,
|
||||
"artists": artists,
|
||||
"album_artist": album_artist,
|
||||
"album": album,
|
||||
"genre": genre,
|
||||
"length": length,
|
||||
"artists": parse_artist_tag(audio),
|
||||
"title": parse_title_tag(audio, full_path),
|
||||
"albumartist": parse_album_artist_tag(audio),
|
||||
"album": parse_album_tag(audio),
|
||||
"genre": parse_genre_tag(audio),
|
||||
"date": parse_date_tag(audio)[:4],
|
||||
"tracknumber": parse_track_number(audio),
|
||||
"discnumber": parse_disk_number(audio),
|
||||
"length": audio.info.length,
|
||||
"bitrate": round(int(audio.info.bitrate) / 1000),
|
||||
"date": str(date)[:4],
|
||||
"image": img_path,
|
||||
"filepath": full_path.replace(helpers.home_dir, ""),
|
||||
"image": extract_thumb(full_path),
|
||||
"folder": os.path.dirname(full_path).replace(helpers.home_dir, ""),
|
||||
}
|
||||
|
||||
instances.songs_instance.insert_song(tags)
|
||||
print(tags['filepath'])
|
||||
|
||||
return tags
|
||||
|
||||
|
||||
def getAlbumBio(title: str, album_artist: str):
|
||||
def get_album_bio(title: str, albumartist: str):
|
||||
"""
|
||||
Returns the album bio for a given album.
|
||||
"""
|
||||
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
|
||||
helpers.LAST_FM_API_KEY, albumartist, title
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -262,11 +304,14 @@ def getAlbumBio(title: str, album_artist: str):
|
||||
|
||||
|
||||
def create_track_class(tags):
|
||||
"""
|
||||
Creates a Track class from a dictionary of tags.
|
||||
"""
|
||||
return models.Track(
|
||||
tags["_id"]["$oid"],
|
||||
tags["title"],
|
||||
tags["artists"],
|
||||
tags["album_artist"],
|
||||
tags["albumartist"],
|
||||
tags["album"],
|
||||
tags["filepath"],
|
||||
tags["folder"],
|
||||
@@ -275,4 +320,6 @@ def create_track_class(tags):
|
||||
tags["genre"],
|
||||
tags["bitrate"],
|
||||
tags["image"],
|
||||
tags['tracknumber'],
|
||||
tags['discnumber'],
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user