-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvad-import-patients.py
46 lines (32 loc) · 1.58 KB
/
tvad-import-patients.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
import pymongo
from pymongo import MongoClient
import requests
import logging
if __name__ == '__main__' :
# set logging level
logging.getLogger().setLevel(logging.INFO)
# connect to mongodb
client = MongoClient()
db = client.tvad
collection = db.patients
# get patient data
result = requests.get('http://tvassistdem-backend.istc.cnr.it/patients')
# check result
if result.status_code != 200 :
# something went wrong
raise RuntimeError('GET /patients/2/data {}'.format(result.status_code))
# check patients
for patient in result.json() :
# document to insert
doc = { "_id" : patient['id'], "name" : patient['name'].encode('utf-8').strip(), "surname" : patient['surname'].encode('utf-8').strip()}
try :
# save document into the collection
collection.insert_one(doc)
logging.info('Importing {} {} {}'.format(patient['id'], patient['name'].encode('utf-8').strip(), patient['surname'].encode('utf-8').strip()))
except :
# document already exist, do update fields
query = {"_id": patient['id']}
updates = { "$set" : { "name" : patient['name'].encode('utf-8').strip(), "surname" : patient['surname'].encode('utf-8').strip()} }
# update database
collection.update_one(query, updates)
logging.warn('Updating {} {} {}'.format(patient['id'], patient['name'].encode('utf-8').strip(), patient['surname'].encode('utf-8').strip()))