API Setup

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
Diginet
Posts: 118
Joined: Tue Jul 09, 2013 1:43 pm

Hi,
I am trying to collect my data and store it in a SQL. The problem im getting is the data coming back is always Null, any ideas what im doing wrong ?
Capture.JPG
You do not have the required permissions to view the files attached to this post.
LinusP
Posts: 1944
Joined: Mon Jul 02, 2012 10:45 pm

code?
Diginet
Posts: 118
Joined: Tue Jul 09, 2013 1:43 pm

Hi

Thank you for the reply

def get_db_connection():
return psycopg2.connect(**DB_CONFIG)

def apply_coupon(coupon_name="Todays_Horses"):
"""Load the coupon once at startup."""
url = f"{BA_BASE_URL}guardian/v1.0/applyCoupon"
payload = {
"couponName": coupon_name,
"clearOption": "CLEAR_GUARDIAN_AND_WATCH_LIST",
"watchListNumber": 1
}
try:
print(f"[{datetime.now().strftime('%H:%M:%S')}] Loading coupon '{coupon_name}'...")
r = requests.post(url, json=payload, timeout=30)
r.raise_for_status()
print("[SUCCESS] Coupon loaded.")
return True
except Exception as e:
print(f"[ERROR] Failed to load coupon: {e}")
return False

def get_all_guardian_markets():
"""Simply returns every market currently in Guardian."""
url = f"{BA_BASE_URL}markets/v1.0/getMarkets"
try:
r = requests.post(url, json={"dataRequired": ["ID", "NAME"]}, timeout=10)
r.raise_for_status()
markets = r.json().get("result", {}).get("markets", [])
return markets
except Exception as e:
print(f" [!] API Error fetching markets: {e}")
return []

def collect_and_save():
markets = get_all_guardian_markets()

if not markets:
print(f"[{datetime.now().strftime('%H:%M:%S')}] Guardian is empty. Waiting...")
return False

market_ids = [m["id"] for m in markets]
url = f"{BA_BASE_URL}markets/v1.0/getMarketPrices"
payload = {"marketIds": market_ids, "dataRequired": ["EX_ALL_OFFERS", "LAST_TRADED"]}

try:
r = requests.post(url, json=payload, timeout=20)
r.raise_for_status()
price_data = r.json().get("result", {}).get("markets", [])

conn = get_db_connection()
cur = conn.cursor()

for m in price_data:
m_id = str(m["id"])
# Save Market Info
cur.execute("INSERT INTO markets (market_id, market_name) VALUES (%s, %s) ON CONFLICT (market_id) DO NOTHING;", (m_id, m.get("name", "")))

for s in m.get("selections", []):
s_id = int(s["id"])
# Save Selection Info
cur.execute("INSERT INTO selections (selection_id, market_id, horse_name) VALUES (%s, %s, %s) ON CONFLICT (selection_id) DO NOTHING;", (s_id, m_id, s.get("name", "Unknown")))

# Get Top 3 Prices
b = s.get("availableToBack", [])[:3]
l = s.get("availableToLay", [])[:3]
ltp = s.get("lastTradedPrice", 0)

# DEBUG: Print raw data for inspection
# print(f"DEBUG: Selection {s_id} | Back: {b} | Lay: {l} | LTP: {ltp}")

def safe_get(lst, idx, key):
try:
val = lst[idx][key]
if val is None:
return 0
return float(val)
except (IndexError, KeyError, ValueError, TypeError):
return 0

cur.execute("""INSERT INTO price_snapshots (market_id, selection_id, last_traded_price,
back_price_1, back_size_1, back_price_2, back_size_2, back_price_3, back_size_3,
lay_price_1, lay_size_1, lay_price_2, lay_size_2, lay_price_3, lay_size_3)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);""",
(m_id, s_id, float(ltp),
safe_get(b,0,"price"), safe_get(b,0,"size"),
safe_get(b,1,"price"), safe_get(b,1,"size"),
safe_get(b,2,"price"), safe_get(b,2,"size"),
safe_get(l,0,"price"), safe_get(l,0,"size"),
safe_get(l,1,"price"), safe_get(l,1,"size"),
safe_get(l,2,"price"), safe_get(l,2,"size")))

conn.commit()
cur.close()
conn.close()
print(f"[{datetime.now().strftime('%H:%M:%S')}] Saved data for {len(markets)} markets.")
return True
except Exception as e:
print(f" [!] Database Error: {e}")
return False

def main():
print("--- Bet Angel Data Collector: Guardian Sync Mode ---")

# Load Coupon
apply_coupon("Todays_Horses")

print("Waiting 10 seconds for Guardian to populate...")
time.sleep(10)

while True:
collect_and_save()
time.sleep(1)

if __name__ == "__main__":
main()
LinusP
Posts: 1944
Joined: Mon Jul 02, 2012 10:45 pm

What is the table you screenshot as it doesn't match up with any of the schemas described in the code?
Diginet
Posts: 118
Joined: Tue Jul 09, 2013 1:43 pm

Sorry I forgot to update the screenshot. Anyways I spent the day figuring out a better way. :) Thank you for taking the time to reply

Cheers
Capture.JPG
You do not have the required permissions to view the files attached to this post.
Post Reply

Return to “Bet Angel - API”