-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmbfingerprint.py
57 lines (51 loc) · 1.48 KB
/
mbfingerprint.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
import os
import argparse
import acoustid
import config
import time
import json
RETRIES = 5
def fingerprint(thefile):
attempt = 0
success = False
while not success and attempt < RETRIES:
try:
matches = acoustid.match(config.ACOUSTID_KEY, thefile)
success = True
except acoustid.WebServiceError as e:
time.sleep(1)
attempt += 1
if success:
return list(matches)
return []
def save_cache(data):
json.dump(data, open("lookupcache.json", "w"))
def main(thedir):
if os.path.exists("lookupcache.json"):
data = json.load(open("lookupcache.json"))
else:
data = {}
total = 0
matches = 0
for root, dirs, files in os.walk(thedir):
for f in files:
fname = os.path.join(root, f)
print(fname)
if fname not in data:
matchdata = fingerprint(fname)
data[fname] = matchdata
else:
matchdata = data[fname]
if matchdata:
matches += 1
total += 1
if total % 10 == 0:
print("{}: matched {}/{} fingerprints".format(total, matches, total))
save_cache(data)
save_cache(data)
print("matched {}/{} files with fingerprint".format(matches, total))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("audiodir", type=str)
args = parser.parse_args()
main(args.audiodir)