From 5fbf647db1905b104830d4ef7718156f5ca58108 Mon Sep 17 00:00:00 2001 From: ErNis Date: Wed, 11 Dec 2024 10:46:59 +0200 Subject: [PATCH] userlist fetcher from radioid --- users.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 users.py diff --git a/users.py b/users.py new file mode 100644 index 0000000..a6ecee6 --- /dev/null +++ b/users.py @@ -0,0 +1,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 user files 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_channels(): + 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''' + + + {channels} + +''') + +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''' + {contact_name} + + + {contact_name} + {item["id"]} + REGULAR + False + NOSTYLE + REPETITIVE + PRIVCALL + False + 0 + False + NONE + SELECTED + Digital Calls-Private Call + {item["id"]} + + + + + + +''' + + +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_channels()