Webservice script for using pergola through the Web
# -*- coding: utf-8 -*- | |
""" | |
Pergola webservice client | |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
More details at: https://pergola.crg.eu | |
Usage: python pergola-webservice.py myconfig.json myoutput.zip | |
""" | |
import requests | |
import json | |
import sys | |
import os | |
# For debugging | |
import pprint | |
pp = pprint.PrettyPrinter(depth=6) | |
def read_config( configfile ): | |
import json | |
config = None | |
with open(configfile) as data_file: | |
config = json.load(data_file) | |
return config | |
# Read Example JSON file | |
config = None | |
# Default output file | |
outputfile = "output.zip" | |
if len( sys.argv ) > 1 : | |
configfile = sys.argv[1] | |
config = read_config( configfile ) | |
if len( sys.argv ) > 2 : | |
outputfile = sys.argv[2] | |
if config : | |
if 'url' in config : | |
url = config['url'] | |
else : | |
url = "https://pergola.crg.eu" | |
files = [] | |
# http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file | |
if 'files' in config: | |
for key in config['files'] : | |
if type(config['files'][key]) is list : | |
for f in config['files'][key] : | |
files.append( ( key, ( f['filename'], open( f['content-file']), f['content-type'] ) ) ) | |
else : | |
files.append( ( key, ( config['files'][key]['filename'], open( config['files'][key]['content-file']), config['files'][key]['content-type'] ) ) ) | |
# pp.pprint( files ) | |
response = requests.post( url+"/submit", data=config['params'], files=files, verify=False ) | |
if response and response.content and response.status_code == 200 : | |
content = json.loads( response.content ) | |
if content["status"] == "OK" : | |
if content["session"] : | |
download_url = url+"/download?session="+content["session"]+"&out=output.zip&format=zipdir" | |
response_down = requests.get( download_url, allow_redirects=True, verify=False ) | |
open( outputfile, 'wb' ).write( response_down.content ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment