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