Commit f1663b06 authored by Gink's avatar Gink

Adds caching of album art, making the first retrieval the only slow one

parent d8a91ab0
...@@ -2,15 +2,21 @@ ...@@ -2,15 +2,21 @@
import pprint import pprint
import musicbrainzngs import musicbrainzngs
from musicbrainzngs import ResponseError from musicbrainzngs import ResponseError
from core.models import Album
pp = pprint.PrettyPrinter(indent=4) pp = pprint.PrettyPrinter(indent=4)
def init(): def init():
musicbrainzngs.set_useragent("mymusiclist","0.1", contact="none") musicbrainzngs.set_useragent("mymusiclist","0.1", contact="none")
musicbrainzngs.set_rate_limit(limit_or_interval=False, new_requests=1) musicbrainzngs.set_rate_limit(limit_or_interval=False, new_requests=1)
print("[Exthook] Initialized") print("[Exthook] Initialized")
def verifyAlbumArt(album, artist):
if(album.art == None):
album.art = fetchAlbumArt(album.album_name, artist)
album.save()
#may or may not add checks to see if valid http link
def getAlbumArtByName(name, artist): def fetchAlbumArt(name, artist):
debug("GET " + name + " " + artist) debug("GET " + name + " " + artist)
results = musicbrainzngs.search_release_groups(name, limit = 3, artistname=artist, primarytype="album") results = musicbrainzngs.search_release_groups(name, limit = 3, artistname=artist, primarytype="album")
#pp.pprint(results['release-group-list']) #pp.pprint(results['release-group-list'])
...@@ -21,9 +27,11 @@ def getAlbumArtByName(name, artist): ...@@ -21,9 +27,11 @@ def getAlbumArtByName(name, artist):
imagelist = musicbrainzngs.get_release_group_image_list(resultid) imagelist = musicbrainzngs.get_release_group_image_list(resultid)
except ResponseError: except ResponseError:
debug("No image found.") debug("No image found.")
return "NaN" return None
except NetworkError:
debug("Cannot connect to MB server")
return None
return imagelist['images'][0]['thumbnails']['large'] return imagelist['images'][0]['thumbnails']['large']
def debug(string): def debug(string):
print("[Exthook] " + string) print("[Exthook] " + string)
\ No newline at end of file
...@@ -12,6 +12,7 @@ class Album(models.Model): ...@@ -12,6 +12,7 @@ class Album(models.Model):
album_name = models.CharField(max_length=64) album_name = models.CharField(max_length=64)
year = models.DecimalField(max_digits=4, decimal_places=0) year = models.DecimalField(max_digits=4, decimal_places=0)
artist = models.ForeignKey(Artist) artist = models.ForeignKey(Artist)
art = models.URLField(null=True, blank=True)
def __str__(self): def __str__(self):
return self.album_name return self.album_name
......
...@@ -32,5 +32,5 @@ def album_profile(request, identifier): ...@@ -32,5 +32,5 @@ def album_profile(request, identifier):
result = Album.objects.filter(id = identifier) result = Album.objects.filter(id = identifier)
artist_name = result[0].artist.name artist_name = result[0].artist.name
songs = Song.objects.filter(album = identifier) songs = Song.objects.filter(album = identifier)
albumart = exthook.getAlbumArtByName(result[0].album_name, artist_name) exthook.verifyAlbumArt(result[0], artist_name)
return render(request, 'album.html', {'result':result[0], 'songs': songs, 'artist': artist_name, 'image': albumart}) return render(request, 'album.html', {'result':result[0], 'songs': songs, 'artist': artist_name})
\ No newline at end of file \ No newline at end of file
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
{% block sidebar %} {% block sidebar %}
<img src = "{{image}}" alt= "No album art available"> <img src = "{{result.art}}" alt= "No album art available">
<p> <p>
<b> {{result.album_name}} </b> ({{result.year}}) <br> <b> {{result.album_name}} </b> ({{result.year}}) <br>
{{artist}} <br> <br> {{artist}} <br> <br>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment