-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsaveGroups.py
More file actions
69 lines (56 loc) · 2.02 KB
/
saveGroups.py
File metadata and controls
69 lines (56 loc) · 2.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
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
import requests
import csv
import sys
import json
email = "email"
password = "password"
shortname = "shortname"
if len(sys.argv) != 2:
print("python3 saveGroups.py <openmhzuploadedfile>")
sys.exit(0)
if email == "email" or password == "password" or shortname == "shortname":
print("Please modify the email, password and shortname before running this script.")
sys.exit(1)
headers = {"content-type": "application/json"}
loginURL = "https://account.openmhz.com/login"
data = {"email": email, "password": password}
r = requests.post(loginURL, headers=headers, data=json.dumps(data))
response = r.json()
if response["success"] == True:
cookies = {"sessionId": r.headers["set-cookie"][10:]}
groups = {}
with open(sys.argv[1]) as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
for row in readCSV:
dec = row[0]
group_name = row[5]
if group_name in groups:
groups[group_name].append(dec)
else:
groups[group_name] = [dec]
print(
"Adding the following groups to the system with shortname {}:".format(shortname)
)
url = "https://admin.openmhz.com/groups/{}".format(shortname)
# url = "http://localhost:9123"
for groupName, groupKeys in groups.items():
headers = {"content-type": "application/json"}
requestData = {
"shortname": shortname,
"groupName": groupName,
"talkgroups": json.dumps(groupKeys),
}
r_group = requests.post(
url, headers=headers, data=json.dumps(requestData), cookies=cookies
)
response_group = r_group.json()
if response_group["success"] == True:
print("Uploaded group: '{}' successfully".format(groupName))
else:
print(
"ERROR! Could not upload group: {}, reponse: {}".format(
groupName, response_group
)
)
else:
print("Invalid username or password, response: \n{}".format(r.json()))