-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathloader.py
More file actions
45 lines (32 loc) · 951 Bytes
/
loader.py
File metadata and controls
45 lines (32 loc) · 951 Bytes
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
#!/usr/local/bin/ python
import csv, json
import requests
import os, sys
from dotenv import load_dotenv
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csv_file, token, ep):
# Open a csv reader called DictReader
with open(csv_file, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for row in csvReader:
message = json.dumps({
"events": [ row ]
})
print(message)
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": "Bearer {0}".format(token)
}
url = "https://{0}.api.decodable.co{1}".format(os.getenv("ACCOUNT"), ep)
response = requests.post(url, headers=headers, data=message)
print(response.text)
if __name__ == "__main__":
csv_file = sys.argv[1]
token = sys.argv[2]
ep = sys.argv[3]
load_dotenv()
make_json(csv_file, token, ep)