-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload-csv.py
executable file
·55 lines (41 loc) · 1.34 KB
/
upload-csv.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
#!/usr/bin/env python3
import csv
import json
import sys
import requests
import os
verbose = os.getenv('VERBOSE', False)
url = os.getenv('URL')
if len(sys.argv) != 2:
print("Usage: [URL=...] ./upload-csv.py file.csv")
sys.exit(1)
csv_path = sys.argv[1]
class Dumper:
MAX_RECORDS_IN_POST = os.getenv('MAX_RECORDS_IN_POST', 50)
def __init__(self, url, verbose):
self.url = url
self.verbose = verbose
self.data = []
def sumo_json(self, data):
rows = []
for record in data:
rows.append(json.dumps(record))
return "\n".join(rows)
def dump_maybe(self, force=False):
if len(self.data) >= Dumper.MAX_RECORDS_IN_POST or force:
payload = self.sumo_json(self.data)
if verbose:
print(payload)
if self.url:
print("Sending a payload of %d records to %s" % (len(self.data), self.url))
response = requests.post(url=self.url, data=payload)
print(response)
self.data = []
def process(self, path):
with open(path) as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
self.data.append(row)
self.dump_maybe()
self.dump_maybe(True)
Dumper(url, verbose).process(csv_path)