Eubrewnet Wiki
Eubrewnet Documentation
Eubrewnet Documentation
#! /usr/bin/python ################################################################################## # Module: connect_example.py # # Usage: $>connect_example.py -h # # Comments: # # - Connects to Eubrewnet and returns the result of a get function to the # # database. Executes between two different days to check system perfomance # # Author: Bentorey Hernandez Cruz (2015-01-01) # ################################################################################## import urllib2, base64,json import sys, getopt import datetime import os user = 'user' password = 'password' #getdataUrl = 'http://rbcce.ciai.inm.es/eubrewnet/getdata' getdataUrl = 'http://rbcce.aemet.es/eubrewnet/data/get' ################################################################################## # Function: usage # # Comments: Show information message # ################################################################################## def usage(): print '************************************************************************' print 'Usage:' print ' $>./connect_example.py [options]' print 'Options:' print ' -b, --brewerid: Brewer (DEFAULT = 157)' print ' -d, --date: Start date in YYYY-MM-DD format (DEFAULT = 2015-01-01)' print ' -e, --enddate: End date in YYYY-MM-DD format (DEFAULT = 2015-01-01)' print ' -f, --function: Function to be executed (DEFAULT = DSS)' print ' -h, --help: Show this help' print '************************************************************************' ################################################################################## # Function: main # # Comments: Parses the options that have been selected in command line or set # # the default values # ################################################################################## def main(argv): params = {} params['function'] = 'DSS' params['brewerid'] = '157' params['date'] = datetime.date(2015,1,1) params['enddate'] = datetime.date(2015,1,1) try: opts, args = getopt.getopt(argv, "h:b:d:e:f:", ["help", "brewerid=", "date=","enddate=", "function="]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): usage() sys.exit() elif opt in ("-f", "--function"): params['function'] = arg elif opt in ("-b", "--brewerid"): try: params['brewerid'] = int(arg) params['brewerid'] = str(params['brewerid']).zfill(3) except: usage() sys.exit(2) elif opt in ("-d", "--date"): try: params['date'] = datetime.datetime.strptime(arg, "%Y-%m-%d").date() except: usage() sys.exit(2) elif opt in ("-e", "--enddate"): try: params['enddate'] = datetime.datetime.strptime(arg, "%Y-%m-%d").date() except: usage() sys.exit(2) return params ################################################################################## # Function: connection # # Comments: Try to connect to a specific url using Basic Authentication # ################################################################################## def connection(url,user,password): request = urllib2.Request(url) base64string = base64.standard_b64encode('%s:%s' % (user, password)) request.add_header("Authorization", "Basic %s" % base64string) content = urllib2.urlopen(request).read() return json.loads(content) ################################################################################## # Function: getdata # Comments: Build the url of a specific function given by ################################################################################## def getdata(params, user,password): brewerid = params['brewerid'] date = params['date'].strftime('%Y-%m-%d') enddate = params['enddate'].strftime('%Y-%m-%d') url = "/".join([getdataUrl,params['function']]) brewerid = "=".join(['brewerid',brewerid]) date = "=".join(['date',date]) url = "?".join([url, brewerid]) url = "&".join([url,date]) url = "&".join([url,enddate]) content = connection(url,user,password) return content ################################################################################## if __name__ == '__main__': print 'Connection example\n' params = main(sys.argv[1:]) print params date_ini = params['date'] tic = datetime.datetime.now() while params['date'] <= params['enddate']: print getdata(params,user,password) params['date'] = params['date'] + datetime.timedelta(1,0,0) toc = datetime.datetime.now() print "TIME for", params['function'], "and Brewer #", params['brewerid'], "between", date_ini.strftime("%Y-%m-%d"),"and",params['enddate'].strftime("%Y-%m-%d"),"->", toc - tic ##################################################################################