Browse Source

Merge branch 'history'

master
Kevin Mok 6 years ago
parent
commit
b5152f988f
Signed by: Kevin-Mok GPG Key ID: AEA75288DC135CF5
  1. 2
      api/models.py
  2. 8
      api/utils.py
  3. 2
      login/views.py
  4. 8
      reset_db.sh
  5. 14
      update-history.service
  6. 4
      update-history.sh

2
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)

8
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']),

2
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 {{{ #

8
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

14
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

4
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
Loading…
Cancel
Save