Probably fixed false negative when polling in the middle of switching tracks.

This commit is contained in:
nikolark committed 2011-05-30 00:01:20 +02:00
1 parent 6ba7e00612
commit 33fa80ade4
2 files changed
+58 -4

No files matched your search

+9 -2
View File
@@ -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()
+49 -2
View File
@@ -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)'}