Browse Source

Only get history after latest stored one

master
Kevin Mok 6 years ago
parent
commit
a399960a49
  1. 4
      api/models.py
  2. 12
      api/views.py

4
api/models.py

@ -94,11 +94,11 @@ class History(models.Model):
class Meta: class Meta:
verbose_name = "History" verbose_name = "History"
verbose_name_plural = "History" verbose_name_plural = "History"
unique_together = (("user", "time"),)
unique_together = (("user", "timestamp"),)
history_id = models.AutoField(primary_key=True) history_id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
time = models.DateTimeField()
timestamp = models.DateTimeField()
track = models.ForeignKey(Track, on_delete=models.CASCADE) track = models.ForeignKey(Track, on_delete=models.CASCADE)
def __str__(self): def __str__(self):

12
api/views.py

@ -10,7 +10,7 @@ import string
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import JsonResponse from django.http import JsonResponse
from django.db.models import Count, Q
from django.db.models import Count, Q, Max
from .utils import * from .utils import *
from .models import * from .models import *
from login.models import User from login.models import User
@ -133,11 +133,11 @@ def parse_history(request, user_secret):
:returns: None :returns: None
""" """
payload = {'limit': str(USER_TRACKS_LIMIT)}
artist_genre_queue = []
user_obj = User.objects.get(secret=user_secret) user_obj = User.objects.get(secret=user_secret)
last_time_played = History.objects.filter(user=user_obj).aggregate(Max('timestamp'))['timestamp__max']
payload = {'limit': str(USER_TRACKS_LIMIT), 'after': last_time_played.isoformat()}
artist_genre_queue = []
user_headers = get_user_header(user_obj) user_headers = get_user_header(user_obj)
history_response = requests.get(HISTORY_ENDPOINT, history_response = requests.get(HISTORY_ENDPOINT,
headers=user_headers, headers=user_headers,
params=payload).json()['items'] params=payload).json()['items']
@ -171,7 +171,7 @@ def parse_history(request, user_secret):
track_artists, None) track_artists, None)
history_obj, history_created = History.objects.get_or_create( history_obj, history_created = History.objects.get_or_create(
user=user_obj, user=user_obj,
time=parse(track_dict['played_at']),
timestamp=parse(track_dict['played_at']),
track=track_obj,) track=track_obj,)
if console_logging: if console_logging:
@ -179,7 +179,7 @@ def parse_history(request, user_secret):
print("Added track #{} for user {}: {} - {}".format( print("Added track #{} for user {}: {} - {}".format(
tracks_processed, tracks_processed,
history_obj.user, history_obj.user,
history_obj.time,
history_obj.timestamp,
history_obj.track, history_obj.track,
)) ))

Loading…
Cancel
Save