-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateCouchDB.py
72 lines (53 loc) · 1.81 KB
/
updateCouchDB.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
import couchdb
from couchdb import *
import json
import tarfile
from fileExplorer import fileExplorer
# Provide commentary!
def updateDB():
# Connect to server - no args means local connection
couch = couchdb.Server()
# Commentary:
print "Connected to CouchDB"
# Ask for database to connect to or create:
userInput = raw_input("Enter Database: ")
if userInput in couch: #Checks if entered db exists on server
db = couch[userInput]
else:
db = couch.create(userInput)
print userInput + " database opened."
sample = tarfile.open("C://Users/Saggu/Downloads/sample.tar")
sampleFiles = sample.getmembers()
for file in sampleFiles:
#Extract file from tarfile
exFile = sample.extractfile(file)
#Deserialize the extracted ExFileObject into JSON object
document = json.load(exFile)
print "Adding document " + document.get("id")
#Create '_id' field and set it equal to 'id' field in json document
document["_id"] = document.get("id")
document["testerspace"] = "WAZZUP"
# document["testerspace2"] = "Get at ME!"
#Add document to specified CouchDB db
if document["_id"] not in db:
_id, _rev = db.save(document)
else:
# del db[document.get("id")]
document["_rev"] = db[document["_id"]].get("_rev")
db.save(document)
# print document["_rev"]
print "Document %s added succesfully" %document["_id"]
db.commit()
# Useful for parsing through JSON docs
# for key, value in doc.iteritems():
# print key, value
print "Success"
def main():
updateDB()
# print "Sup?"
if __name__ == "__main__":
main()
#SOURCES
#docs.python.org
#philpapers.py by Jaimie Murdock
#StackOverflow