-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathget_dmrmarc.py
64 lines (45 loc) · 1.26 KB
/
get_dmrmarc.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# KD4Z
# dmr-marc decided to stop offering CSV output format, so json here we come
from __future__ import print_function
import requests
import sys
def noCommas(field):
try:
if field is None:
return ""
field = field.encode('ascii', 'ignore').decode('ascii')
field = field.replace(","," ").strip()
except (UnicodeEncodeError, TypeError, AttributeError):
pass
return field
def dmrmarc_json():
myreq = requests.get('http://www.dmr-marc.net/cgi-bin/trbo-database/datadump.cgi?table=users&format=json')
if len(myreq.text) < 1000:
print("dmr-marc site error:", myreq.text, file=sys.stderr)
return
dmrmarc = myreq.json()
for user in dmrmarc['users']:
try:
sir = noCommas(user["surname"])
fullname = noCommas(user["name"])
if len(sir) > 0:
fullname = fullname + " " + sir
line = "{0},{1},{2},{3},{4},,{5}".format(
user["radio_id"],
noCommas(user["callsign"]),
fullname,
noCommas(user["city"]),
noCommas(user["state"]),
noCommas(user["country"]))
print(line)
except (UnicodeEncodeError, TypeError, AttributeError):
sys.exc_clear()
def main():
try:
dmrmarc_json()()
except Exception:
pass
if __name__ == '__main__':
main()