Refactor saving artists to track
Exact same code in parse_history, parse_library and import_history.
This commit is contained in:
27
api/utils.py
27
api/utils.py
@@ -1,7 +1,6 @@
|
|||||||
# imports {{{ #
|
# imports {{{ #
|
||||||
import requests
|
import requests
|
||||||
import math
|
import math
|
||||||
import pprint
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@@ -10,7 +9,9 @@ from django.http import JsonResponse
|
|||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from .models import *
|
from .models import *
|
||||||
|
from . import views
|
||||||
from login.models import User
|
from login.models import User
|
||||||
|
from pprint import pprint
|
||||||
|
|
||||||
# }}} imports #
|
# }}} imports #
|
||||||
|
|
||||||
@@ -182,7 +183,7 @@ def add_artist_genres(headers, artist_objs):
|
|||||||
artist_ids = str.join(",", [artist_obj.id for artist_obj in artist_objs])
|
artist_ids = str.join(",", [artist_obj.id for artist_obj in artist_objs])
|
||||||
params = {'ids': artist_ids}
|
params = {'ids': artist_ids}
|
||||||
artists_response = requests.get('https://api.spotify.com/v1/artists/',
|
artists_response = requests.get('https://api.spotify.com/v1/artists/',
|
||||||
headers=headers,
|
headers=headers,
|
||||||
params={'ids': artist_ids},
|
params={'ids': artist_ids},
|
||||||
).json()['artists']
|
).json()['artists']
|
||||||
for i in range(len(artist_objs)):
|
for i in range(len(artist_objs)):
|
||||||
@@ -235,14 +236,28 @@ def get_artists_in_genre(user, genre, max_songs):
|
|||||||
|
|
||||||
# }}} get_artists_in_genre #
|
# }}} get_artists_in_genre #
|
||||||
|
|
||||||
def create_artist_for_track(artist_dict):
|
def save_track_artists(track_dict, artist_genre_queue, user_headers):
|
||||||
"""TODO: Docstring for create_artist_for_track.
|
""" Update artist info before creating Track so that Track object can
|
||||||
|
reference Artist object.
|
||||||
|
|
||||||
:artist_dict: TODO
|
:track_dict: TODO
|
||||||
:returns: None
|
:returns: None
|
||||||
|
|
||||||
"""
|
"""
|
||||||
pass
|
track_artists = []
|
||||||
|
for artist_dict in track_dict['artists']:
|
||||||
|
artist_obj, artist_created = Artist.objects.get_or_create(
|
||||||
|
id=artist_dict['id'],
|
||||||
|
name=artist_dict['name'],)
|
||||||
|
# only add/tally up artist genres if new
|
||||||
|
if artist_created:
|
||||||
|
artist_genre_queue.append(artist_obj)
|
||||||
|
if len(artist_genre_queue) == views.ARTIST_LIMIT:
|
||||||
|
add_artist_genres(user_headers, artist_genre_queue)
|
||||||
|
artist_genre_queue[:] = []
|
||||||
|
track_artists.append(artist_obj)
|
||||||
|
|
||||||
|
return track_artists
|
||||||
|
|
||||||
def get_user_header(user_obj):
|
def get_user_header(user_obj):
|
||||||
"""Returns the authorization string needed to make an API call.
|
"""Returns the authorization string needed to make an API call.
|
||||||
|
|||||||
86
api/views.py
86
api/views.py
@@ -60,7 +60,8 @@ def parse_library(request, user_secret):
|
|||||||
# create this obj so loop runs at least once
|
# create this obj so loop runs at least once
|
||||||
saved_tracks_response = [0]
|
saved_tracks_response = [0]
|
||||||
# scan until reach num_tracks or no tracks left if scanning entire library
|
# scan until reach num_tracks or no tracks left if scanning entire library
|
||||||
while (TRACKS_TO_QUERY == 0 or offset < TRACKS_TO_QUERY) and len(saved_tracks_response) > 0:
|
while ((TRACKS_TO_QUERY == 0 or offset < TRACKS_TO_QUERY) and
|
||||||
|
len(saved_tracks_response) > 0):
|
||||||
payload['offset'] = str(offset)
|
payload['offset'] = str(offset)
|
||||||
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
|
saved_tracks_response = requests.get('https://api.spotify.com/v1/me/tracks',
|
||||||
headers=user_headers,
|
headers=user_headers,
|
||||||
@@ -70,25 +71,8 @@ def parse_library(request, user_secret):
|
|||||||
tracks_processed = 0
|
tracks_processed = 0
|
||||||
|
|
||||||
for track_dict in saved_tracks_response:
|
for track_dict in saved_tracks_response:
|
||||||
# add artists {{{ #
|
track_artists = save_track_artists(track_dict['track'], artist_genre_queue,
|
||||||
|
user_headers)
|
||||||
# update artist info before track so that Track object can reference
|
|
||||||
# Artist object
|
|
||||||
track_artists = []
|
|
||||||
for artist_dict in track_dict['track']['artists']:
|
|
||||||
artist_obj, artist_created = Artist.objects.get_or_create(
|
|
||||||
id=artist_dict['id'],
|
|
||||||
name=artist_dict['name'],)
|
|
||||||
# only add/tally up artist genres if new
|
|
||||||
if artist_created:
|
|
||||||
artist_genre_queue.append(artist_obj)
|
|
||||||
if len(artist_genre_queue) == ARTIST_LIMIT:
|
|
||||||
add_artist_genres(user_headers, artist_genre_queue)
|
|
||||||
artist_genre_queue = []
|
|
||||||
track_artists.append(artist_obj)
|
|
||||||
|
|
||||||
# }}} add artists #
|
|
||||||
|
|
||||||
track_obj, track_created = save_track_obj(track_dict['track'],
|
track_obj, track_created = save_track_obj(track_dict['track'],
|
||||||
track_artists, user_obj)
|
track_artists, user_obj)
|
||||||
|
|
||||||
@@ -158,29 +142,14 @@ def parse_history(request, user_secret):
|
|||||||
tracks_processed = 0
|
tracks_processed = 0
|
||||||
|
|
||||||
for track_dict in history_response:
|
for track_dict in history_response:
|
||||||
# add artists {{{ #
|
|
||||||
|
|
||||||
# update artist info before track so that Track object can reference
|
|
||||||
# Artist object
|
|
||||||
track_artists = []
|
|
||||||
for artist_dict in track_dict['track']['artists']:
|
|
||||||
artist_obj, artist_created = Artist.objects.get_or_create(
|
|
||||||
id=artist_dict['id'],
|
|
||||||
name=artist_dict['name'],)
|
|
||||||
# only add/tally up artist genres if new
|
|
||||||
if artist_created:
|
|
||||||
artist_genre_queue.append(artist_obj)
|
|
||||||
if len(artist_genre_queue) == ARTIST_LIMIT:
|
|
||||||
add_artist_genres(user_headers, artist_genre_queue)
|
|
||||||
artist_genre_queue = []
|
|
||||||
track_artists.append(artist_obj)
|
|
||||||
|
|
||||||
# }}} add artists #
|
|
||||||
|
|
||||||
# don't associate history track with User, not necessarily in their
|
# don't associate history track with User, not necessarily in their
|
||||||
# library
|
# library
|
||||||
track_obj, track_created = save_track_obj(track_dict['track'],
|
# track_obj, track_created = save_track_obj(track_dict['track'],
|
||||||
track_artists, None)
|
# 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, history_created = History.objects.get_or_create(
|
history_obj, history_created = History.objects.get_or_create(
|
||||||
user=user_obj,
|
user=user_obj,
|
||||||
timestamp=parse(track_dict['played_at']),
|
timestamp=parse(track_dict['played_at']),
|
||||||
@@ -276,6 +245,8 @@ def import_history(request, upload_id):
|
|||||||
:returns: None
|
:returns: None
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# setup {{{ #
|
||||||
|
|
||||||
headers = ['timestamp', 'track_id']
|
headers = ['timestamp', 'track_id']
|
||||||
upload_obj = HistoryUpload.objects.get(id=upload_id)
|
upload_obj = HistoryUpload.objects.get(id=upload_id)
|
||||||
user_headers = get_user_header(upload_obj.user)
|
user_headers = get_user_header(upload_obj.user)
|
||||||
@@ -290,6 +261,7 @@ def import_history(request, upload_id):
|
|||||||
row = next(csv_reader)
|
row = next(csv_reader)
|
||||||
last_row = False
|
last_row = False
|
||||||
while not last_row:
|
while not last_row:
|
||||||
|
|
||||||
# if Track.objects.filter(id__exact=row[1]).exists():
|
# if Track.objects.filter(id__exact=row[1]).exists():
|
||||||
history_obj_info = {}
|
history_obj_info = {}
|
||||||
for i in range(len(headers)):
|
for i in range(len(headers)):
|
||||||
@@ -298,9 +270,14 @@ def import_history(request, upload_id):
|
|||||||
row = next(csv_reader)
|
row = next(csv_reader)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
last_row = True
|
last_row = True
|
||||||
|
|
||||||
|
# }}} setup #
|
||||||
|
|
||||||
history_obj_info_lst.append(history_obj_info)
|
history_obj_info_lst.append(history_obj_info)
|
||||||
rows_read += 1
|
rows_read += 1
|
||||||
if (rows_read % TRACKS_LIMIT == 0) or last_row:
|
if (rows_read % TRACKS_LIMIT == 0) or last_row:
|
||||||
|
# get tracks_response {{{ #
|
||||||
|
|
||||||
track_ids_lst = [info['track_id'] for info in history_obj_info_lst]
|
track_ids_lst = [info['track_id'] for info in history_obj_info_lst]
|
||||||
# print(len(track_ids_lst))
|
# print(len(track_ids_lst))
|
||||||
track_ids = ','.join(track_ids_lst)
|
track_ids = ','.join(track_ids_lst)
|
||||||
@@ -309,29 +286,16 @@ def import_history(request, upload_id):
|
|||||||
headers=user_headers,
|
headers=user_headers,
|
||||||
params=payload).json()['tracks']
|
params=payload).json()['tracks']
|
||||||
responses_processed = 0
|
responses_processed = 0
|
||||||
|
|
||||||
|
# }}} get tracks_response #
|
||||||
|
|
||||||
for track_dict in tracks_response:
|
for track_dict in tracks_response:
|
||||||
# add artists {{{ #
|
# create History obj {{{ #
|
||||||
|
|
||||||
# update artist info before track so that Track object can reference
|
|
||||||
# Artist object
|
|
||||||
track_artists = []
|
|
||||||
for artist_dict in track_dict['artists']:
|
|
||||||
artist_obj, artist_created = Artist.objects.get_or_create(
|
|
||||||
id=artist_dict['id'],
|
|
||||||
name=artist_dict['name'],)
|
|
||||||
# only add/tally up artist genres if new
|
|
||||||
if artist_created:
|
|
||||||
artist_genre_queue.append(artist_obj)
|
|
||||||
if len(artist_genre_queue) == ARTIST_LIMIT:
|
|
||||||
add_artist_genres(user_headers, artist_genre_queue)
|
|
||||||
artist_genre_queue = []
|
|
||||||
track_artists.append(artist_obj)
|
|
||||||
|
|
||||||
# }}} add artists #
|
|
||||||
|
|
||||||
# don't associate history track with User, not necessarily in their
|
# don't associate history track with User, not necessarily in their
|
||||||
# library
|
# library
|
||||||
|
track_artists = save_track_artists(track_dict, artist_genre_queue,
|
||||||
|
user_headers)
|
||||||
track_obj, track_created = save_track_obj(track_dict, track_artists, None)
|
track_obj, track_created = save_track_obj(track_dict, track_artists, None)
|
||||||
|
|
||||||
timestamp = parse(history_obj_info_lst[responses_processed]['timestamp'])
|
timestamp = parse(history_obj_info_lst[responses_processed]['timestamp'])
|
||||||
@@ -351,6 +315,8 @@ def import_history(request, upload_id):
|
|||||||
print("Processed row #{}: {}".format(
|
print("Processed row #{}: {}".format(
|
||||||
(rows_read - TRACKS_LIMIT) + responses_processed, history_obj,))
|
(rows_read - TRACKS_LIMIT) + responses_processed, history_obj,))
|
||||||
responses_processed += 1
|
responses_processed += 1
|
||||||
|
|
||||||
|
# }}} create History obj #
|
||||||
|
|
||||||
history_obj_info_lst = []
|
history_obj_info_lst = []
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user