forked from yl3im/motobm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
121 lines (90 loc) · 3.95 KB
/
users.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
import argparse
import json
from os.path import exists
import requests
import urllib3
import unicodedata
parser = argparse.ArgumentParser(description='Generate MOTOTRBO contacts file from RadioId.net data')
parser.add_argument('-f', '--force', action='store_true',
help='Forcibly download user list even if it exists locally')
parser.add_argument('-c', '--country', required=True, help='Country name'
'As per RadioID.net')
args = parser.parse_args()
radioid_url = 'https://radioid.net/api/dmr/user/?'
radioid_file = 'radioid_' + args.country + '.json'
existing = {}
def download_file():
if not exists(radioid_file) or args.force:
download_ext_path = f'country={args.country}' if args.country else print('Country name is required.') and exit(1)
download_full_path = radioid_url + download_ext_path
print(f'Downloading from {download_full_path}')
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
response = requests.get(download_full_path, verify=False)
response.raise_for_status()
with open(radioid_file, 'wb') as file:
file.write(response.content)
print(f'Saved to {radioid_file}')
def process_contacts():
f = open(radioid_file, 'r')
json_list = json.loads(f.read())
idx = 0
channels = ''
for item in json_list['results']:
idx += 1
channels += format_channel(item)
f.close()
print(f'Processed {idx} records.')
write_zone_file(args.country,
f'''<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<config>
<category name="PCRContacts">
{channels}
</category>
</config>''')
def format_channel(item):
global existing
city = unicodedata.normalize('NFKD', item['city']).encode('ascii', 'ignore').decode('utf-8').strip()
name = unicodedata.normalize('NFKD', item['fname']).encode('ascii', 'ignore').decode('utf-8').strip()
surname = unicodedata.normalize('NFKD', item['surname']).encode('ascii', 'ignore').decode('utf-8').strip()
# max len 16 chars
contact_name = f'{item["callsign"]} {name} {surname}'.strip()[:16]
if not contact_name in existing: existing[contact_name] = 0
existing[contact_name] += 1
if existing[contact_name] > 1:
# cut contact name to 14 chars and add space and number
contact_name = f'{contact_name[:14]} {existing[contact_name]}'
return f'''<set name="PCRContacts" alias="{contact_name}">
<field name="ContactName">{contact_name}</field>
<collection name="DigitalCalls">
<set name="DigitalCalls" index="0" key="PRIVCALL">
<field name="DU_CALLALIAS">{contact_name}</field>
<field name="DU_CALLLSTID">{item["id"]}</field>
<field name="DU_ROUTETYPE" Name="Regular">REGULAR</field>
<field name="DU_CALLPRCDTNEN">False</field>
<field name="DU_RINGTYPE" Name="No Style">NOSTYLE</field>
<field name="DU_TXTMSGALTTNTP" Name="Repetitive">REPETITIVE</field>
<field name="DU_CALLTYPE" Name="Private Call">PRIVCALL</field>
<field name="DU_OVCMCALL">False</field>
<field name="DU_CALLTYPEPART2">0</field>
<field name="DU_UKPOTCFLG">False</field>
<field name="DU_RVRTPERS_Zone" Name="None">NONE</field>
<field name="DU_RVRTPERS" Name="Selected">SELECTED</field>
<field name="CallType">Digital Calls-Private Call</field>
<field name="PeudoCallId">{item["id"]}</field>
</set>
</collection>
<collection name="CapacityPlusCalls" />
<collection name="PhoneCalls" />
<field name="Comments"></field>
</set>
'''
def write_zone_file(zone_alias, contents):
zone_file_name = zone_alias + ".xml"
zone_file = open(zone_file_name, "wt")
zone_file.write(contents)
zone_file.close()
print(f'Contact file "{zone_file_name}" written\n')
if __name__ == '__main__':
download_file()
process_contacts()