Searching for odds when name entered (Python)

The Bet Angel API makes it easy for you to further enhance your betting and trading by integrating your own code into Bet Angel
Post Reply
User avatar
ilovepizza82
Posts: 495
Joined: Thu Nov 02, 2017 3:41 pm
Location: Sewers
Contact:

Hi guys, Im still studying Python hence struggling quite a bit.
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}.")
    
    
I also tried this following one of the BA's devs' advice:

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}.")
    
    
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

The problem is a 404 on this command:
Request to http://localhost:9000/api/markets/v1.0/getMarketPrices returned status code 404

(You can test this your self, Ive changed the original code to this :

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)
    print(f"Request to {endpoint} returned status code {response.status_code}") # Print the status code
    if response.status_code != 200:
        print(f"Request failed with status code {response.status_code}")
        exit()
    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))

print("Raw response:", response_markets)  # Print raw response

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))

print("Raw response:", response)  # Print raw response

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}.")
Has the endpoint changed? (Sorry short of time today but that might get you started...)
User avatar
ilovepizza82
Posts: 495
Joined: Thu Nov 02, 2017 3:41 pm
Location: Sewers
Contact:

Thanks.
Unfortunately im getting the same error message as before:
"No best three prices found for selection: (...)"
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

yes as I mentioned; the error is with this :-

Request to http://localhost:9000/api/markets/v1.0/getMarketPrices

drop a note to support and ask them to confirm it is the correct endpoint
Bet Angel
Bet Angel
Bet Angel
Posts: 4001
Joined: Tue Apr 14, 2009 3:47 pm

The response from the API from that code is working ok when I run it, but the code to extract the information doesn't look correct.

Request to http://localhost:9000/api/markets/v1.0/getMarketPrices returned status code 200

Raw response: {"status":"OK","result":{"markets":[{"id":"1.216882788","status":"OPEN","lastUpdated":"2023-08-10T16:31:16.4337011+01:00","selections":[{"id":"45005923","back3":{"prc":4.1,"sz":121.93},"back2":{"prc":4.2,"sz":81.01},"back1":{"prc":4.3,"sz":88.32},"lay1":{"prc":4.4,"sz":93.85},"lay2":{"prc":4.5,"sz":57.85},"lay3":{"prc":4.6,"sz":105.37}},{"id":"23071434","back3":{"prc":4.5,"sz":59.22},"back2":{"prc":4.6,"sz":73.12},"back1":{"prc":4.7,"sz":156.05},"lay1":{"prc":4.8,"sz":36.17},"lay2":{"prc":4.9,"sz":47.52},"lay3":{"prc":5.0,"sz":62.04}},{"id":"26181625","back3":{"prc":4.2,"sz":110.92},"back2":{"prc":4.3,"sz":83.35},"back1":{"prc":4.4,"sz":77.93},"lay1":{"prc":4.5,"sz":47.72},"lay2":{"prc":4.6,"sz":48.22},"lay3":{"prc":4.7,"sz":88.9}},{"id":"24989004","back3":{"prc":4.9,"sz":148.23},"back2":{"prc":5.0,"sz":52.11},"back1":{"prc":5.1,"sz":38.87},"lay1":{"prc":5.2,"sz":62.76},"lay2":{"prc":5.3,"sz":25.43},"lay3":{"prc":5.4,"sz":59.37}},{"id":"37778140","back3":{"prc":11.0,"sz":27.49},"back2":{"prc":11.5,"sz":102.4},"back1":{"prc":12.0,"sz":15.51},"lay1":{"prc":12.5,"sz":38.52},"lay2":{"prc":13.0,"sz":49.35},"lay3":{"prc":13.5,"sz":31.85}},{"id":"24977903","back3":{"prc":16.0,"sz":148.55},"back2":{"prc":16.5,"sz":37.48},"back1":{"prc":17.0,"sz":29.93},"lay1":{"prc":17.5,"sz":3.71},"lay2":{"prc":18.0,"sz":54.63},"lay3":{"prc":18.5,"sz":1.54}}]}]}}
Bet Angel
Bet Angel
Bet Angel
Posts: 4001
Joined: Tue Apr 14, 2009 3:47 pm

PeterLe wrote:
Wed Aug 09, 2023 2:07 pm
The problem is a 404 on this command:
Request to http://localhost:9000/api/markets/v1.0/getMarketPrices returned status code 404
Peter, You are testing that against the new beta version, aren't you? It's just that you'd get a 404 if you made that request to v1.60
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

Bet Angel wrote:
Thu Aug 10, 2023 4:52 pm
PeterLe wrote:
Wed Aug 09, 2023 2:07 pm
The problem is a 404 on this command:
Request to http://localhost:9000/api/markets/v1.0/getMarketPrices returned status code 404
Peter, You are testing that against the new beta version, aren't you? It's just that you'd get a 404 if you made that request to v1.60
Hi Yes, I am using V1.60.0. Ill try it later when the racing finishes, on the latest latest beta thanks
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

Works just fine with 1.610_B1
Thanks
regards
Peter
Request to http://localhost:9000/api/markets/v1.0/getMarkets returned status code 200
Raw response: {"status":"OK","result":{"markets":[{"id":"1.216939714","name":"Ascot 12th Aug - 13:35 5f Hcap","marketType":"WIN","eventId":"32547426_13:35","eventTypeId":"7","startTime":"2023-08-12T13:35:00+01:00","selections":[{"id":"28575480","name":"Existent"},{"id":"39311119","name":"Bond Chairman"},{"id":"46091420","name":"Rogue Lightning"},{"id":"25253491","name":"Intrinsic Bond"},{"id":"43930431","name":"Michaelas Boy"},{"id":"28588717","name":"Dream Composer"},.....ETC


Enter the selection name you require the best three prices for: Judicial
Request to http://localhost:9000/api/markets/v1.0/getMarketPrices returned status code 200
Raw response: {"status":"OK","result":{"markets":[{"id":"1.216939714","status":"OPEN","lastUpdated":"2023-08-12T09:58:42.5923304+01:00","selections":[{"id":"28575480","back3":{"prc":5.8,"sz":329.21},"back2":{"prc":5.9,"sz":70.98},"back1":{"prc":6.0,"sz":55.24},"lay1":{"prc":6.2,"sz":28.75},"lay2":{"prc":6.4,"sz":79.23},"lay3":{"prc":6.6,"sz":103.83}},{"id":"39311119","back3":{"prc":6.0,"sz":94.71},"back2":{"prc":6.2,"sz":47.29},"back1":{"prc":6.4,"sz":15.87},"lay1":{"prc":6.6,"sz":10.47},"lay2":{"prc":6.8,"sz":90.57},"lay3":{"prc":7.0,"sz":618.94}},{"id":"46091420","back3":{"prc":8.0,"sz":19.92},"back2":{"prc":8.2,"sz":10.04},"back1":{"prc":8.4,"sz":221.92},"lay1":{"prc":8.8,"sz":7.55},"lay2":{"prc":9.0,"sz":6.64},"lay3":{"prc":9.2,"sz":7.03}},{"id":"25253491","back3":{"prc":7.2,"sz":39.8},"back2":{"prc":7.4,"sz":12.51},"back1":{"prc":7.6,"sz":11.39},"lay1":{"prc":7.8,"sz":48.04},"lay2":{"prc":8.0,"sz":313.3},"lay3":{"prc":8.2,"sz":23.73}},{"id":"43930431","back3":{"prc":9.8,"sz":25.83},"back2":{"prc":10.0,"sz":106.45},"back1":{"prc":10.5,"sz":141.49},"lay1":{"prc":11.0,"sz":124.4},"lay2":{"prc":11.5,"sz":136.27},"lay3":{"prc":12.0,"sz":28.63}},{"id":"28588717","back3":{"prc":10.5,"sz":8.86},"back2":{"prc":11.0,"sz":37.61},"back1":{"prc":11.5,"sz":9.44},"lay1":{"prc":12.0,"sz":5.28},"lay2":{"prc":12.5,"sz":33.67},"lay3":{"prc":13.0,"sz":86.92}},{"id":"11374278","back3":{"prc":14.0,"sz":114.52},"back2":{"prc":14.5,"sz":11.02},"back1":{"prc":15.0,"sz":5.18},"lay1":{"prc":15.5,"sz":14.0},"lay2":{"prc":16.0,"sz":7.38},"lay3":{"prc":16.5,"sz":2.51}},{"id":"35808650","back3":{"prc":13.5,"sz":44.01},"back2":{"prc":14.0,"sz":21.03},"back1":{"prc":14.5,"sz":11.43},"lay1":{"prc":15.0,"sz":16.86},"lay2":{"prc":15.5,"sz":4.28},"lay3":{"prc":16.0,"sz":14.84}},{"id":"36503513","back3":{"prc":16.0,"sz":16.75},"back2":{"prc":16.5,"sz":16.17},"back1":{"prc":17.0,"sz":5.39},"lay1":{"prc":17.5,"sz":11.58},"lay2":{"prc":18.0,"sz":12.69},"lay3":{"prc":18.5,"sz":3.68}},{"id":"7119704","back3":{"prc":19.0,"sz":23.0},"back2":{"prc":19.5,"sz":6.23},"back1":{"prc":20.0,"sz":17.72},"lay1":{"prc":21.0,"sz":12.94},"lay2":{"prc":22.0,"sz":12.86},"lay3":{"prc":23.0,"sz":4.48}},{"id":"13676157","back3":{"prc":65.0,"sz":4.64},"back2":{"prc":70.0,"sz":5.02},"back1":{"prc":75.0,"sz":11.59},"lay1":{"prc":85.0,"sz":6.07},"lay2":{"prc":90.0,"sz":10.45},"lay3":{"prc":95.0,"sz":11.37}}]}]}}
User avatar
ilovepizza82
Posts: 495
Joined: Thu Nov 02, 2017 3:41 pm
Location: Sewers
Contact:

*just bumping the topic in case if anyone knew how to do this
Post Reply

Return to “Bet Angel - API”