Skip to content

Commit

Permalink
Update formatting annd add most listened song
Browse files Browse the repository at this point in the history
  • Loading branch information
yussufbiyik committed Jul 12, 2024
1 parent 4f55b7f commit d68e351
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions listening_time_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# startTime = time.time()

total = 0 # A variable to collect the total listening time

allSongs = [] # A list to collect all the songs that have been listened to
try: # Going through each StreamingHistory file and gathering listening time data for each song
for path in pathlib.Path('your-data').iterdir():
# Look for both naming conventions of the file to take the new naming convention into account
Expand All @@ -13,8 +13,11 @@
for item in json.loads(currentFile.read()):
# Look for both naming conventions of the file to take the new naming convention into account
total += int(item.get("msPlayed", item.get("ms_played", 0)))
allSongs.append({"Track Name": item.get("trackName", item.get("track_name", "Unknown song")), "Listening Time": int(item.get("msPlayed", item.get("ms_played", 0)))/60000 })
currentFile.close()
# endTime = time.time()
finally: # Calculating total listening time in minutes, hours and days
print(f'Your total listening time is {total/60000:.2f} minutes which is {total/3600000:.2f} hours which is {total/86400000:.2f} days!')
print(f'Your total listening time is:\n\t\x1b[1;36m{total/60000:.2f} minutes\033[0m which is \x1b[1;36m{total/3600000:.2f} hours\033[0m which is \x1b[1;36m{total/86400000:.2f} days\033[0m!')
allSongs.sort(key=lambda song: song["Listening Time"], reverse=True)
print(f'Your most listened to song is:\n\t\x1b[1;36m{allSongs[0]["Track Name"]}\033[0m with \x1b[1;36m{allSongs[0]["Listening Time"]:.2f} minutes\033[0m of listening time!')
# print("This script took {:.5f} seconds to run.".format(endTime - startTime))

0 comments on commit d68e351

Please sign in to comment.