Once you’ve got Eric installed, you need to download bfpy, the Python library written for the Betfair API. It comes as a tar.gz and includes a version of suds known to work the library (the author explains since suds changes quite often, it makes sense to include a stable version with the release).
Unzip the file and do some file jiggling:
unzip bfpy[version] cp -r home/user/bfpy/bfpy/* /home/user/BOT_DIRECTORY
It makes sense to keep a separate directory for working in when creating the bot. Here we are copying the source files to the directory.
Back to Eric – create new a python file and trying importing the two module.
import bfpy
If Eric doesn’t complain, well done – the modules are now ready to use. Now might be a good time to take a look at the bfpy documentation (in the /bfpy/doc folder) which goes through the various methods in the library. The documentation is a bit sparse, but with a bit of common sense and the Betfair API to hand, nothing is going to get in our way! If Eric complains when you try to import the modules, read the error message carefully. Python in general is pretty good at returning useful error messages. So if the interpreter is saying the file can’t be found, check your newly created python file can see the modules.
Now for creating our bot, we’ll also need to import some otherĀ python modules that are used later on.
import bfpy import psycopg2 import datetime from time import localtime import random
10 points for anyone that spotted psycopg2 in there. Psycopg2 is a database adapter for python which allows connectivity to a postgresql database. You’ll need to set this up before we start coding, because you’ll soon be writing things to a database. Postgres MySQL is a super friendly easy to learn relational database. But the hard way is more fun, right? And since it’s my tutorial, we’re going to be using Postgres
. For this application, there’s really nothing between the two so feel free to use either. I’m going to be using syntax from postgres though, because I never bothered to learn MySQL. Sue me!
If Eric failed that time round, you obviously don’t have postgres/psycopg2 installed. There’s lots of install guides which cover this so I’m not going to go into detail here. The psycopg2 homepage has a pretty detailed walkthrough that is easy enough to follow without any prior experience. The same goes for postgres. A word of warning though – set up a new user in your Linux system to run postgres. Many a postgres beginner has fallen foul of this.
Now lets do a checklist. You should have the following things installed:
- Postgresql
- Psycopg Adapter
- Python 2.6+
- Eric (Python IDE)
- All the usual Python modules (datetime, random, etc)
- BFPY – the python betfair library
- suds – SOAP in python (comes with bfpy)
Got all those? Good. If you have any problems along the way feel free to shoot me an email and I’ll try to help you out.
Before we get into anything Betfair specific, lets make sure our database connection is working.
import bfpy
import psycopg2
import time
import datetime
from time import localtime
import random
try:
conn = psycopg2.connect("dbname='betfair' user='postgres' host='localhost' password='mypass'");
except:
print "No connecty worky
"
Running the program will now check if psycopg can access your database. In Part 3 I’ll start looking at making simple API calls to Betfair and handling the results. I’ll look at what the aim of the bot is and the several paths to go down at this point. Stay tuned!
Related posts:
Hi,
Nice to see that someone is using the library (although I guess some others do without going public)
I would just like to point out two small things:
– bfpy has always been packaged as a zip file and never as tar.gz (although). The reasoning behind this: zip is a lot more common and Linux users should have no problem opening it.
– import suds shouldn’t be needed because suds is internally imported by bfpy
Should you have suggestions for bfpy improvements, do not hesitate to open a ticket/issue in the project page
And just another comment: anyone using bfpy is better off using the included copy of suds, because the version is patched to correct bugs (like one creating race conditions in multi-threaded environments)
Best regards
Thanks for your comments. I have updated the post with the corrections. Currently I am still working through porting an existing bot I have to your python library which should allow for much greater customisation in the future. It amazes me how on the betfair developers forum some people still insist on using VB.
Apologies I had hoped to get parts 3 and the rest out quickly but I’ve been snowed under at work. Hopefully early next year this series can continue.
Great Post, Very Informative Read. I have subscribed and look forward to being updated