-
Notifications
You must be signed in to change notification settings - Fork 0
/
yearly.py
90 lines (73 loc) · 2.41 KB
/
yearly.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from datetime import datetime
import json
from operator import itemgetter
import os
import sys
RESULTS_TO_SHOW = 3
def bold(s):
return f"\033[1m{s}\033[0m"
def analyze(data: dict):
artist_playcounts = {}
track_playcounts = {}
platforms = {}
total_ms_played = 0
for scrobble in data:
try:
artist_playcounts[scrobble["artist"]] += 1
except KeyError:
artist_playcounts[scrobble["artist"]] = 1
try:
track_playcounts[scrobble["artist"] + " - " + scrobble["track"]] += 1
except KeyError:
track_playcounts[scrobble["artist"] + " - " + scrobble["track"]] = 1
try:
platforms[scrobble["platform"]] += 1
except KeyError:
platforms[scrobble["platform"]] = 1
total_ms_played += scrobble["ms_played"]
print()
print("Total scrobbles:", bold(len(data)))
print("Total listening time:", bold(int(total_ms_played / 1000 / 60)), "minutes")
print()
print(bold("Top artists:"))
for i, (item, value) in enumerate(
sorted(artist_playcounts.items(), key=itemgetter(1), reverse=True)[:RESULTS_TO_SHOW],
start=1,
):
print(f"#{i:>3} \t {value} plays \t {item}")
print()
print(bold("Top tracks:"))
for i, (item, value) in enumerate(
sorted(track_playcounts.items(), key=itemgetter(1), reverse=True)[:RESULTS_TO_SHOW],
start=1,
):
print(f"#{i:>3} \t {value} plays \t {item}")
print()
def main(folder):
files = sorted(
[f"{folder}/{file}" for file in filter(lambda f: f.endswith(".json"), os.listdir(folder))]
)
all_data = []
for file in files:
with open(file, "r", encoding="utf-8") as f:
data = json.load(f)
all_data += data
years = {}
for item in all_data:
year = datetime.fromtimestamp(item["timestamp"]).year
try:
years[year].append(item)
except KeyError:
years[year] = [item]
print()
for year, data in sorted(years.items()):
print("-------", bold(year), "-------")
analyze(data)
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] in ["-h", "--help"]:
print("Get your yearly stats from the converted json files generated by convert.py")
print()
print("Usage:")
print("\tpython yearly.py [path/to/folder]")
quit(1)
main(sys.argv[1])