Could anyone tell me why my code doesnt work ?
It should
1) ask user for a name of a runner
2) print the runner's odds.
Code: Select all
import json
import requests
def post_to_ba(endpoint, raw_json):
headers = {'Content-Type': 'application/json'}
response = requests.post(endpoint, data=raw_json, headers=headers)
return response.text
# Prompt the user for the selection name
selection_name = input("Enter the selection name you require the best three prices for: ")
# Make the request and store the response in a variable
request_data = {
"dataRequired": ["BEST_THREE_PRICES"]
}
response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarketPrices", json.dumps(request_data))
# Parse the response text as JSON
try:
response_json = json.loads(response)
except json.JSONDecodeError as e:
print("Error decoding the response JSON:", e)
exit()
# Initialize a list to store the best three prices data
best_three_prices_data = []
# Check if the response contains a string "BEST_THREE_PRICES"
if "BEST_THREE_PRICES" in response_json:
best_three_prices_data = response_json["BEST_THREE_PRICES"]
# Print the best three prices if available
if best_three_prices_data:
print("Best Three Prices:")
for price_data in best_three_prices_data:
price = price_data['price']
size = price_data['size']
print(f"Price: {price}, Size: {size}")
else:
print(f"No best three prices found for selection: {selection_name}.")
Code: Select all
import json
import requests
def post_to_ba(endpoint, raw_json):
headers = {'Content-Type': 'application/json'}
response = requests.post(endpoint, data=raw_json, headers=headers)
return response.text
get_markets_request = {
"dataRequired": ["ID", "NAME", "MARKET_START_TIME", "EVENT_ID", "EVENT_TYPE_ID", "MARKET_TYPE", "SELECTION_IDS", "SELECTION_NAMES"]
}
response_markets = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets", json.dumps(get_markets_request))
try:
markets_response_json = json.loads(response_markets)
except json.JSONDecodeError as e:
print("Error decoding the response JSON:", e)
exit()
selection_name = input("Enter the selection name you require the best three prices for: ")
matching_market_id = None
matching_selection_id = None
for market_data in markets_response_json['result']['markets']:
for selection_data in market_data["selections"]:
if selection_name == selection_data["name"]:
matching_market_id = market_data["id"]
matching_selection_id = selection_data["id"]
break
if matching_market_id is None or matching_selection_id is None:
print(f"No market or selection found with name: {selection_name}.")
exit()
request_data = {
"marketsFilter": {
"filter": "SPECIFIED_IDS",
"ids": [matching_market_id]
},
"dataRequired": ["BEST_THREE_PRICES"]
}
response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarketPrices", json.dumps(request_data))
try:
response_json = json.loads(response)
except json.JSONDecodeError as e:
print("Error decoding the response JSON:", e)
exit()
if "BEST_THREE_PRICES" in response_json:
best_three_prices_data = response_json["BEST_THREE_PRICES"]
print("Best Three Prices:")
for price_data in best_three_prices_data:
price = price_data['price']
size = price_data['size']
print(f"Price: {price}, Size: {size}")
else:
print(f"No best three prices found for selection: {selection_name}.")