-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeetsstatistics.py
336 lines (287 loc) · 10.7 KB
/
beetsstatistics.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import sqlite3
import yaml
import os
import argparse
import os.path
from enum import Enum
LOSSY_FORMATS = ["mp3", "aac", "ogg"]
LOSSLESS_FORMATS = ["flac", "wav"]
class DBNotFoundError(Exception):
pass
class DBQueryError(Exception):
pass
class Album:
def __repr__(self):
return f"ID: {self.id}, Title: {self.title}, Tracks: {self.tracks}/{self.track_total} ({self.complete_percentage}%), Album artist: {self.album_artist}, Genre: {self.genre}, Year: {self.year} ({self.original_year}), Album Art: {self.album_cover}"
class AlbumSort(Enum):
ARTIST = "albumartist_sort"
ALBUM = "album"
YEAR = "year"
class BeetsStatistics:
def __init__(self, db_file: str):
self.db_file = db_file
self.connection = None
def get_db_file_name(self):
if self.db_file is not None:
return self.db_file
db_config_key = "library"
# TODO Catch exception if file does not exist
config_file_name = os.path.expanduser("~/.config/beets/config.yaml")
with open(config_file_name, "r") as file:
config = yaml.safe_load(file)
return os.path.expanduser(config[db_config_key])
def get_db_connection(self):
if self.connection is not None:
return self.connection
db_file_name = self.get_db_file_name()
if not db_file_name:
raise DBNotFoundError
if not os.path.isfile(db_file_name):
raise DBNotFoundError(db_file_name)
self.connection = sqlite3.connect(db_file_name)
# Set row factory so that named dicts are returned
self.connection.row_factory = sqlite3.Row
return self.connection
def close(self):
if self.connection:
self.connection.close()
def get_albums_from_db(self, sort_by: AlbumSort = AlbumSort.ARTIST):
connection = self.get_db_connection()
cursor = connection.cursor()
res = cursor.execute(
"""select
i.album_id,
i.album,
count(i.track) as tracks,
max(i.tracktotal) as tracktotal,
ifnull(round(count(i.track)/ cast(max(i.tracktotal) as float), 2) * 100, 0) as complete,
a.*
from
albums a
left join
items i on
i.album_id = a.id
where
album_id is not null
group by
i.album_id
order by a.{} asc
""".format(sort_by.value)
)
albums = res.fetchall()
cursor.close()
result = []
for album in albums:
return_album = Album()
return_album.id = album["album_id"]
return_album.title = album["album"]
return_album.tracks = album["tracks"]
return_album.track_total = album["tracktotal"]
return_album.complete_percentage = album["complete"]
return_album.album_artist = album["albumartist"]
return_album.genre = album["genre"]
return_album.year = album["year"]
return_album.original_year = album["original_year"]
# id |artpath|added |albumartist |albumartist_sort|albumartist_credit|albumartists|albumartists_sort |albumartists_credit|album |genre |style|discogs_albumid|discogs_artistid|discogs_labelid|year|month|day|disctotal|comp|mb_albumid|mb_albumartistid|albumtype|albumtypes|label |barcode |mb_releasegroupid |release_group_title |asin|catalognum|script|language|country|albumstatus|albumdisambig|releasegroupdisambig|rg_album_gain|rg_album_peak|r128_album_gain|original_year|original_month|original_day|
result.append(return_album)
return result
""" By album
"""
def get_genre_count(self, limit: int = 0):
try:
cursor = self.get_db_connection().cursor()
res = cursor.execute(
"""select
case
when a.genre = '' then "n/a"
else ifnull(a.genre, "n/a")
end as genre,
count(1) as count
from
albums a
group by
a.genre
order by
2 desc
{}""".format("LIMIT {}".format(limit) if limit > 0 else "")
)
genres = res.fetchall()
res = cursor.execute(
"""select
count(1)
from
albums"""
)
count = res.fetchone()[0]
cursor.close()
return genres, count
except sqlite3.Error as e:
raise DBQueryError from e
def get_artist_stats(self, limit: int = 0):
try:
cursor = self.get_db_connection().cursor()
res = cursor.execute(
"""select
count(1) as track_count,
i.artist
from
items i
group by
i.artist
having
track_count > 1
order by
1 desc,
i.artist_sort asc
{}""".format("LIMIT {}".format(limit) if limit > 0 else "")
)
artists = res.fetchall()
cursor.close()
return artists
except sqlite3.Error as e:
raise DBQueryError from e
def get_track_count(self):
query = """select
count(1) as count
from
items"""
track_count = self._query_one_value(query)
return track_count
def _query_one_value(self, query: str):
try:
cursor = self.get_db_connection().cursor()
res = cursor.execute(query)
value = res.fetchone()
cursor.close()
if value is not None:
return value[0]
else:
return None
except sqlite3.Error as e:
raise DBQueryError from e
def get_album_count(self):
query = """select count(1) as count from albums"""
return self._query_one_value(query)
def get_playback_length(self):
query = """SELECT sum(length) FROM items i"""
return self._query_one_value(query)
def get_file_size(self):
query = """SELECT
sum(i.bitrate * i."length" / 8) AS SIZE
FROM
items i;"""
return self._query_one_value(query)
def get_avg_bpm(self):
query = """SELECT
round(avg(i.bpm))
FROM
items i
WHERE
i.bpm > 0"""
return self._query_one_value(query)
def map_file_format_to_lossy(self, formats):
lossy = 0
lossless = 0
unknown = 0
for file in formats:
if file[0].lower() in LOSSLESS_FORMATS:
lossless += file[1]
elif file[0].lower() in LOSSY_FORMATS:
lossy += file[1]
else:
unknown += file[1]
return lossless, lossy, unknown
def get_track_formats(self):
try:
cursor = self.get_db_connection().cursor()
query = """SELECT
i.format,
count(1) AS count
FROM
items i
GROUP BY
i.format;"""
res = cursor.execute(query)
results = res.fetchall()
cursor.close()
lossless, lossy, unknown = self.map_file_format_to_lossy(results)
print(
"Lossless: {}, lossy: {}, unknown: {}".format(lossless, lossy, unknown)
)
return results, lossless, lossy, unknown
except sqlite3.Error as e:
raise DBQueryError from e
def get_track_decades(self):
try:
cursor = self.get_db_connection().cursor()
query = """SELECT
i.YEAR / 10 * 10 as decade,
count(1) AS count
FROM
items i
GROUP BY
1
ORDER BY
1 ASC;"""
res = cursor.execute(query)
results = res.fetchall()
cursor.close()
count = self._query_one_value("""SELECT count(1) from items i;""")
return results, count
except sqlite3.Error as e:
raise DBQueryError from e
def get_track_quality(self, limit: int = 0):
try:
cursor = self.get_db_connection().cursor()
query = """SELECT
bitrate / 10000*10000 / 1000 as bitrate,
count(1) AS count
FROM
items i
GROUP BY
1
ORDER BY
1 desc
{}""".format("LIMIT {}".format(limit) if limit > 0 else "")
res = cursor.execute(query)
results = res.fetchall()
cursor.close()
count = self._query_one_value("""SELECT count(1) from items i;""")
return results, count
except sqlite3.Error as e:
raise DBQueryError from e
def get_genre_decade_heatmap(self):
try:
cursor = self.get_db_connection().cursor()
query = """select
genre,
YEAR / 10 * 10 as decade,
count(1) as count
from albums
where genre != '' and year > 0
group by 1,2
order by 1,2 asc"""
res = cursor.execute(query)
results = res.fetchall()
cursor.close()
return results
except sqlite3.Error as e:
raise DBQueryError from e
def get_album_cover_path(self, album_id: int):
query = """select artpath from albums where id = {}""".format(album_id)
path = self._query_one_value(query)
print(query)
print("{} -> {}".format(album_id, path))
return path
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="beets-statistics")
parser.add_argument("-d", "--beets-db", required=False)
args = parser.parse_args()
bs = BeetsStatistics(db_file=args.beets_db)
print(bs.get_db_file_name())
for album in bs.get_albums_from_db():
print(album.title, album.complete_percentage) # Complete percentage
print("-" * 140)
for artist in bs.get_artist_stats():
print("{} - {}".format(artist["track_count"], artist["artist"]))
bs.close()