#!/usr/bin/python3 # Demo to report the status of observations to NEOfixer. - A. R. Gibbs # From: https://neofixer.arizona.edu/api-info # Standard modules. import sys import json import argparse import requests # Version of this program. sVersion = '1.0 2022-06-14' # Default NEOfixer site name. You can change this to yours. sSite = 'I52' # Default NEOfixer account API key, which you should change to yours. sKey = 'Your key here' # Prepare for reliably retrieving data from the NEOfixer API. def SetupAPI (): global oHttpSession oRetry = requests.packages.urllib3.util.retry.Retry (total = 4, status_forcelist = [429, 500, 502, 503, 504], backoff_factor = 1) oAdapter = requests.adapters.HTTPAdapter (max_retries = oRetry) oHttpSession = requests.Session () oHttpSession.mount ("https://", oAdapter) # API call to NEOfixer. def API (sURL): global oHttpSession # Try to send to NEOfixer API. ret = False try: s = oHttpSession.get (sURL, timeout = 15).text except Exception as e: print ('Unable to talk to NEOfixer: ' + str (e)) else: # Get reply. In this case we expect JSON. try: # Convert from JSON. d = json.loads (s) ret = d['result'] except: # If failed then see if NEOfixer reported an error. try: print ('NEOfixer says: "' + d['error']['message'] + '"') except: print ('Unable to parse reply from NEOfixer.') return ret # Setup command line arguments, help, and defaults. oParse = argparse.ArgumentParser (description = 'Report your observation status of objects to NEOfixer. Below units are enclosed in () and defaults are enclosed in []. Version: ' + sVersion, add_help = False) oSelect = oParse.add_argument_group ('Required') oSelect.add_argument ('object', metavar='OBJECT', nargs = '+', help='Object name or list of object names.') oSelect.add_argument ('-r', dest = 'status', default='', help = "Status to report for the object(s). Case insensitive. One of 'may_observe', 'will_observe', 'observing', 'observed', 'found', 'reported', 'not_found', or 'canceled'.") oSelect = oParse.add_argument_group ('Optional') oSelect.add_argument ('-s', dest = 'site', default = sSite, help = 'NEOfixer site name. [' + sSite + ']') oSelect.add_argument ('-k', dest = 'key', default = sKey, help = 'API key from NEOfixer account profile. [set in program]') oSelect.add_argument ('-t', dest = 'time', type = float, default = -1, help = 'Time of the action (JD) or \'now\'. [now]') oSelect = oParse.add_argument_group ('Other') oSelect.add_argument ('-h', '--help', action='help', help = 'Display this help and exit. [False]') oSelect.add_argument ('-T', dest = 'test', action='store_true', help = 'Test. The request will be validated by the servers and a reply will be returned but no action will be taken. [False]') # Parse command line arguments. oArgs = oParse.parse_args () # If only testing then append 'test=1' to request. if oArgs.test: sTest = '&test=1' else: sTest = '' print ("NEOfixer reporting for site " + oArgs.site + ".", file=sys.stderr) # Setup API interface once. SetupAPI () # For each object specified report the status to NEOfixer. bAll = True for sObject in oArgs.object: bSet = API ('https://neofixerapi.arizona.edu/report/?key=' + oArgs.key + '&site=' + oArgs.site + '&status=' + oArgs.status + '&object=' + sObject + sTest) bAll &= bSet if bSet: print (sObject + ' = ' + oArgs.status) else: print (sObject + " couldn't be reported.") # Say success if the status was set for all the objects, else failure. if bAll: print ('Success.') else: print ('Failure.') if oArgs.test: print ('Only a test.') exit (0 if bAll else 1)