Skip to content

Commit 26d010f

Browse files
author
Wasin Waeosri
committed
revise codes, README.md to be more 'pratical'
1 parent f0dee5b commit 26d010f

File tree

8 files changed

+55
-41
lines changed

8 files changed

+55
-41
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $>python <application>.py
3030
```
3131

3232
## Optional - How to install requests
33-
The best way is to get the pip package management tool
33+
The best way is via the pip package management tool
3434
1. export <Python_folder>\Scripts to your OS PATH environment
3535
2. call pip command to install requests
3636
```
@@ -75,4 +75,7 @@ You can modify the scripts to run with Python 3 (with requests library installed
7575
- revies README.md
7676
- modify trkd_authen.py
7777
- modify trkd_quote.py
78+
- version 1.0.7: 9 May 2017
79+
- revies README.md
80+
- modify the rest of application files
7881

trkd_chart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import getpass
1616
import urllib2
1717

18-
18+
# Send HTTP request for all services
1919
def doSendRequest(url, requestMsg, headers):
2020
result = None
2121
try:

trkd_interday.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import json
1515
import getpass
1616

17-
17+
# Send HTTP request for all services
1818
def doSendRequest(url, requestMsg, headers):
1919
result = None
2020
try:

trkd_intraday.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import json
1515
import getpass
1616

17-
17+
# Send HTTP request for all services
1818
def doSendRequest(url, requestMsg, headers):
1919
result = None
2020
try:

trkd_newsheadline.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
import getpass
1515

16+
# Send HTTP request for all services
1617
def doSendRequest(url, requestMsg, headers):
1718
result = None
1819
try:

trkd_newsstory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import json
1414
import getpass
1515

16-
16+
# Send HTTP request for all services
1717
def doSendRequest(url, requestMsg, headers):
1818
result = None
1919
try:

trkd_onlinereport.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,81 @@
1313
import json
1414
import getpass
1515

16+
# Send HTTP request for all services
1617
def doSendRequest(url, requestMsg, headers):
1718
result = None
1819
try:
19-
##send request
20-
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
21-
if result.status_code == 500:
22-
print 'Request fail'
23-
print 'response status %s' % result.status_code
24-
print 'Error: %s' % result.json()
25-
sys.exit(1)
20+
# send request
21+
result = requests.post(
22+
url, data=json.dumps(requestMsg), headers=headers)
23+
# handle error
24+
if result.status_code is not 200:
25+
print('Request fail')
26+
print('response status %s' % (result.status_code))
27+
if result.status_code == 500: # if username or password or appid is wrong
28+
print('Error: %s' % (result.json()))
29+
result.raise_for_status()
2630
except requests.exceptions.RequestException, e:
27-
print 'Exception!!!'
28-
print e
31+
print('Exception!!!')
32+
print(e)
2933
sys.exit(1)
3034
return result
3135

3236

33-
## Perform authentication
37+
# Perform authentication
3438
def CreateAuthorization(username, password, appid):
3539
token = None
36-
##create authentication request URL, message and header
37-
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
40+
# create authentication request URL, message and header
41+
authenMsg = {'CreateServiceToken_Request_1': {
42+
'ApplicationID': appid, 'Username': username, 'Password': password}}
3843
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
3944
headers = {'content-type': 'application/json;charset=utf-8'}
4045
print '############### Sending Authentication request message to TRKD ###############'
4146
authenResult = doSendRequest(authenURL, authenMsg, headers)
4247
if authenResult is not None and authenResult.status_code == 200:
43-
print 'Authen success'
44-
print 'response status %s'%(authenResult.status_code)
45-
##get Token
48+
print('Authen success')
49+
print('response status %s' % (authenResult.status_code))
50+
# get Token
4651
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
47-
52+
4853
return token
4954

50-
## Perform Online Report request
55+
# Perform Online Report request
56+
57+
5158
def RetrieveOnlineReport(token, appid):
52-
##construct Online Report URL and header
59+
# construct Online Report URL and header
5360
onlinereportURL = 'http://api.rkd.reuters.com/api/OnlineReports/OnlineReports.svc/REST/OnlineReports_1/GetSummaryByTopic_1'
54-
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
55-
##construct a Online Report request message
61+
headers = {'content-type': 'application/json;charset=utf-8',
62+
'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token}
63+
# construct a Online Report request message
5664
onelinereportRequestMsg = {'GetSummaryByTopic_Request_1': {
5765
'Topic': 'OLRUTOPNEWS',
5866
'MaxCount': 20,
5967
'ReturnPrivateNetworkURL': False
60-
}
6168
}
62-
print '############### Sending News - Online Report request message to TRKD ###############'
63-
onlinereportResult = doSendRequest(onlinereportURL, onelinereportRequestMsg,headers)
69+
}
70+
print('############### Sending News - Online Report request message to TRKD ###############')
71+
onlinereportResult = doSendRequest(
72+
onlinereportURL, onelinereportRequestMsg, headers)
6473
if onlinereportResult is not None and onlinereportResult.status_code == 200:
65-
print 'Online Report response message: '
66-
print onlinereportResult.json()
74+
print('Online Report response message: ')
75+
print(onlinereportResult.json())
6776

6877

6978

7079
## ------------------------------------------ Main App ------------------------------------------ ##
71-
##Get username, password and applicationid
72-
username = raw_input('Please input username: ')
73-
##use getpass.getpass to hide user inputted password
74-
password = getpass.getpass(prompt='Please input password: ')
75-
appid = raw_input('Please input appid: ')
7680

77-
token = CreateAuthorization(username,password,appid)
78-
print 'Token = %s'%(token)
81+
if __name__ == '__main__':
82+
# Get username, password and applicationid
83+
username = raw_input('Please input username: ')
84+
# use getpass.getpass to hide user inputted password
85+
password = getpass.getpass(prompt='Please input password: ')
86+
appid = raw_input('Please input appid: ')
7987

80-
## if authentiacation success, continue subscribing Online Report
81-
if token is not None:
82-
RetrieveOnlineReport(token,appid)
88+
token = CreateAuthorization(username, password, appid)
89+
print 'Token = %s' % (token)
8390

91+
# if authentiacation success, continue subscribing Online Report
92+
if token is not None:
93+
RetrieveOnlineReport(token, appid)

trkd_quote.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import json
1515
import getpass
1616

17-
17+
# Send HTTP request for all services
1818
def doSendRequest(url, requestMsg, headers):
1919
result = None
2020
try:

0 commit comments

Comments
 (0)