1515import getpass
1616import urllib2
1717
18-
18+ # Send HTTP request for all services
1919def doSendRequest (url , requestMsg , headers ):
2020 result = None
2121 try :
2222 ##send request
2323 result = requests .post (url , data = json .dumps (requestMsg ), headers = headers )
24- if result .status_code == 500 :
25- print 'Request fail'
26- print 'response status %s' % result .status_code
27- print 'Error: %s' % result .json ()
28- sys .exit (1 )
24+ ## handle error
25+ if result .status_code is not 200 :
26+ print ('Request fail' )
27+ print ('response status %s' % (result .status_code ))
28+ if result .status_code == 500 : ## if username or password or appid is wrong
29+ print ('Error: %s' % (result .json ()))
30+ result .raise_for_status ()
2931 except requests .exceptions .RequestException , e :
30- print 'Exception!!!'
31- print e
32+ print ( 'Exception!!!' )
33+ print ( e )
3234 sys .exit (1 )
3335 return result
3436
@@ -40,11 +42,11 @@ def CreateAuthorization(username, password, appid):
4042 authenMsg = {'CreateServiceToken_Request_1' : { 'ApplicationID' :appid , 'Username' :username ,'Password' :password }}
4143 authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
4244 headers = {'content-type' : 'application/json;charset=utf-8' }
43- print '############### Sending Authentication request message to TRKD ###############'
45+ print ( '############### Sending Authentication request message to TRKD ###############' )
4446 authenResult = doSendRequest (authenURL , authenMsg , headers )
4547 if authenResult is not None and authenResult .status_code == 200 :
46- print 'Authen success'
47- print 'response status %s' % (authenResult .status_code )
48+ print ( 'Authen success' )
49+ print ( 'response status %s' % (authenResult .status_code ) )
4850 ##get Token
4951 token = authenResult .json ()['CreateServiceToken_Response_1' ]['Token' ]
5052
@@ -272,56 +274,58 @@ def RetrieveChart(token, appid):
272274 'ReturnPrivateNetworkURL' : False ,
273275 }}}
274276 ##construct Chart URL and header
275- chartURL = 'http://api.rkd.reuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2 '
277+ chartURL = 'http://api.rkd.reuters.com/api/Charts/Charts.svc/REST/Charts_1/GetChart_2BB '
276278 headers = {'content-type' : 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID' : appid , 'X-Trkd-Auth-Token' : token }
277279
278- print '############### Sending Chart request message to TRKD ###############'
280+ print ( '############### Sending Chart request message to TRKD ###############' )
279281 chartResult = doSendRequest (chartURL , chartRequestMsg ,headers )
280282 if chartResult is not None and chartResult .status_code == 200 :
281- print 'Time Series Interday response message: '
282- print chartResult .json ()
283+ print ( 'Time Series Interday response message: ' )
284+ print ( chartResult .json () )
283285 ##print returned server, tag and image url
284286 server = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Server' ]
285- print '\n Server: %s' % (server )
287+ print ( '\n Server: %s' % (server ) )
286288 tag = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Tag' ]
287- print 'Tag: %s' % (tag )
289+ print ( 'Tag: %s' % (tag ) )
288290 imageUrl = chartResult .json ()['GetChart_Response_2' ]['ChartImageResult' ]['Url' ]
289- print 'Url: %s' % (imageUrl )
291+ print ( 'Url: %s' % (imageUrl ) )
290292 return imageUrl
291293
292294##download image url from the TRKD Chart service as chart.png
293295def downloadChartImage (chartURL ):
294296 ##create header
295297 user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
296298 headers = { 'User-Agent' : user_agent }
297- print '\n Downlading chart.png file from %s' % (chartURL )
299+ print ( '\n Downlading chart.png file from %s' % (chartURL ) )
298300 ##download image using Python urllib2
299301 downloadResult = urllib2 .Request (chartURL , headers = headers )
300302 imgData = urllib2 .urlopen (downloadResult ).read ()
301303 ##write file
302304 fileName = './chart.png'
303305 with open (fileName ,'wb' ) as outfile :
304306 outfile .write (imgData )
305- print 'save chart.png file complete'
307+ print ( 'save chart.png file complete' )
306308
307309
308310
309311## ------------------------------------------ Main App ------------------------------------------ ##
310- ##Get username, password and applicationid
311- username = raw_input ('Please input username: ' )
312- ##use getpass.getpass to hide user inputted password
313- password = getpass .getpass (prompt = 'Please input password: ' )
314- appid = raw_input ('Please input appid: ' )
315312
316- token = CreateAuthorization (username ,password ,appid )
317- print 'Token = %s' % (token )
318- ## if authentiacation success, continue subscribing Chart
319- if token is not None :
320- chartURL = RetrieveChart (token ,appid )
321- ## if chart request success, continue downloading Chart image
322- if chartURL is not None :
323- print '############### Downloading Chart file from TRKD ###############'
324- downloadChartImage (chartURL )
313+ if __name__ == '__main__' :
314+ ##Get username, password and applicationid
315+ username = raw_input ('Please input username: ' )
316+ ##use getpass.getpass to hide user inputted password
317+ password = getpass .getpass (prompt = 'Please input password: ' )
318+ appid = raw_input ('Please input appid: ' )
319+
320+ token = CreateAuthorization (username ,password ,appid )
321+ print ('Token = %s' % (token ))
322+ ## if authentiacation success, continue subscribing Chart
323+ if token is not None :
324+ chartURL = RetrieveChart (token ,appid )
325+ ## if chart request success, continue downloading Chart image
326+ if chartURL is not None :
327+ print ('############### Downloading Chart file from TRKD ###############' )
328+ downloadChartImage (chartURL )
325329
326330
327331
0 commit comments