mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 04:23:01 +00:00
iniitialize project teardown
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
from typing import List
|
||||
from app import models, instances, functions
|
||||
|
||||
all_albums: List[models.Album] = []
|
||||
|
||||
|
||||
def create_all_albums() -> List[models.Album]:
|
||||
"""Creates album objects for all albums"""
|
||||
albums: list[models.Album] = []
|
||||
|
||||
for album in instances.album_instance.get_all_albums():
|
||||
albums.append(models.Album(album))
|
||||
|
||||
return albums
|
||||
|
||||
|
||||
def get_album_duration(album: List[models.Track]) -> int:
|
||||
"""
|
||||
Gets the duration of an album.
|
||||
"""
|
||||
|
||||
album_duration = 0
|
||||
|
||||
for track in album:
|
||||
try:
|
||||
album_duration += track.length
|
||||
except AttributeError:
|
||||
album_duration += track["length"]
|
||||
|
||||
return album_duration
|
||||
|
||||
|
||||
def get_album_image(album: list) -> str:
|
||||
"""
|
||||
Gets the image of an album.
|
||||
"""
|
||||
|
||||
for track in album:
|
||||
img = functions.extract_thumb(track["filepath"])
|
||||
|
||||
if img is not None:
|
||||
return img
|
||||
|
||||
return functions.use_defaults()
|
||||
+3
-3
@@ -11,9 +11,6 @@ bp = Blueprint("api", __name__, url_prefix="")
|
||||
|
||||
home_dir = helpers.home_dir
|
||||
|
||||
all_the_f_albums = helpers.create_all_albums()
|
||||
all_the_f_music = helpers.create_all_tracks()
|
||||
|
||||
|
||||
def initialize() -> None:
|
||||
"""
|
||||
@@ -26,6 +23,9 @@ def initialize() -> None:
|
||||
|
||||
initialize()
|
||||
|
||||
all_the_f_albums = helpers.create_all_albums()
|
||||
all_the_f_music = helpers.create_all_tracks()
|
||||
|
||||
|
||||
@bp.route("/")
|
||||
def say_hi():
|
||||
|
||||
@@ -8,6 +8,7 @@ from io import BytesIO
|
||||
import random
|
||||
import datetime
|
||||
import mutagen
|
||||
import urllib
|
||||
|
||||
import requests
|
||||
from mutagen.flac import MutagenError
|
||||
@@ -18,9 +19,7 @@ from PIL import Image
|
||||
|
||||
from app import helpers
|
||||
from app import instances
|
||||
from app import api
|
||||
from app import models
|
||||
import urllib
|
||||
from app import api, settings
|
||||
|
||||
|
||||
def populate():
|
||||
@@ -166,11 +165,11 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
"""
|
||||
Extracts the thumbnail from an audio file. Returns the path to the thumbnail.
|
||||
"""
|
||||
webp_path = urllib.parse.quote_plus(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
|
||||
return urllib.parse.quote(webp_path)
|
||||
|
||||
album_art = return_album_art(audio_file_path)
|
||||
|
||||
@@ -188,7 +187,7 @@ def extract_thumb(audio_file_path: str) -> str:
|
||||
except:
|
||||
return None
|
||||
|
||||
return webp_path
|
||||
return urllib.parse.quote(webp_path)
|
||||
else:
|
||||
return None
|
||||
|
||||
@@ -321,7 +320,7 @@ 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, albumartist, title
|
||||
settings.LAST_FM_API_KEY, albumartist, title
|
||||
)
|
||||
|
||||
try:
|
||||
|
||||
+10
-96
@@ -16,11 +16,10 @@ from PIL import Image
|
||||
from app import instances
|
||||
from app import functions
|
||||
from app import watchdoge
|
||||
from app import models
|
||||
from app import models, settings
|
||||
|
||||
home_dir = os.path.expanduser("~") + "/"
|
||||
app_dir = os.path.join(home_dir, ".musicx")
|
||||
LAST_FM_API_KEY = "762db7a44a9e6fb5585661f5f2bdf23a"
|
||||
|
||||
|
||||
def background(func):
|
||||
@@ -43,7 +42,7 @@ def reindex_tracks():
|
||||
flag = False
|
||||
|
||||
while flag is False:
|
||||
# functions.populate()
|
||||
functions.populate()
|
||||
functions.get_all_albums()
|
||||
# functions.populate_images()
|
||||
# functions.save_t_colors()
|
||||
@@ -104,14 +103,14 @@ def remove_duplicates(tracklist: List[models.Track]) -> List[models.Track]:
|
||||
return tracklist
|
||||
|
||||
|
||||
def save_image(url: str, path: str) -> None:
|
||||
"""
|
||||
Saves an image from an url to a path.
|
||||
"""
|
||||
# def save_image(url: str, path: str) -> None:
|
||||
# """
|
||||
# Saves an image from an url to a path.
|
||||
# """
|
||||
|
||||
response = requests.get(url)
|
||||
img = Image.open(BytesIO(response.content))
|
||||
img.save(path, "JPEG")
|
||||
# response = requests.get(url)
|
||||
# img = Image.open(BytesIO(response.content))
|
||||
# img.save(path, "JPEG")
|
||||
|
||||
|
||||
def is_valid_file(filename: str) -> bool:
|
||||
@@ -125,62 +124,7 @@ def is_valid_file(filename: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
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)
|
||||
|
||||
dirs = ["", "images", "images/defaults", "images/artists", "images/thumbnails"]
|
||||
|
||||
for _dir in dirs:
|
||||
path = os.path.join(config_folder, _dir)
|
||||
|
||||
try:
|
||||
os.makedirs(path)
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
os.chmod(path, 0o755)
|
||||
|
||||
|
||||
def create_all_tracks() -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs under the ~/ directory.
|
||||
"""
|
||||
print("Getting all songs...")
|
||||
|
||||
tracks: list[models.Track] = []
|
||||
|
||||
for track in instances.songs_instance.get_all_songs():
|
||||
try:
|
||||
os.chmod(track["filepath"], 0o755)
|
||||
except FileNotFoundError:
|
||||
instances.songs_instance.remove_song_by_filepath(track["filepath"])
|
||||
|
||||
album = instances.album_instance.get_album_by_name(track['album', track['albumartist']])
|
||||
|
||||
if album is None:
|
||||
track['albumid'] = album['albumid']
|
||||
track['image'] = album['image']
|
||||
tracks.append(models.Track(track))
|
||||
|
||||
return tracks
|
||||
|
||||
|
||||
def create_all_albums() -> List[models.Album]:
|
||||
"""Creates album objects for all albums"""
|
||||
albums: list[models.Album] = []
|
||||
|
||||
for album in instances.album_instance.get_all_albums():
|
||||
albums.append(models.Album(album))
|
||||
|
||||
return albums
|
||||
|
||||
|
||||
def extract_colors(image) -> list:
|
||||
def extract_image_colors(image) -> list:
|
||||
"""Extracts 2 of the most dominant colors from an image."""
|
||||
try:
|
||||
colors = sorted(colorgram.extract(image, 2), key=lambda c: c.hsl.h)
|
||||
@@ -194,33 +138,3 @@ def extract_colors(image) -> list:
|
||||
formatted_colors.append(color)
|
||||
|
||||
return formatted_colors
|
||||
|
||||
|
||||
def get_album_duration(album: List[models.Track]) -> int:
|
||||
"""
|
||||
Gets the duration of an album.
|
||||
"""
|
||||
|
||||
album_duration = 0
|
||||
|
||||
for track in album:
|
||||
try:
|
||||
album_duration += track.length
|
||||
except AttributeError:
|
||||
album_duration += track["length"]
|
||||
|
||||
return album_duration
|
||||
|
||||
|
||||
def get_album_image(album: list) -> str:
|
||||
"""
|
||||
Gets the image of an album.
|
||||
"""
|
||||
|
||||
for track in album:
|
||||
img = functions.extract_thumb(track["filepath"])
|
||||
|
||||
if img is not None:
|
||||
return img
|
||||
|
||||
return functions.use_defaults()
|
||||
|
||||
@@ -253,6 +253,7 @@ class Track:
|
||||
|
||||
def __init__(self, tags):
|
||||
self.trackid = tags["_id"]["$oid"]
|
||||
self.albumid = tags["albumid"]
|
||||
self.title = tags["title"]
|
||||
self.artists = tags["artists"].split(", ")
|
||||
self.albumartist = tags["albumartist"]
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import os
|
||||
from app import settings
|
||||
|
||||
|
||||
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, settings.CONFIG_FOLDER)
|
||||
|
||||
dirs = ["", "images", "images/artists", "images/thumbnails"]
|
||||
|
||||
for _dir in dirs:
|
||||
path = os.path.join(config_folder, _dir)
|
||||
|
||||
try:
|
||||
os.makedirs(path)
|
||||
except FileExistsError:
|
||||
pass
|
||||
|
||||
os.chmod(path, 0o755)
|
||||
@@ -0,0 +1,2 @@
|
||||
CONFIG_FOLDER = "alice"
|
||||
LAST_FM_API_KEY = "762db7a44a9e6fb5585661f5f2bdf23a"
|
||||
@@ -0,0 +1,34 @@
|
||||
import os
|
||||
from typing import List
|
||||
from app import models, instances
|
||||
|
||||
|
||||
ALL_MUSIC: List[models.Track] = []
|
||||
|
||||
|
||||
def create_all_tracks() -> List[models.Track]:
|
||||
"""
|
||||
Gets all songs under the ~/ directory.
|
||||
"""
|
||||
print("Getting all songs...")
|
||||
|
||||
tracks: list[models.Track] = []
|
||||
|
||||
for track in instances.songs_instance.get_all_songs():
|
||||
print(track)
|
||||
try:
|
||||
os.chmod(track["filepath"], 0o755)
|
||||
except FileNotFoundError:
|
||||
instances.songs_instance.remove_song_by_filepath(track["filepath"])
|
||||
|
||||
album = instances.album_instance.get_album_by_name(
|
||||
track["album"], track["albumartist"]
|
||||
)
|
||||
|
||||
track["albumid"] = album["_id"]["$oid"]
|
||||
track["image"] = album["image"]
|
||||
|
||||
tracks.append(models.Track(track))
|
||||
|
||||
ALL_MUSIC.clear()
|
||||
ALL_MUSIC.extend(tracks)
|
||||
Reference in New Issue
Block a user