rewrite getting artist images using classes

- remove left padding on #acontent
This commit is contained in:
geoffrey45
2022-05-31 00:05:37 +03:00
parent 94504a1575
commit 9879f131e8
6 changed files with 107 additions and 51 deletions
+86 -35
View File
@@ -14,6 +14,7 @@ from app.lib import watchdoge
from app.lib.populate import Populate from app.lib.populate import Populate
from PIL import Image from PIL import Image
from progress.bar import Bar from progress.bar import Bar
from concurrent.futures import ThreadPoolExecutor
from app.lib.trackslib import create_all_tracks from app.lib.trackslib import create_all_tracks
@@ -23,11 +24,10 @@ def reindex_tracks():
""" """
Checks for new songs every 5 minutes. Checks for new songs every 5 minutes.
""" """
is_underway = False
while True: while True:
populate() populate()
fetch_artist_images() CheckArtistImages()()
time.sleep(60) time.sleep(60)
@@ -49,52 +49,103 @@ def populate():
api.TRACKS.extend(tracks) api.TRACKS.extend(tracks)
@helpers.background class getArtistImage:
def fetch_image_path(artist: str) -> str or None:
""" """
Returns a direct link to an artist image. Returns an artist image url.
""" """
try: def __init__(self, artist: str):
url = f"https://api.deezer.com/search/artist?q={artist}" self.artist = artist
response = requests.get(url)
data = response.json()
return data["data"][0]["picture_medium"] def __call__(self):
except requests.exceptions.ConnectionError: try:
time.sleep(5) url = f"https://api.deezer.com/search/artist?q={self.artist}"
return None response = requests.get(url)
except (IndexError, KeyError): data = response.json()
return None
return data["data"][0]["picture_medium"]
except requests.exceptions.ConnectionError:
time.sleep(5)
return None
except (IndexError, KeyError):
return None
@helpers.background class useImageDownloader:
def fetch_artist_images(): def __init__(self, url: str, dest: str) -> None:
"""Downloads the artists images""" self.url = url
self.dest = dest
artists = [] def __call__(self) -> None:
try:
img = Image.open(BytesIO(requests.get(self.url).content))
img.save(self.dest, format="webp")
img.close()
except requests.exceptions.ConnectionError:
print("🔴🔴🔴🔴🔴🔴🔴")
time.sleep(5)
for song in tqdm(api.DB_TRACKS, desc="Finding artists"):
this_artists = song["artists"].split(", ")
for artist in this_artists: class CheckArtistImages:
if artist not in artists: def __init__(self):
artists.append(artist) self.artists: list[str] = []
print("Checking for artist images")
for artist in tqdm(artists, desc="Fetching images"): @staticmethod
file_path = ( def check_if_exists(img_path: str):
helpers.app_dir + "/images/artists/" + artist.replace("/", "::") + ".webp" """
Checks if an image exists on disk.
"""
if os.path.exists(img_path):
return True
else:
return False
def gather_artists(self):
"""
Loops through all the tracks and gathers all the artists.
"""
for song in api.DB_TRACKS:
this_artists: list = song["artists"].split(", ")
for artist in this_artists:
if artist not in self.artists:
self.artists.append(artist)
@classmethod
def download_image(cls, artistname: str):
"""
Checks if an artist image exists and downloads it if not.
:param artistname: The artist name
"""
img_path = (
helpers.app_dir
+ "/images/artists/"
+ artistname.replace("/", "::")
+ ".webp"
) )
if not os.path.exists(file_path): if cls.check_if_exists(img_path):
img_path = fetch_image_path(artist) return
if img_path is not None: url = getArtistImage(artistname)()
try:
img = Image.open(BytesIO(requests.get(img_path).content)) if url is None:
img.save(file_path, format="webp") return
except requests.exceptions.ConnectionError:
time.sleep(5) useImageDownloader(url, img_path)()
def __call__(self):
self.gather_artists()
with ThreadPoolExecutor() as pool:
pool.map(self.download_image, self.artists)
print("Done fetching images")
def fetch_album_bio(title: str, albumartist: str) -> str | None: def fetch_album_bio(title: str, albumartist: str) -> str | None:
+6 -6
View File
@@ -106,13 +106,13 @@ class Populate:
""" """
s = time.time() s = time.time()
# print(f"Started tagging files") print(f"Started tagging files")
# with ThreadPoolExecutor() as executor: with ThreadPoolExecutor() as executor:
# executor.map(self.get_tags, self.files) executor.map(self.get_tags, self.files)
with Pool(maxtasksperchild=10) as p: # with Pool(maxtasksperchild=10, processes=10) as p:
tags = p.map(get_tags, tqdm(self.files)) # tags = p.map(get_tags, tqdm(self.files))
self.process_tags(tags) # self.process_tags(tags)
# for t in tqdm(self.files): # for t in tqdm(self.files):
# self.get_tags(t) # self.get_tags(t)
+11 -4
View File
@@ -46,7 +46,12 @@ class Track:
self.length = tags["length"] self.length = tags["length"]
self.genre = tags["genre"] self.genre = tags["genre"]
self.bitrate = tags["bitrate"] self.bitrate = tags["bitrate"]
self.image = tags["image"]
try:
self.image = tags["image"]
except KeyError:
print(tags)
self.tracknumber = tags["tracknumber"] self.tracknumber = tags["tracknumber"]
self.discnumber = tags["discnumber"] self.discnumber = tags["discnumber"]
self.albumhash = tags["albumhash"] self.albumhash = tags["albumhash"]
@@ -97,9 +102,11 @@ class Album:
def get_p_track(ptrack): def get_p_track(ptrack):
for track in api.TRACKS: for track in api.TRACKS:
if (track.title == ptrack["title"] if (
and track.artists == ptrack["artists"] track.title == ptrack["title"]
and ptrack["album"] == track.album): and track.artists == ptrack["artists"]
and ptrack["album"] == track.album
):
return track return track
+1
View File
@@ -133,6 +133,7 @@ a {
grid-area: content; grid-area: content;
max-width: 1504px; max-width: 1504px;
padding: $small; padding: $small;
padding-left: 0;
overflow: auto; overflow: auto;
.nav { .nav {
+3 -3
View File
@@ -15,7 +15,6 @@
<div <div
class="h" class="h"
v-if="props.album.artist.toLowerCase() == 'various artists'" v-if="props.album.artist.toLowerCase() == 'various artists'"
> >
Compilation Compilation
</div> </div>
@@ -65,8 +64,9 @@ extrackColors();
height: 100%; height: 100%;
background-color: $black; background-color: $black;
background-color: #000000; background-color: #000000;
background-image: linear-gradient(147deg, #436a91 0%, #2c3e50 74%); background-image: linear-gradient(37deg, $black 20%, $gray, $black 90%);
.art {
.art {
width: 100%; width: 100%;
height: 100%; height: 100%;
left: 1rem; left: 1rem;
-3
View File
@@ -21,9 +21,6 @@ const pStore = usePStore();
<style lang="scss"> <style lang="scss">
#p-view { #p-view {
margin: $small;
margin-top: 0;
padding: $small;
overflow: auto; overflow: auto;
scrollbar-color: $gray2 transparent; scrollbar-color: $gray2 transparent;