-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouchDB_connector.py
More file actions
34 lines (28 loc) · 1.02 KB
/
couchDB_connector.py
File metadata and controls
34 lines (28 loc) · 1.02 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
import subprocess
import json
from random import randint
import os
from dotenv import load_dotenv
load_dotenv()
# connect to CouchDB using API
def curl_request(url):
command = ['curl', url]
result = subprocess.run(command, capture_output=True, text=True)
return json.loads(result.stdout)
def gen_key():
value = randint(1, 9999)
return str(value)
# fetch data from CouchDB using API
def load_data():
tmp = []
all_dic = curl_request(database_path + "_all_docs")
for a in all_dic["rows"]:
api_dic = curl_request(database_path + a["id"])
tmp.append([gen_key(), api_dic["cusCode"], api_dic["point"], api_dic["feedback"], api_dic["knowFrom"], api_dic["timestamp"], api_dic["favorite_place"]])
return tmp
url = "http:///{}:{}@localhost:{}/{}/"
chDB_username = os.getenv("chDB_username")
chDB_password = os.getenv("chDB_password")
chDB_port = os.getenv("chDB_port")
chDB_tableName = os.getenv("chDB_tableName")
database_path = url.format(chDB_username, chDB_password, chDB_port, chDB_tableName)