From c4277ab0b451e74fdd437e19f09d800a4dfd8d95 Mon Sep 17 00:00:00 2001 From: Kevin Mok Date: Sat, 8 Dec 2018 05:42:37 -0500 Subject: [PATCH] Setup history log on nzxt-arch --- api/models.py | 2 +- api/utils.py | 8 +++++--- login/views.py | 2 +- reset_db.sh | 8 ++++---- update-history.service | 14 ++++++++++++++ update-history.sh | 4 +++- 6 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 update-history.service diff --git a/api/models.py b/api/models.py index 5d6b4cf..01c69ce 100644 --- a/api/models.py +++ b/api/models.py @@ -46,7 +46,7 @@ class Track(models.Model): id = models.CharField(primary_key=True, max_length=MAX_ID) artists = models.ManyToManyField(Artist, blank=True) - year = models.PositiveSmallIntegerField(null=True) + year = models.PositiveSmallIntegerField(blank=True, null=True) popularity = models.PositiveSmallIntegerField() runtime = models.PositiveSmallIntegerField() name = models.CharField(max_length=200) diff --git a/api/utils.py b/api/utils.py index 6ac7995..c37b4a9 100644 --- a/api/utils.py +++ b/api/utils.py @@ -5,6 +5,7 @@ import os import json from django.db.models import Count, F, Max +from django.db import IntegrityError from django.http import JsonResponse from django.core import serializers from django.utils import timezone @@ -81,7 +82,8 @@ def save_track_obj(track_dict, artists, user_obj): else: # check if track is simple or full, simple Track object won't have year # if 'album' in track_dict: - try: + if 'release_date' in track_dict['album']: + # try: new_track = Track.objects.create( id=track_dict['id'], year=track_dict['album']['release_date'].split('-')[0], @@ -89,8 +91,8 @@ def save_track_obj(track_dict, artists, user_obj): runtime=int(float(track_dict['duration_ms']) / 1000), name=track_dict['name'], ) - # else: - except KeyError: + else: + # except (IntegrityError, KeyError) as e: new_track = Track.objects.create( id=track_dict['id'], popularity=int(track_dict['popularity']), diff --git a/login/views.py b/login/views.py index 0479fa2..226bc1c 100644 --- a/login/views.py +++ b/login/views.py @@ -15,7 +15,7 @@ from .utils import * TIME_FORMAT = '%Y-%m-%d-%H-%M-%S' TRACKS_TO_QUERY = 200 -AUTH_SCOPE = ['user-library-read', 'user-read-recently-played',] +AUTH_SCOPE = ['user-library-read', 'user-read-recently-played', ] # index {{{ # diff --git a/reset_db.sh b/reset_db.sh index b4805d6..9405ebf 100755 --- a/reset_db.sh +++ b/reset_db.sh @@ -1,15 +1,15 @@ # check if in virtual environment # https://stackoverflow.com/questions/15454174/how-can-a-shell-function-know-if-it-is-running-within-a-virtualenv/15454916 -python -c 'import sys; print(sys.real_prefix)' 2>/dev/null && INVENV=1 || INVENV=0 +# python -c 'import sys; print(sys.real_prefix)' 2>/dev/null && INVENV=1 || INVENV=0 # INVENV=$(python -c 'import sys; print ("1" if hasattr(sys, "real_prefix") else "0")') # if $INVENV is 1, then in virtualenv # echo $INVENV -if [ $INVENV -eq 1 ]; then +# if [ $INVENV -eq 1 ]; then rm login/migrations/0* api/migrations/0* sudo -u postgres psql -f reset_db.sql python manage.py makemigrations login api - python manage.py migrate + python manage.py migrate --fake python manage.py runserver -fi +# fi diff --git a/update-history.service b/update-history.service new file mode 100644 index 0000000..ba2797a --- /dev/null +++ b/update-history.service @@ -0,0 +1,14 @@ +[Unit] +Description=Update my Spotify listening history. +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=300 +User=kevin +ExecStart=/usr/bin/bash /home/kevin/coding/spotify-lib-vis/src/update-history.sh + +[Install] +WantedBy=multi-user.target diff --git a/update-history.sh b/update-history.sh index 5f2c10d..a9508d4 100755 --- a/update-history.sh +++ b/update-history.sh @@ -1 +1,3 @@ -/home/kevin/coding/spotify-lib-vis/bin/python /home/kevin/coding/spotify-lib-vis/src/manage.py update-history >> /home/kevin/coding/spotify-lib-vis/src/api/management/commands/update-history.log +#!/bin/bash + +/home/kevin/coding/spotify-lib-vis/bin/python /home/kevin/coding/spotify-lib-vis/src/manage.py update-history >> /home/kevin/coding/spotify-lib-vis/src/api/management/commands/update-history.log 2>&1