1+ #!/usr/bin/env python3
2+
3+ import urllib
4+ import urllib .request
5+ import urllib .error
6+ import json
7+ import datetime
8+ import sys
9+
10+ """
11+ make a call API-NG
12+ """
13+
14+ def callAping (jsonrpc_req ):
15+ try :
16+ req = urllib .request .Request (url , jsonrpc_req .encode ('utf-8' ), headers )
17+ response = urllib .request .urlopen (req )
18+ jsonResponse = response .read ()
19+ return jsonResponse .decode ('utf-8' )
20+ except urllib .error .URLError as e :
21+ print (e .reason )
22+ print ('Oops no service available at ' + str (url ))
23+ exit ()
24+ except urllib .error .HTTPError :
25+ print ('Oops not a valid operation from the service ' + str (url ))
26+ exit ()
27+
28+
29+ """
30+ calling getEventTypes operation
31+ """
32+
33+ def getEventTypes ():
34+ event_type_req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listEventTypes", "params": {"filter":{ }}, "id": 1}'
35+ print ('Calling listEventTypes to get event Type ID' )
36+ eventTypesResponse = callAping (event_type_req )
37+ eventTypeLoads = json .loads (eventTypesResponse )
38+ """
39+ print eventTypeLoads
40+ """
41+
42+ try :
43+ eventTypeResults = eventTypeLoads ['result' ]
44+ return eventTypeResults
45+ except :
46+ print ('Exception from API-NG' + str (eventTypeLoads ['error' ]))
47+ exit ()
48+
49+
50+ """
51+ Extraction eventypeId for eventTypeName from evetypeResults
52+ """
53+
54+ def getEventTypeIDForHorseRacing (eventTypesResult , requestedEventTypeName ):
55+ if (eventTypesResult is not None ):
56+ for event in eventTypesResult :
57+ eventTypeName = event ['eventType' ]['name' ]
58+ if ( eventTypeName == requestedEventTypeName ):
59+ return event ['eventType' ]['id' ]
60+ else :
61+ print ('Oops there is an issue with the input' )
62+ exit ()
63+
64+
65+ """
66+ Calling marketCatalouge to get marketDetails
67+ """
68+
69+ def getMarketCatalouge (eventTypeID ):
70+ if (eventTypeID is not None ):
71+ print ('Calling listMarketCatalouge Operation to get MarketID and selectionId' )
72+ now = datetime .datetime .now ().strftime ('%Y-%m-%dT%H:%M:%SZ' )
73+ market_catalouge_req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listMarketCatalogue", "params": {"filter":{"eventTypeIds":["' + eventTypeID + '"],"marketCountries":["GB"],"marketTypeCodes":["WIN"],' \
74+ '"marketStartTime":{"from":"' + now + '"}},"sort":"FIRST_TO_START","maxResults":"1","marketProjection":["RUNNER_METADATA"]}, "id": 1}'
75+ """
76+ print market_catalouge_req
77+ """
78+ market_catalouge_response = callAping (market_catalouge_req )
79+ """
80+ print market_catalouge_response
81+ """
82+ market_catalouge_loads = json .loads (market_catalouge_response )
83+ try :
84+ market_catalouge_resluts = market_catalouge_loads ['result' ]
85+ return market_catalouge_resluts
86+ except :
87+ print ('Exception from API-NG' + str (market_catalouge_resluts ['error' ]))
88+ exit ()
89+
90+
91+ def getMarketId (marketCatalougeResult ):
92+ if ( marketCatalougeResult is not None ):
93+ for market in marketCatalougeResult :
94+ return market ['marketId' ]
95+
96+
97+ def getSelectionId (marketCatalougeResult ):
98+ if (marketCatalougeResult is not None ):
99+ for market in marketCatalougeResult :
100+ return market ['runners' ][0 ]['selectionId' ]
101+
102+
103+ def getMarketBook (marketId ):
104+ print ('Calling listMarketBook to read prices for the Market with ID :' + marketId )
105+ market_book_req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/listMarketBook", "params": {"marketIds":["' + marketId + '"],"priceProjection":{"priceData":["EX_BEST_OFFERS"]}}, "id": 1}'
106+ """
107+ print market_book_req
108+ """
109+ market_book_response = callAping (market_book_req )
110+ """
111+ print market_book_response
112+ """
113+ market_book_loads = json .loads (market_book_response )
114+ try :
115+ market_book_result = market_book_loads ['result' ]
116+ return market_book_result
117+ except :
118+ print ('Exception from API-NG' + str (market_catalouge_resluts ['error' ]))
119+ exit ()
120+
121+
122+ def printPriceInfo (market_book_result ):
123+ if (market_book_result is not None ):
124+ print ('Please find Best three available prices for the runners' )
125+ for marketBook in market_book_result :
126+ runners = marketBook ['runners' ]
127+ for runner in runners :
128+ print ('Selection id is ' + str (runner ['selectionId' ]))
129+ if (runner ['status' ] == 'ACTIVE' ):
130+ print ('Available to back price :' + str (runner ['ex' ]['availableToBack' ]))
131+ print ('Available to lay price :' + str (runner ['ex' ]['availableToLay' ]))
132+ else :
133+ print ('This runner is not active' )
134+
135+
136+ def placeBet (marketId , selectionId ):
137+ if ( marketId is not None and selectionId is not None ):
138+ print ('Calling placeOrder for marketId :' + marketId + ' with selection id :' + str (selectionId ))
139+ place_order_Req = '{"jsonrpc": "2.0", "method": "SportsAPING/v1.0/placeOrders", "params": {"marketId":"' + marketId + '","instructions":' \
140+ '[{"selectionId":"' + str (
141+ selectionId ) + '","handicap":"0","side":"BACK","orderType":"LIMIT","limitOrder":{"size":"0.01","price":"1.50","persistenceType":"LAPSE"}}],"customerRef":"test12121212121"}, "id": 1}'
142+ """
143+ print place_order_Req
144+ """
145+ place_order_Response = callAping (place_order_Req )
146+ place_order_load = json .loads (place_order_Response )
147+ try :
148+ place_order_result = place_order_load ['result' ]
149+ print ('Place order status is ' + place_order_result ['status' ])
150+ """
151+ print 'Place order error status is ' + place_order_result['errorCode']
152+ """
153+ print ('Reason for Place order failure is ' + place_order_result ['instructionReports' ][0 ]['errorCode' ])
154+ except :
155+ print ('Exception from API-NG' + str (market_catalouge_resluts ['error' ]))
156+ """
157+ print place_order_Response
158+ """
159+
160+
161+ url = "https://api.betfair.com/exchange/betting/json-rpc/v1"
162+
163+ """
164+ headers = { 'X-Application' : 'xxxxxx', 'X-Authentication' : 'xxxxx' ,'content-type' : 'application/json' }
165+ """
166+
167+ args = len (sys .argv )
168+
169+ if ( args < 3 ):
170+ print ('Please provide Application key and session token' )
171+ appKey = input ('Enter your application key :' )
172+ sessionToken = input ('Enter your session Token/SSOID :' )
173+ print ('Thanks for the input provided' )
174+ else :
175+ appKey = sys .argv [1 ]
176+ sessionToken = sys .argv [2 ]
177+
178+ headers = {'X-Application' : appKey , 'X-Authentication' : sessionToken , 'content-type' : 'application/json' }
179+
180+ eventTypesResult = getEventTypes ()
181+ horseRacingEventTypeID = getEventTypeIDForHorseRacing (eventTypesResult , 'Horse Racing' )
182+
183+ print ('Eventype Id for Horse Racing is :' + str (horseRacingEventTypeID ))
184+
185+ marketCatalougeResult = getMarketCatalouge (horseRacingEventTypeID )
186+ marketid = getMarketId (marketCatalougeResult )
187+ runnerId = getSelectionId (marketCatalougeResult )
188+ """
189+ print marketid
190+ print runnerId
191+ """
192+ market_book_result = getMarketBook (marketid )
193+ printPriceInfo (market_book_result )
194+
195+ placeBet (marketid , runnerId )
0 commit comments