import sys import urllib import urllib2 import md5 import json def run(client, session, server, args): if len(args) == 0: print "route required" return route = args[0] print "route(" + route + ") session(" + session + ")" if "download" not in route: headers = {"Content-type": "application/json", "Accept": "text/plain", "Cookie": "RS_API=" + str(session)} client.request("GET", "/api" + route, None, headers) response = client.getresponse() print "HTTP Status/Reason: " + str(response.status) + " / " + response.reason print "Response header: " + str(response.getheaders()) print "Response:" print response.read() response.read() else: headers = {"Content-type": "application/json", "Accept-Encoding": "gzip, deflate", "Cookie": "RS_API=" + str(session)} url = "https://portal.royaltyshare.com/api" + route req = urllib2.Request(url, headers=headers) response = urllib2.urlopen(req) print "Response header: " + str(response.info()) contentType = response.info()['Content-Type'].split(';')[0] print "#### Content Type(" + contentType + ")" if contentType == 'application/octet-stream': localfile = response.info()['Content-Disposition'].split('filename=')[-1].replace('"','').replace(';','') print "Downloaded filename: " + localfile with open( localfile, "wb") as f: f.write(response.read()) else: print "No statement(s) found"