You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3.11
"""Lookup DMR users on radioid.net
This script fetches user information from radioid.net API and
prints them to stdout.
Created: maybe back in 2021
Last modified: 2023-01-02 00:07:34+0100
Usage:
------
With arguments
./call.py [callsign]|[dmrid]....
Without arguments - but enter them when the script asks you for them
./call.py
Examples:
---------
./call.py oe7drt oe%kbc
DMRID CALLSIGN FIRSTNAME CITY COUNTRY
2327180 OE7DRT Dominic Laengenfeld Austria
2327212 OE7DRT Dominic Laengenfeld Austria
2321001 OE1KBC Kurt Wien Austria
2321002 OE1KBC Kurt Wien Austria
2321003 OE1KBC Kurt Wien Austria
2328023 OE8KBC Kurt Saurachberg Austria
./call.py 2327212
DMRID CALLSIGN FIRSTNAME CITY COUNTRY
2327180 OE7DRT Dominic Laengenfeld Austria
2327212 OE7DRT Dominic Laengenfeld Austria
"""
import re # regex
import sys
import requests
def printFormatted(data: dict) -> None:
"""Formats and prints to stdout.
Expects data coming in as dict, returns nothing but
the text printed out to stdout."""
print("{:>7.8} {:<8.8} {:12.12} {:12.12} {:13.13}".format(
str(data['id']),
data['callsign'],
data['fname'],
data['city'],
data['country']))
def getFromCallsign(url: str, call: str) -> None:
"""Maps the given callsign to a DMRID
Uses the given base-url and callsign and fetches the
connected DMRIDs. Loops over the result and executes
printFormatted to print the results to stdout."""
r = requests.get(url + "/api/dmr/user/?callsign={}".format(call))
for ids in range(0, r.json()['count']):
printFormatted(r.json()['results'][ids])
def main():
"""main function
Runs if script is run by itself."""
if len(sys.argv) <= 1:
# Check if arguments were given, if not, let the user
# input some here
arguments = []
userinput = input("No arguments found, enter them now: ")
words = userinput.split()
for word in words:
arguments.append(word)
else:
# If arguments were given, take them and move on
arguments = sys.argv[1:]
# Using these regex pattern to match agains valid DMRIDs
# and callsigns. The callsign pattern was taken from
# <https://gist.github.com/JoshuaCarroll/f6b2c64992dfe23feed49a117f5d1a43>
# and slighty modifed to also allow the percent sign (%) used by
# used by radioid.net as wildmask.
dmrid_userid_patt = re.compile("^[0-9]{7}$")
dmrid_rep_patt = re.compile("^[0-9]{6}$")
# dmrid_other_patt = re.compile("^[0-9]{0,5}$")
call_patt = re.compile("^[a-zA-Z0-9%]{1,3}[0123456789%][a-zA-Z0-9%]{0,2}[a-zA-Z%]$")
baseurl = 'https://www.radioid.net'
print(" DMRID CALLSIGN FIRSTNAME CITY COUNTRY")
for arg in arguments:
if dmrid_userid_patt.match(arg):
# A valid DMRID was found, so we have to lookup the
# callsign and we may get more DMRIDs for this OM
# Valid means 7 chars long, all numbers. We do not know
# yet if the id really exists.
r = requests.get(baseurl + '/api/dmr/user/?id={}'.format(arg))
if r.status_code == 200:
# Only fetch more DMRIDs if the first one exists,
# otherwise we would try to run code on an non
# existing variable
getFromCallsign(baseurl, r.json()['results'][0]['callsign'])
elif dmrid_rep_patt.match(arg):
# A valid repeater ID was found. Valid means, technically
# correct -> 6 characters long, all numbers
print("{} looks like a repeater. Skipping for now.".format(arg))
# elif dmrid_other_patt.match(arg):
# # Print a warning for numbers less than 6 characters
# print("{} is not a valid dmr id!".format(arg))
elif call_patt.match(arg):
getFromCallsign(baseurl, arg)
else:
print('{} is an invalid value'.format(arg))
if __name__ == "__main__":
main()