-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapped.py
More file actions
72 lines (54 loc) · 2.69 KB
/
Copy pathwrapped.py
File metadata and controls
72 lines (54 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import json
from collections import defaultdict
import os
import time
def load_playtimes():
if os.path.exists("song_playtimes.json"):
with open("song_playtimes.json", "r", encoding='utf-8') as f:
song_playtimes = defaultdict(int, json.load(f))
return song_playtimes
else:
return defaultdict(int)
def all_songs(file):
if input("Do you want to write out all songs? (y/n): ") == "y":
song_playtimes = load_playtimes()
file.write("\nAll songs:\n")
for song, duration in song_playtimes.items():
file.write(f"- {song} ({duration // 60} Minutes)\n")
def generate_wrapped_report():
song_playtimes = load_playtimes()
sorted_songs = sorted(song_playtimes.items(), key=lambda x: x[1], reverse=True)
top_songs = sorted_songs[:10]
with open("wrapped_report.txt", "w", encoding='utf-8') as f:
f.write("Your Top 10 Songs:\n")
for i, (song, duration) in enumerate(top_songs, 1):
f.write(f"{i}. {song} - {duration // 3600} Hours, {(duration % 3600) // 60} Minutes, {duration % 60} Seconds\n")
total_playtime = sum(song_playtimes.values())
total_minutes = total_playtime // 60
total_hours = total_minutes // 60
f.write("\nTotal Playtime:\n")
f.write(f"{total_hours} Hours and {total_minutes % 60} Minutes\n")
f.write("\nSongs Played Less Than 5 Minutes (Top 5):\n")
less_than_5_minutes = [(song, duration) for song, duration in song_playtimes.items() if duration < 300]
top_5_less_than_5_minutes = sorted(less_than_5_minutes, key=lambda x: x[1], reverse=True)[:5]
for song, duration in top_5_less_than_5_minutes:
f.write(f"- {song} ({duration // 60} Minutes)\n")
least_played_songs = sorted_songs[-5:]
f.write("\nYour Least Played 5 Songs:\n")
for i, (song, duration) in enumerate(reversed(least_played_songs), 1):
f.write(f"{i}. {song} - {duration // 60} Minutes (aka. {duration % 60} Seconds)\n")
unique_songs_count = len(song_playtimes)
f.write("\nUnique Songs Played:\n")
f.write(f"{unique_songs_count} unique songs\n")
f.write("\n-----------------------------------------------------\n")
all_songs(f)
f.write("\n-----------------------------------------------------\n")
f.write("Generated by https://github.com/Tamino1230/bTM-Wrapped")
if __name__ == "__main__":
if os.path.exists("song_playtimes.json"):
generate_wrapped_report()
else:
print("You need to put \"song_playtimes.json\" in the same directory as this script.")
print("Exiting in 5 seconds...")
time.sleep(5)
print("Finished! with your Wrapped")