-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmongo_audio_print_db.py
More file actions
57 lines (45 loc) · 1.9 KB
/
mongo_audio_print_db.py
File metadata and controls
57 lines (45 loc) · 1.9 KB
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 pymongo
from audio_search_dbs import AudioPrintsDB, DuplicateKeyError
class MongoAudioPrintDB(AudioPrintsDB):
def __init__(self):
self.client = self.get_client()
self.fingerprints_collection = self.client.audioprintsDB.fingerprints
self.songs_collection = self.client.audioprintsDB.songs
pass
def get_db_fingerprint_song_id(self, fingerprint):
return fingerprint['songID']
def get_db_fingerprint_offset(self, fingerprint):
return fingerprint['offset']
def insert_one_fingerprint(self, fingerprint):
try:
self.fingerprints_collection.insert_one(fingerprint)
except pymongo.errors.DuplicateKeyError:
raise DuplicateKeyError
return
def insert_many_fingerprints(self, fingerprints):
self.fingerprints_collection.insert_many(fingerprints)
return
def find_one_song(self, song):
return self.songs_collection.find_one(song)
def get_next_song_id(self):
most_recent_song = self.songs_collection.find_one({}, sort=[(u"_id", -1)])
if most_recent_song is not None:
new_id = most_recent_song['_id'] + 1
else:
new_id = 0
return new_id
def insert_one_song(self, song):
try:
insert_song_result = self.songs_collection.insert_one(song)
except pymongo.errors.DuplicateKeyError:
# increment the key and try again
song['_id'] += 1
return self.insert_one_song(song)
return insert_song_result.inserted_id
def find_db_fingerprints_with_hash_key(self, fingerprint):
return self.fingerprints_collection.find({'hash': fingerprint['hash']}, projection={"_id": 0, "hash": 0})
def get_client(self):
print("getting client...")
client = pymongo.MongoClient('mongodb://localhost:27017')
print("got client", flush=True)
return client