-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.py
67 lines (54 loc) · 2.17 KB
/
scanner.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
import stagger
import os
import re
import codecs
import pickle
import hashlib
import base64
from stagger.id3 import *
def scan(basepath, logfunc):
file_matcher = re.compile(r".*\.(mp3)")
image_dict = {}
tracks = []
i = 0
for dirpath, dirnames, filenames in os.walk(basepath):
for filename in filenames:
if file_matcher.match(filename):
filepath = os.path.join(dirpath, filename)
try:
tag = stagger.read_tag(filepath)
albumart = {}
if 'APIC' in tag._frames:
apic_frame = tag._frames['APIC']
for art in apic_frame:
m = hashlib.md5()
m.update(art.data)
hashdata = m.digest()
hash = base64.b16encode(hashdata).decode("ASCII")
if not hash in image_dict:
image_dict[hash] = {
"mimetype": art.mime,
"data": art.data
}
albumart[art.type] = hash
tracks.append({
"index": i,
"title": tag.title,
"track": tag.track,
"artist": tag.artist,
"album": tag.album,
"disc": tag.disc if tag.disc > 0 else 1,
"album_artist": tag.album_artist if tag.album_artist != "" else tag.artist,
"date": tag.date,
"filepath": filepath,
"album_art": albumart
})
i = i + 1
if i % 100 == 0:
logfunc("{0} tracks".format(i))
except stagger.errors.NoTagError as err:
logfunc("No tag found in {0}".format(filepath))
logfunc("Scan complete - {0} tracks found".format(i))
return (tracks, image_dict)
#with open("astarael.db","wb") as f:
# pickle.dump((tracks, image_dict), f)