import json
artistTracksPlayedCounter = {}
artistUniqueTracksPlayedCounter = {}
artistCumulativeTimePlayed = {}
songPlayCounter = {}
REPORT_SIZE = 30
with open('StreamingHistory_music_0.json') as f:
data = json.load(f)
for song in data:
# example record
# "endTime" : "2023-12-01 13:36",
# "artistName" : "The Vaccines",
# "trackName" : "Teenage Icon",
# "msPlayed" : 184240
artist = song['artistName']
track = song['trackName']
timePlayed = song['msPlayed']
datePlayed = song['endTime']
# only consider songs that were played after Jan 1 2024
if datePlayed < "2024-01-01":
continue
if artist in artistTracksPlayedCounter:
artistTracksPlayedCounter[artist] += 1
else:
artistTracksPlayedCounter[artist] = 1
if artist in artistCumulativeTimePlayed:
artistCumulativeTimePlayed[artist] += timePlayed
else:
artistCumulativeTimePlayed[artist] = timePlayed
if track in songPlayCounter:
songPlayCounter[track] += 1
else:
songPlayCounter[track] = 1
if artist in artistUniqueTracksPlayedCounter:
if track not in artistUniqueTracksPlayedCounter[artist]:
artistUniqueTracksPlayedCounter[artist].append(track)
else:
artistUniqueTracksPlayedCounter[artist] = [track]
# print the top 10 artists by number of tracks played
print("Top %d artists by number of tracks played" % REPORT_SIZE)
sortedArtists = sorted(artistTracksPlayedCounter.items(), key=lambda x: x[1], reverse=True)
for i in range(REPORT_SIZE):
print(sortedArtists[i])
# print the top 10 artists by cumulative time played
print("\n\n\nTop %d artists by cumulative time played" % REPORT_SIZE)
sortedArtists = sorted(artistCumulativeTimePlayed.items(), key=lambda x: x[1], reverse=True)
for i in range(REPORT_SIZE):
# convert time to minutes
time = sortedArtists[i][1] / 60000
# round to 2 decimal places
time = round(time, 2)
print(sortedArtists[i][0], time)
# print the top 10 songs by number of times played
print("\n\n\nTop %d songs by number of times played" % REPORT_SIZE)
sortedSongs = sorted(songPlayCounter.items(), key=lambda x: x[1], reverse=True)
for i in range(REPORT_SIZE):
print(sortedSongs[i])
# print the top 10 artists by number of unique tracks played
print("\n\n\nTop %d artists by number of unique tracks played" % REPORT_SIZE)
sortedArtists = sorted(artistUniqueTracksPlayedCounter.items(), key=lambda x: len(x[1]), reverse=True)
for i in range(REPORT_SIZE):
# just print the artist name and the count of unique tracks
print(sortedArtists[i][0], len(sortedArtists[i][1]))
Click Run or press shift + ENTER to run code