diff --git a/.gitignore b/.gitignore index 73bb973..d7ce0a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.pyc +*.log db.sqlite3 *.bak .idea/ diff --git a/api/management/commands/update-history.py b/api/management/commands/update-history.py new file mode 100644 index 0000000..0e48296 --- /dev/null +++ b/api/management/commands/update-history.py @@ -0,0 +1,10 @@ +from django.core.management.base import BaseCommand, CommandError +from api.utils import parse_history +from login.models import User + +class Command(BaseCommand): + help = 'Update history for users who requested it' + + def handle(self, *args, **options): + user_id = "polarbier" + parse_history(User.objects.get(id=user_id).secret) diff --git a/api/utils.py b/api/utils.py index 9f24864..6ac7995 100644 --- a/api/utils.py +++ b/api/utils.py @@ -13,13 +13,14 @@ from . import views from login.models import User from pprint import pprint from dateutil.parser import parse +from datetime import datetime HISTORY_ENDPOINT = 'https://api.spotify.com/v1/me/player/recently-played' # }}} imports # -console_logging = True -# console_logging = False +# console_logging = True +console_logging = False artists_genre_processed = 0 features_processed = 0 @@ -365,8 +366,7 @@ def parse_history(user_secret): params=payload).json()['items'] # pprint(history_response) - if console_logging: - tracks_processed = 0 + tracks_processed = 0 for track_dict in history_response: # don't associate history track with User, not necessarily in their @@ -379,9 +379,9 @@ def parse_history(user_secret): track_artists, None) history_obj = save_history_obj(user_obj, parse(track_dict['played_at']), track_obj) + tracks_processed += 1 if console_logging: - tracks_processed += 1 print("Added history track #{}: {}".format( tracks_processed, history_obj,)) @@ -391,5 +391,8 @@ def parse_history(user_secret): # TODO: update track genres from History relation # update_track_genres(user_obj) + print("Scanned {} history tracks for user {} at {}.".format( + tracks_processed, user_obj.id, datetime.now())) + # }}} get_history # diff --git a/api/views.py b/api/views.py index bd6df30..17e20b4 100644 --- a/api/views.py +++ b/api/views.py @@ -115,59 +115,7 @@ def parse_library(request, user_secret): # }}} parse_library # -# # parse_history {{{ # - -# def parse_history(request, user_secret): - # """Scans user's listening history and stores the information in a - # database. - - # :user_secret: secret for User object who's library is being scanned. - # :returns: None - # """ - - # user_obj = User.objects.get(secret=user_secret) - # payload = {'limit': str(USER_TRACKS_LIMIT)} - # last_time_played = History.objects.filter(user=user_obj).aggregate(Max('timestamp'))['timestamp__max'] - # if last_time_played is not None: - # payload['after'] = last_time_played.isoformat() - # artist_genre_queue = [] - # user_headers = get_user_header(user_obj) - # history_response = requests.get(HISTORY_ENDPOINT, - # headers=user_headers, - # params=payload).json()['items'] - # # pprint(history_response) - - # if console_logging: - # tracks_processed = 0 - - # for track_dict in history_response: - # # don't associate history track with User, not necessarily in their - # # library - # # track_obj, track_created = save_track_obj(track_dict['track'], - # # track_artists, None) - # track_artists = save_track_artists(track_dict['track'], artist_genre_queue, - # user_headers) - # track_obj, track_created = save_track_obj(track_dict['track'], - # track_artists, None) - # history_obj = save_history_obj(user_obj, parse(track_dict['played_at']), - # track_obj) - - # if console_logging: - # tracks_processed += 1 - # print("Added history track #{}: {}".format( - # tracks_processed, history_obj,)) - - # if len(artist_genre_queue) > 0: - # add_artist_genres(user_headers, artist_genre_queue) - - # # TODO: update track genres from History relation - # # update_track_genres(user_obj) - - # return render(request, 'graphs/logged_in.html', get_user_context(user_obj)) - -# # }}} get_history # - -# parse_history {{{ # +# parse_history_request {{{ # def parse_history_request(request, user_secret): """Request function to call parse_history. Scans user's listening history diff --git a/graphs/views.py b/graphs/views.py index 8c410d6..4b5278a 100644 --- a/graphs/views.py +++ b/graphs/views.py @@ -71,8 +71,9 @@ class HistoryList(ExportMixin, SingleTableView): def get_export_filename(self, export_format): user_id = self.request.session['user_id'] - timestamp = strftime("%m%d%Y-%H%M") - return "{}.{}".format("-".join((user_id, timestamp)), export_format) + # timestamp = strftime("%m%d%Y-%H%M") + # return "{}.{}".format("-".join((user_id, timestamp)), export_format) + return "{}.{}".format(user_id, export_format) def create_export(self, export_format): export_exclude = ('id', 'user', 'track', 'track_name', 'artists', diff --git a/update-history.sh b/update-history.sh new file mode 100755 index 0000000..5f2c10d --- /dev/null +++ b/update-history.sh @@ -0,0 +1 @@ +/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