Setup Artist table

Removed printing of library_stats.
This commit is contained in:
2018-06-04 21:52:30 -04:00
parent c17d318f7d
commit 549af96db7
3 changed files with 17 additions and 12 deletions

5
.gitignore vendored
View File

@@ -6,6 +6,5 @@ db.sqlite3
api-keys.sh api-keys.sh
Pipfile Pipfile
super-pass.txt *.txt
*.js graph.js
*.ini

View File

@@ -51,7 +51,7 @@ def parse_library(headers, tracks, library_stats, user):
library_stats['num_songs'] = offset + len(saved_tracks_response['items']) library_stats['num_songs'] = offset + len(saved_tracks_response['items'])
offset += limit offset += limit
calculate_genres_from_artists(headers, library_stats) calculate_genres_from_artists(headers, library_stats)
pprint.pprint(library_stats) # pprint.pprint(library_stats)
# }}} parse_library # # }}} parse_library #
@@ -185,6 +185,14 @@ def increase_artist_count(headers, artist_name, artist_id, library_stats):
else: else:
library_stats['artists'][artist_name]['count'] += 1 library_stats['artists'][artist_name]['count'] += 1
# add artist to database if new
if len(Artist.objects.filter(artist_id__contains=artist_id)) == 0:
new_artist = Artist(
artist_id=artist_id,
name=artist_name,
)
new_artist.save()
# }}} increase_artist_count # # }}} increase_artist_count #
# update_popularity_stats {{{ # # update_popularity_stats {{{ #
@@ -236,11 +244,6 @@ def get_track_info(track_dict, library_stats, sample_size):
year_released = track_dict['album']['release_date'].split('-')[0] year_released = track_dict['album']['release_date'].split('-')[0]
increase_nested_key('year_released', year_released, library_stats) increase_nested_key('year_released', year_released, library_stats)
# artist
# artist_names = [artist['name'] for artist in track_dict['artists']]
# for artist_name in artist_names:
# increase_nested_key('artists', artist_name)
# runtime # runtime
library_stats['total_runtime'] += float(track_dict['duration_ms']) / (1000 * 60) library_stats['total_runtime'] += float(track_dict['duration_ms']) / (1000 * 60)
@@ -263,6 +266,9 @@ def calculate_genres_from_artists(headers, library_stats):
for genre in artist_response['genres']: for genre in artist_response['genres']:
increase_nested_key('genres', genre, library_stats, artist_entry['count']) increase_nested_key('genres', genre, library_stats, artist_entry['count'])
# update genre for artist in database with top genre
Artist.objects.filter(artist_id=artist_entry['id']).update(genre=artist_response['genres'][0])
# }}} calculate_genres_from_artists # # }}} calculate_genres_from_artists #
# process_library_stats {{{ # # process_library_stats {{{ #

View File

@@ -164,9 +164,9 @@ def user_data(request):
} }
parse_library(headers, TRACKS_TO_QUERY, library_stats, user) parse_library(headers, TRACKS_TO_QUERY, library_stats, user)
processed_library_stats = process_library_stats(library_stats) processed_library_stats = process_library_stats(library_stats)
print("================================================") # print("================================================")
print("Processed data follows\n") # print("Processed data follows\n")
pprint.pprint(processed_library_stats) # pprint.pprint(processed_library_stats)
return render(request, 'spotifyvis/user_data.html', context) return render(request, 'spotifyvis/user_data.html', context)
# }}} user_data # # }}} user_data #