Betangel API (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
Win
Posts: 6
Joined: Sun Feb 02, 2020 10:16 pm

PeterLe wrote:
Sat Dec 31, 2022 10:07 am
Correction to the code in the opening post :

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 ID for: ")

# Make the request and store the response in a variable
response = post_to_ba("http://localhost:9000/api/markets/v1.0/getMarkets",
                      '{"dataRequired":["ID","NAME","MARKET_START_TIME","EVENT_ID","EVENT_TYPE_ID","MARKET_TYPE","SELECTION_IDS","SELECTION_NAMES"]}')

# Parse the response text as JSON
response_json = json.loads(response)
print(response_json)

# Initialize an empty list to store the matching selection names and IDs
matching_selections = []

# Search for the selection in ALL markets
for market in response_json['result']['markets']:
    for selection in market['selections']:
        if selection['name'].startswith(selection_name):
            matching_selections.append({'name': selection['name'], 'id': selection['id']})

# Print the name and ID of each matching selection

if matching_selections:
    for selection in matching_selections:
        print(f"Selection name: {selection['name']}, Selection ID: {selection['id']}")
else:
    print(f"No selections found with names starting with {selection_name}.")
    
Hi Peter
I'm new to Python but I downloaded PyCharm as suggested and copying the above code but when I ran it I got the following error:
Traceback (most recent call last):
File "C:\Data\PycharmProjects\pythonProject2\main.py", line 2, in <module>
import requests
ModuleNotFoundError: No module named 'requests'


I also tried testing your code from immediately above (your post of 7 January) and I get this error:
File "C:\Data\PycharmProjects\pythonProject1\main.py", line 3, in post_to_ba
response = requests.post(endpoint, data=raw_json, headers=headers)
^^^^^^^^
NameError: name 'requests' is not defined

In both cases it doesn't like 'requests'. Any suggestions as to what is wrong?

Thanks
Richard
PeterLe
Posts: 3715
Joined: Wed Apr 15, 2009 3:19 pm

Hi Richard,
It just means that the request module isn't in your current environment
(ie it looks for the module but it cant find it)

Open your terminal/command prompt and type the following command:
pip install requests
If you have multiple versions of python running then you may need to type :
pip3 install requests

I still consider myself a newbie where Python is concerned, chat GPT is your friend here..This is the full answer :-

The error message you're seeing suggests that the requests module is not currently installed in your Python environment. requests is a third-party library that isn't included in Python's standard library.

You can install the requests library using pip. Pip is a package manager for Python and is typically included with a Python installation.

Open your terminal/command prompt and type the following command:

python
Copy code
pip install requests
If you are using a specific Python environment or if you have multiple Python versions installed, you may need to specify the version by using pip3 or the full path to the pip module that corresponds to the Python interpreter you're using. In this case, you would use:

python
Copy code
pip3 install requests
After running one of these commands, pip should download and install the requests library. Once the installation is complete, try to run your code again. You should no longer see the ModuleNotFoundError.

If you are working in a specific project environment in PyCharm, you might need to install the library there. Here is how to do it:

Go to File > Settings (CTRL+ALT+S) in PyCharm.
In the settings window, navigate to Project: [your_project_name] > Python Interpreter.
In the Python Interpreter settings page, you'll see a list of all packages installed in the current environment.
Click on the + button to add a new package.
In the search bar, type requests, then find it in the list and click on the Install Package button at the bottom.
This should install requests in your project environment and you should be able to import it in your code.
---End of ChatGPT -------

Hope that helps.
Regards
Peter

PS Stick at it after a while Python begins to click into place. it opens up a whole new world
Win
Posts: 6
Joined: Sun Feb 02, 2020 10:16 pm

Thanks Peter
I now have a python script that adds new markets to Guardian based on my coupon filters and then adds my automation rule to those markets.
Additionally, I now have a Windows scheduled task to run the python script.
Now all I need to do is work out how to remove all markets from Guardian once they go into play - but I will leave that for tomorrow night.
Thanks for your help.
Richard
sniffer66
Posts: 1681
Joined: Thu May 02, 2019 8:37 am

Win wrote:
Wed Jul 19, 2023 9:59 pm
Thanks Peter
I now have a python script that adds new markets to Guardian based on my coupon filters and then adds my automation rule to those markets.
Additionally, I now have a Windows scheduled task to run the python script.
Now all I need to do is work out how to remove all markets from Guardian once they go into play - but I will leave that for tomorrow night.
Thanks for your help.
Richard
The best way I've found to remove markets is to set an SV using any required conditions in Guardian. I name mine "RemoveMe". Give it a value of 1

Then query set SV's for each market via the API, if RemoveMe =1 then delete that market ID from Guardian.

That gives you a granular control over which markets are removed using Guardian, without having to edit your code constantly. I.e no matched bets after x minutes, set the SV. No goals at HT , set the SV
Win
Posts: 6
Joined: Sun Feb 02, 2020 10:16 pm

Hi Sniffer66
I agree, using a stored value to identify the markets I want auto-removed from Guardian is a lot better solution because in reality I only want to remove the markets that go into play that don't have any bets on them.
Anyway, I have worked out how to get all markets with a stored value of 'Remove' using the API request below, but I can't work out how to use that information to then remove the markets. I think, based on what I have read, that I need to save the market ID's from the first API to a variable then read that variable using the second API, but how I do this is completely beyond my understanding.
Any pointers would be greatly appreciated.
The API request I am using to get the market ID's that have an SV of 'remove' is:
{"marketsFilter":{"filter":"ALL"},"selectionsFilter":{"filter":"ALL"},"storedValueFilterMarketLevel":{"storedValueFilter":"SPECIFIED_NAMES","names":["Remove"],"excludeInstanceValues":true}}

Thanks
Richard
Win
Posts: 6
Joined: Sun Feb 02, 2020 10:16 pm

I feel like I've achieved something!
I've now got a scheduled batch file that runs every five minutes and adds new markets using a coupon, adds automation rules to those markets and removes any markets that have gone into play without having any bets placed on them.
Now back to my day job!
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 for a name of a runner
1) print the runner's odds.
SOunds simple.....

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 and it fails too:

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}.")
Post Reply

Return to “Bet Angel - API”