Created
July 30, 2022 11:47
-
-
Save kadirmalak/762e7931a774af259b87f043b9e929ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
#import pprint | |
#pp = pprint.PrettyPrinter(2) | |
# ----------- | |
f = open("my_collection.json", "r") | |
contents = f.read() | |
data = json.loads(contents) | |
song_list = data['response'] | |
#pp.pprint(songs) | |
# ----------- | |
max_number = -1 | |
most_listened_song = None | |
loop_count = 0 | |
for song in song_list: | |
loop_count = loop_count + 1 | |
print("=================== loop", loop_count, '===================') | |
print('checking', song['title'], 'from', song['artists'][0], '...') | |
if song['listen_count'] > max_number: | |
print('found greater number:', song['listen_count']) | |
max_number = song['listen_count'] | |
print('new max number:', max_number) | |
most_listened_song = song | |
print('new most listened song:', song['title'], 'with listen count:', song['listen_count']) | |
else: | |
print('skipping', song['title'], 'because it has a lower listen_count number') | |
print("=================== loop finished ===================") | |
print("---") | |
print("most listened song:", most_listened_song) | |
print(max_number) | |
# def get_listen_count(song): | |
# return song['listen_count'] | |
# | |
# | |
# print("=========") | |
# print(get_listen_count(most_listened_song)) | |
# print(most_listened_song['listen_count']) | |
# sorted_songs = sorted(song_list, key=get_listen_count, reverse=True) | |
# for song in sorted_songs: | |
# print(song) | |
# | |
# most_popular_song = sorted_songs[0] | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment