replace progress.bar with tqdm

This commit is contained in:
geoffrey45
2022-05-10 08:20:24 +03:00
parent f913e59103
commit 86c2744e07
6 changed files with 48 additions and 42 deletions
+2 -1
View File
@@ -44,7 +44,8 @@ def get_album_tracks():
artist = data["artist"]
songs = trackslib.get_album_tracks(album, artist)
index = albumslib.find_album(album, artist)
albumhash = helpers.create_album_hash(album, artist)
index = albumslib.find_album(api.ALBUMS, albumhash)
album = api.ALBUMS[index]
album.count = len(songs)
+12 -5
View File
@@ -14,6 +14,8 @@ from app.lib.populate import Populate
from PIL import Image
from progress.bar import Bar
from app.lib.trackslib import create_all_tracks
@helpers.background
def reindex_tracks():
@@ -41,6 +43,10 @@ def populate():
pop = Populate()
pop.run()
tracks = create_all_tracks()
api.TRACKS.clear()
api.TRACKS.extend(tracks)
@helpers.background
def fetch_image_path(artist: str) -> str or None:
@@ -76,8 +82,9 @@ def fetch_artist_images():
_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):
img_path = fetch_image_path(artist)
@@ -99,7 +106,8 @@ def fetch_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(
settings.LAST_FM_API_KEY, albumartist, title)
settings.LAST_FM_API_KEY, albumartist, title
)
try:
response = requests.get(last_fm_url)
@@ -108,8 +116,7 @@ def fetch_album_bio(title: str, albumartist: str):
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
+19 -26
View File
@@ -1,7 +1,6 @@
"""
This library contains all the functions related to albums.
"""
from copy import deepcopy
import random
import urllib
from typing import List
@@ -12,7 +11,7 @@ from app import models
from app.lib import taglib
from app.lib import trackslib
from progress.bar import Bar
from tqdm import tqdm
from app import helpers
@@ -26,13 +25,9 @@ def get_all_albums() -> List[models.Album]:
db_albums = instances.album_instance.get_all_albums()
_bar = Bar("Creating albums", max=len(db_albums))
for album in db_albums:
for album in tqdm(db_albums, desc="Creating albums"):
aa = models.Album(album)
albums.append(aa)
_bar.next()
_bar.finish()
return albums
@@ -54,27 +49,23 @@ def create_everything() -> List[models.Track]:
api.TRACKS.sort(key=lambda x: x.title)
def find_album(albumtitle: str, artist: str) -> int or None:
def find_album(albums: List[models.Album], hash: str) -> int | None:
"""
Finds an album by album title and artist.
"""
left = 0
right = len(api.ALBUMS) - 1
right = len(albums) - 1
iter = 0
while left <= right:
iter += 1
mid = (left + right) // 2
hash = helpers.create_album_hash(albumtitle, artist)
try:
if api.ALBUMS[mid].hash == hash:
return mid
except:
print(api.ALBUMS[mid])
if albums[mid].hash == hash:
return mid
if api.ALBUMS[mid].hash < hash:
if albums[mid].hash < hash:
left = mid + 1
else:
right = mid - 1
@@ -136,9 +127,9 @@ class GetAlbumTracks:
and album artist.
"""
def __init__(self, album: str, artist: str) -> None:
self.hash = helpers.create_album_hash(album, artist)
self.tracks = api.DB_TRACKS
def __init__(self, tracklist: list, albumhash: str) -> None:
self.hash = albumhash
self.tracks = tracklist
self.tracks.sort(key=lambda x: x["albumhash"])
def find_tracks(self):
@@ -151,7 +142,8 @@ class GetAlbumTracks:
self.tracks.remove(track)
index = trackslib.find_track(self.tracks, self.hash)
api.DB_TRACKS.extend(tracks)
# self.tracks.extend(tracks)
# self.tracks.sort(key=lambda x: x["albumhash"])
return tracks
@@ -159,7 +151,7 @@ def get_album_tracks(album: str, artist: str) -> List:
return GetAlbumTracks(album, artist).find_tracks()
def create_album(track) -> models.Album:
def create_album(track: dict, tracklist: list) -> models.Album:
"""
Generates and returns an album object from a track object.
"""
@@ -168,15 +160,16 @@ def create_album(track) -> models.Album:
"artist": track["albumartist"],
}
album_tracks = get_album_tracks(album["title"], album["artist"])
albumhash = helpers.create_album_hash(album["title"], album["artist"])
album_tracks = get_album_tracks(tracklist, albumhash)
if len(album_tracks) == 0:
return None
album["date"] = album_tracks[0]["date"]
album["artistimage"] = urllib.parse.quote_plus(
album_tracks[0]["albumartist"] + ".webp"
)
album["image"] = get_album_image(album_tracks)
# album["image"] = "".join(x for x in albumhash if x not in "\/:*?<>|")
return album
+3 -5
View File
@@ -2,6 +2,8 @@ import time
from typing import List
from typing import Set
from tqdm import tqdm
from app import api
from app import helpers
from app import models
@@ -34,15 +36,11 @@ def create_folder(foldername: str) -> models.Folder:
def create_all_folders() -> Set[models.Folder]:
folders: List[models.Folder] = []
_bar = Bar("Creating folders", max=len(api.VALID_FOLDERS))
for foldername in api.VALID_FOLDERS:
for foldername in tqdm(api.VALID_FOLDERS, desc="Creating folders"):
folder = create_folder(foldername)
folders.append(folder)
_bar.next()
_bar.finish()
return folders
+7 -4
View File
@@ -10,7 +10,7 @@ from app.exceptions import TrackExistsInPlaylist
from app import helpers
@dataclass
@dataclass(slots=True)
class Track:
"""
Track class
@@ -46,7 +46,7 @@ class Track:
self.image = tags["image"]
self.tracknumber = tags["tracknumber"]
self.discnumber = tags["discnumber"]
self.albumhash = tags['albumhash']
self.albumhash = tags["albumhash"]
@dataclass
@@ -58,7 +58,6 @@ class Album:
title: str
artist: str
date: int
artistimage: str
image: str
hash: str
count: int = 0
@@ -68,10 +67,14 @@ class Album:
self.title = tags["title"]
self.artist = tags["artist"]
self.date = tags["date"]
self.artistimage = tags["artistimage"]
self.image = tags["image"]
self.hash = helpers.create_album_hash(self.title, self.artist)
try:
self.hash = tags["albumhash"]
except KeyError:
self.hash = helpers.create_album_hash(self.title, self.artist)
def get_p_track(ptrack):
for track in api.TRACKS:
+5 -1
View File
@@ -3,13 +3,15 @@ Contains default configs
"""
import os
from dataclasses import dataclass
import multiprocessing
# paths
CONFIG_FOLDER = ".alice"
HOME_DIR = os.path.expanduser("~")
APP_DIR = os.path.join(HOME_DIR, CONFIG_FOLDER)
THUMBS_PATH = os.path.join(APP_DIR, "images", "thumbnails")
TEST_DIR = "/home/cwilvx/Music/Link to Music/Chill"
# URL
IMG_BASE_URI = "http://127.0.0.1:8900/images/"
IMG_ARTIST_URI = IMG_BASE_URI + "artists/"
@@ -34,6 +36,8 @@ P_COLORS = [
"rgb(141, 11, 2)",
]
CPU_COUNT = multiprocessing.cpu_count()
class logger:
enable = True