-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
executable file
·102 lines (79 loc) · 3.25 KB
/
stats.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
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
"""A tool to run stats over your iTunes library."""
import collections
import logging
import os
import os.path
import analysis
import pytunes
LOG_FORMAT = '%(message)s'
logger = logging.getLogger(__name__)
def print_incompletely_rated_albums(albums):
print 'Incompletely rated albums:'
incompletely_rated_albums = analysis.find_incompletely_rated_albums(albums)
albums_sorted_by_completeness = sorted(
incompletely_rated_albums, key=lambda a: a.rating_completeness)
for album in albums_sorted_by_completeness:
print unicode(album).encode('utf-8')
print
def print_best_albums(albums, n=50):
print '%d best rated albums (by average rating):' % n
completely_rated_albums = analysis.find_completely_rated_albums(albums)
albums_sorted_by_rating = sorted(
completely_rated_albums, key=lambda a: a.avg_rating)
for album in reversed(albums_sorted_by_rating[-n:]):
print unicode(album).encode('utf-8')
print
def print_worst_albums(albums, n=50):
print '%d worst rated albums (by average rating):' % n
completely_rated_albums = analysis.find_completely_rated_albums(albums)
albums_sorted_by_rating = sorted(
completely_rated_albums, key=lambda a: a.avg_rating)
for album in albums_sorted_by_rating[:n]:
print unicode(album).encode('utf-8')
print
def print_duplicates(tracks, tolerated_time_difference=10):
print 'Duplicates:'
duplicates = analysis.find_duplicates(tracks, tolerated_time_difference)
sorted_duplicates = sorted(
duplicates, key=lambda t: '%s-%s' % (t.artist, t.name))
for track in sorted_duplicates:
print unicode(track).encode('utf-8')
def print_favorite_bands(songs, n=50):
print '%d favorite bands (by number of tracks in the library):' % n
counter = collections.Counter()
counter.update([t.artist for t in songs])
for artist, count in counter.most_common(n):
print '%s - %d' % (unicode(artist).encode('utf-8'), count)
print
def print_best_bands(songs, min_rating, n=50):
print '%d best bands (by number of highly rated tracks):' % n
good_tracks = filter(lambda t: t.rating >= min_rating, songs)
counter = collections.Counter()
counter.update([t.artist for t in good_tracks])
for artist, count in counter.most_common(n):
print '%s - %d' % (unicode(artist).encode('utf-8'), count)
print
def main():
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
logger.info('Opening iTunes library...')
lib = pytunes.Library()
logger.info('Loading all tracks...')
songs = filter(lambda t: not t.podcast, lib.tracks)
albums = list(pytunes.Album.group_tracks_into_albums(songs))
albums = filter(lambda a: len(a.tracks) > 7, albums)
htgt = filter(lambda a: a.album == 'Here Today Gone Tomorrow', albums)[0]
dryrun = True
n = 10
min_rating = 80 # 4 stars.
min_good_tracks = 4
keep_good_tracks = True
tolerated_time_difference = 10
print_best_albums(albums, n)
print_best_bands(songs, min_rating, n)
print_favorite_bands(songs, n)
print_worst_albums(albums, n)
print_incompletely_rated_albums(albums)
print_duplicates(songs, tolerated_time_difference)
if __name__ == '__main__':
main()