-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_devices.py
82 lines (75 loc) · 3.13 KB
/
upload_devices.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
73
74
75
76
77
78
79
80
81
82
"""
This code uploads data from CSV to CustomerIO
"""
import csv
import sys
import logging
from customerio import CustomerIO, CustomerIOException
from config import FILE, SITE_ID, API_KEY, LOG_FILE
try:
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO, filename=LOG_FILE)
except Exception as error_desc:
print('Can not open logfile.\r\n', error_desc)
sys.exit(13)
cio = CustomerIO(SITE_ID, API_KEY)
logger = logging.getLogger(__name__)
def print_progress_bar(iteration, total, prefix='', suffix='',
decimals=1, length=100, fill='█', print_end="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
print_end - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
p_bar = fill * filled_length + '-' * (length - filled_length)
print('\r%s |%s| %s%% %s' % (prefix, p_bar, percent, suffix), end=print_end)
# Print New Line on Complete
if iteration == total:
print()
def upload_devices(csv_file):
"""
Upload device token bindings to api
@params:
csv_files - Required : path to csv file (Int)
"""
try:
with open(csv_file, 'r', encoding="utf-8") as device_tokens:
data = csv.DictReader(device_tokens)
csv_lines = sum(1 for line in open(csv_file, encoding="utf-8")) - 1
logging.debug(data.fieldnames)
errors = 0
i = 0
print(csv_lines)
for row in data:
i = i + 1
try:
cio.add_device(customer_id=row['user_id'], device_id=row['registration_token'],
platform=row['operation_system'].lower())
except CustomerIOException as err_put:
logging.error("Failed to associate %s. Error: %s", row['user_id'], err_put)
errors = errors + 1
else:
logging.info("%s is associated with %s device id=%s",
row['user_id'], row['operation_system'], row['registration_token'])
print_progress_bar(i, csv_lines, prefix='Progress:', suffix=
f'Complete, line {i} of {csv_lines}, errors: {errors}, '
f'current id: {row["user_id"]}',
length=50)
except Exception as err_open:
print('Failed to open file: ', err_open)
def main():
"""
Main
"""
upload_devices(FILE)
if __name__ == "__main__":
main()