#!/usr/bin/env python
# Michael Jack
# Based on this example: https://github.com/duk3luk3/onion-py/blob/master/bin/onion.py

# Dependencies:
#               onion-py https://github.com/duk3luk3/onion-py/
#               pip install onionpy

from onion_py.manager import Manager

# Functions
def sizeof_fmt(num, suffix='/s'):
    for unit in ['','KB','MB','GB','TB','PB','EB','ZB']:
        if abs(num) < 1024.0:
            return "%3.1f%s%s" % (num, unit, suffix)
        num /= 1024.0
    return "%.1f%s%s" % (num, 'Yi', suffix)
# Source: https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
# Suports: all currently known binary prefixes
        # negative and positive numbers
        # numbers larger than 1000 Yobibytes
        # arbitrary units (maybe you like to count in Gibibits!)

            # ! - - - Main - - - ! #
manager = Manager()

# Variables
fp = "*** YOUR_RELAY_FINGERPRINT ***"
fields = 'nickname,running,observed_bandwidth'

d = manager.query('details',lookup=fp, limit=1, type='relay', fields=fields)
if len(d.relays) < 1:
   print('not_found,{},...'.format(fp))
else:
   r = d.relays[0]
   print("{} | Running: {} | Observed Bandwidth {}".format(r.nickname,r.running,sizeof_fmt(r.bandwidth[2])))
