forked from philipmat/discogs-xml2db
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcouchdbexporter.py
52 lines (40 loc) · 1.29 KB
/
couchdbexporter.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
import couchdb
import urlparse
from jsonexporter import jsonizer
import json
class CouchDbExporter(object):
def __init__(self, server_url, data_quality=[]):
self.min_data_quality = data_quality
self.server = server_url
self.connect(server_url)
def connect(self, server_url):
u = urlparse.urlparse(server_url)
db_name = u.path.split('/')[1]
server = "%s://%s" % (u.scheme, u.netloc)
print 'Connecting to %s and database %s.' % (server, db_name)
couch = couchdb.Server(server)
self.db = couch[db_name]
def good_quality(self, what):
if len(self.min_data_quality):
return what.data_quality.lower() in self.min_data_quality
return True
def execute(self, what):
if not self.good_quality(what):
return
# have to convert it to json and back because
# on simple objects couchdb-python throws:
# TypeError: argument of type 'instance' is not iterable
# and on dicts:
# AttributeError: 'dict' object has no attribute 'read'
doc = json.loads(json.dumps(what, default=jsonizer))
self.db.save(doc)
def finish(self, completely_done = False):
pass
def storeLabel(self, label):
self.execute(label)
def storeArtist(self, artist):
self.execute(artist)
def storeRelease(self, release):
self.execute(release)
def storeMaster(self, master):
self.execute(master)