From 33fa80ade43e0ee8f94c69ca2484488020afea4c Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Mon, 30 May 2011 00:01:20 +0200 Subject: [PATCH] Probably fixed false negative when polling in the middle of switching tracks. --- oliver_screen/tracks/models.py | 11 ++++++-- oliver_screen/tracks/views.py | 51 ++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/oliver_screen/tracks/models.py b/oliver_screen/tracks/models.py index def9fa4..c604248 100644 --- a/oliver_screen/tracks/models.py +++ b/oliver_screen/tracks/models.py @@ -1,7 +1,14 @@ from django.db import models -# Create your models here. -# +class CurrentTrack(models.Model): + title = models.CharField(max_length=200) + artist = models.CharField(max_length=200) + image = models.CharField(max_length=200) + retries = models.IntegerField() + + def __unicode__(self): + return self.title + " - " + self.artist + class LastFMUser(models.Model): name = models.CharField(max_length=200) active = models.BooleanField() diff --git a/oliver_screen/tracks/views.py b/oliver_screen/tracks/views.py index 8b1bc17..9c4ec2a 100644 --- a/oliver_screen/tracks/views.py +++ b/oliver_screen/tracks/views.py @@ -2,7 +2,7 @@ from django.http import HttpResponse from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext -from oliver_screen.tracks.models import LastFMUser, YouTubeVideo +from oliver_screen.tracks.models import LastFMUser, YouTubeVideo, CurrentTrack import pylast from datetime import datetime @@ -44,7 +44,6 @@ def get_now_playing(request): user = get_lastfmuser() track = user.get_now_playing() now_playing = [] - video = "" if not track is None: artist = track.get_artist() video = get_youtubevideo(artist.get_name(), track.get_title()) @@ -58,8 +57,56 @@ def get_now_playing(request): 'image': image, 'video': video } + cache_current(now_playing) + else: + # No response from LastFM db? + now_playing = get_current_from_cache() + return render_to_response("public/now_playing.html", {'now_playing':now_playing}, context_instance=RequestContext(request)); +def cache_current(now_playing): + # Fetch the one CurrentTrack entry in the db and update it with the current track. + current = CurrentTrack.objects.get(pk=1) + if not current is None: + #update + current.artist = now_playing['artist'] + current.title = now_playing['title'] + current.image = now_playing['image'] + current.retries = 0 + current.save() + else: + # First time running? Then creat it. + current = CurrentTrack(artist=now_playing.artist, title=now_playing.title, image=now_playing.image, retries=0) + current.save() + +def get_current_from_cache(): + current = [] + try: + current = CurrentTrack.objects.get(pk=1) + except: + current = CurrentTrack(artist="", title="", image="", retries=0) + current.save() + + now_playing = {} + + # try 3 times before clearing the cache + if current.artist != "" and current.retries < 3: + now_playing = { + 'artist': current.artist, + 'title': current.title, + 'image': current.image + } + + current.retries = current.retries + 1 + current.save() + else: + current.artist = "" + current.title = "" + current.image = "" + current.save() + + return now_playing + # Mock views def dummy_get_last_track(request): played = {'artist': 'The Album Leaf', 'title': 'Another Day (Revised)'}