Mercurial > repos > stevecassidy > alveoimport
view alveo_item_list_importer.py @ 1:6fef3489d97c draft
planemo upload commit 0fca33c3b7285bd31f6c7380393d08bbdad4e4d6
author | stevecassidy |
---|---|
date | Mon, 15 Aug 2016 23:45:46 -0400 |
parents | bfe39bd252df |
children | 7b6021997b8e |
line wrap: on
line source
from __future__ import print_function import json import argparse import pyalveo import sys API_URL = 'https://app.alveo.edu.au' # TODO: export constants to a separate module def parser(): parser = argparse.ArgumentParser(description="Retrieves Alveo Item Lists") parser.add_argument('--api_key', required=True, action="store", type=str, help="Alveo API key") parser.add_argument('--output', required=True, action="store", type=str, help="Path to output file") return parser.parse_args() # TODO: export common function to helper module def get_item_lists(api_key): client = pyalveo.Client(api_key=api_key, api_url=API_URL) return client.get_item_lists() def write_table(item_lists, filename): with open(filename, 'w') as outfile: for list_set in item_lists.itervalues(): for item_list in list_set: outfile.write("%s (%d)\t%s\n" % (item_list['name'], item_list['num_items'], item_list['item_list_url'])) def main(): args = parser() try: api_key = open(args.api_key, 'r').read().strip() item_lists = get_item_lists(api_key) if item_lists: write_table(item_lists, args.output) except Exception as e: print("ERROR: " + str(e), file=sys.stderr) sys.exit(1) if __name__ == '__main__': main()