|
| 1 | +from __future__ import print_function |
| 2 | +try: |
| 3 | + from idigbio.json_client import iDbApiJson |
| 4 | + import requests |
| 5 | + import shutil |
| 6 | + import os |
| 7 | + import sys |
| 8 | + import time |
| 9 | + import argparse |
| 10 | + import json |
| 11 | +except ImportError as e: |
| 12 | + print ("IMPORT ERROR (This exception is likely caused by a missing module): '{0}'".format(e)) |
| 13 | + raise SystemExit |
| 14 | + |
| 15 | +help_blob = """ |
| 16 | +
|
| 17 | + This script will print information about recordsets and their indexed pubdate, contacts. |
| 18 | +
|
| 19 | + Input list of recordset uuids is specified by putting them in a file and using --uuids-file option. |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | +argparser = argparse.ArgumentParser(description=help_blob, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 24 | +argparser.add_argument("-d", "--debug", default=False, action='store_true', |
| 25 | + help="enable debugging output") |
| 26 | +arg_group = argparser.add_mutually_exclusive_group(required=True) |
| 27 | +arg_group.add_argument("-u", "--uuid", |
| 28 | + help="single iDigBio recordset uuid to query") |
| 29 | +arg_group.add_argument("--uuids-file", |
| 30 | + help="file path containing list of iDigBio recordset uuids to query, one per line") |
| 31 | +args = argparser.parse_args() |
| 32 | + |
| 33 | +QUERY_TYPE = 'rq' |
| 34 | + |
| 35 | +debug_flag = args.debug |
| 36 | +if debug_flag: |
| 37 | + print () |
| 38 | + print ("** DEBUGGING ENABLED **") |
| 39 | + print () |
| 40 | + print () |
| 41 | + modulenames = set(sys.modules)&set(globals()) |
| 42 | + allmodules = [sys.modules[name] for name in modulenames] |
| 43 | + print ("Loaded modules...") |
| 44 | + for each_mod in allmodules: |
| 45 | + print (each_mod) |
| 46 | + print () |
| 47 | + |
| 48 | +def get_list_of_uuids_from_file(uuids_file): |
| 49 | + uuids_from_file = [] |
| 50 | + with open(uuids_file) as uf: |
| 51 | + for line in uf: |
| 52 | + uuids_from_file.append(line.strip()) |
| 53 | + return uuids_from_file |
| 54 | + |
| 55 | + |
| 56 | +def check_archive_status(url): |
| 57 | + try: |
| 58 | + r = requests.head(url, timeout=5) |
| 59 | + r.raise_for_status() |
| 60 | + return r.reason |
| 61 | + except: |
| 62 | + return "NO_ARCHIVE_AVAILABLE" |
| 63 | + |
| 64 | + |
| 65 | +def print_recordset_view_data(api,uuid): |
| 66 | + recordset_item_from_api = api.view("recordsets", uuid) |
| 67 | + the_important_data = [ |
| 68 | + recordset_item_from_api["uuid"], |
| 69 | + recordset_item_from_api["indexTerms"]["indexData"]["update"], |
| 70 | + recordset_item_from_api["indexTerms"]["name"], |
| 71 | + recordset_item_from_api["indexTerms"]["indexData"]["link"], |
| 72 | +# recordset_item_from_api[""], |
| 73 | + check_archive_status(recordset_item_from_api["indexTerms"]["indexData"]["link"]) |
| 74 | + ] |
| 75 | + print(*the_important_data, sep="\t") |
| 76 | + |
| 77 | + |
| 78 | +if args.uuids_file: |
| 79 | + uuid_list = get_list_of_uuids_from_file(args.uuids_file) |
| 80 | +else: |
| 81 | + uuid_list = [args.uuid] |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + |
| 85 | + api = iDbApiJson() |
| 86 | + |
| 87 | + print () |
| 88 | + for each in uuid_list: |
| 89 | + print_recordset_view_data(api,each) |
| 90 | + |
| 91 | + |
| 92 | + print () |
| 93 | + print ("***END***") |
0 commit comments