Remove this ad
avatar

traderbetf

rookie botter

Posts: 3 Member Since: February 22, 2011

Lead

February 22, 2011 13:20:29

Tags : :

Hi,
I am trying to login to the betfair website using python with the mechanize module but struggling to login. Has anyone done this who would be willing to share their code? If not python login code for vb.net or any language would help me figure it out for python.
thanks.

Quote    Reply   
Remove this ad
Remove this ad
avatar

fred77

Botting Guru

Posts: 199 Member Since:May 14, 2008

#1 [url]

February 22, 2011 22:23:05

I don't have any python but here's some pseudo code stripped from a working bot.

 # get homepage to get default insecure cookies
 GET 'http://www.betfair.com/'

 # get uk LocalAccountSummary for secure cookies / to see if we're already logged in / to get account funds
 GET 'https://uk.site.sports.betfair.com/wallet/LocalAccountSummaryWalletView.do?productId=1'
 EXTRACT JSON from response.
 # If the following are available and have appropriate values then we're logged in already:
 # json['availableToWithdraw'] json['canViewLivePrices'] json['canViewLivePrices']
  .......
  .......
 # otherwise

  # Existing login not working? Attempt login
  POSTDATA = 'username=xxxxxxxx&password=xxxxxxxx'
  POST 'https://accountservices.betfair.com/account/login/LoginRequest.do'

  # If the above POST redirects to 'http://accountservices.betfair.com/account/login/LoginResponse.do?status=success' then login was successful
   # if not already fetched then explicitly get redirect page incase there are any updated cookies
   GET 'http://accountservices.betfair.com/account/login/LoginResponse.do?status=success'

Quote    Reply   
avatar

raistlin

rookie botter

Posts: 3 Member Since:December 25, 2010

#2 [url]

February 23, 2011 12:40:22

Hi, I'm no Python guru, in fact this is the first script I've ever written in it but it works. At least two months ago it worked, I've been so busy with other stuff since then, I couldn't work on our bots. Below is the snippet of a code you need to log into BetFair. Note, that you need to import modified httplib and urllib2 libraries from coda_network because the default ones don't support keep-alive connection. Apart from that, all you need to do is write your login and password instead of ('LOGIN', 'PASS') on the last line. If you pass debugLevel "1" instead of "0" to the WebAPI constructor, you get more info on the command line about what's going on.

--

#!/usr/bin/python
#python 2.6.4

import httplib # coda_network modification
import urllib2 # coda_network modification
import cookielib
import mimetools
import itertools
import mx.DateTime
import re

class WebAPI:

  USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100218 Fedora/3.6.1-2.fc14 Firefox/3.6.12'
  REFERER = 'http://sports.betfair.com/en/'

  def __init__(self, debugLevel):
    cookies = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel = debugLevel), urllib2.HTTPCookieProcessor(cookies))
    urllib2.install_opener(opener)
    try:
      urllib2.urlopen('http://www.betfair.com/')
    except urllib2.HTTPError, err:
      print mx.DateTime.localtime(), 'WebAPI initialisation failed:', err.code
      raise Error(0)
    except urllib2.URLError, err:
      print mx.DateTime.localtime(), 'WebAPI initialisation failed:', err.reason
      raise Error(1)
    else:
      print mx.DateTime.localtime(), 'WebAPI has been initialised with debug level %d.' % debugLevel

  def login(self, username, password):
    data = 'username=%s&password=%s' % (username, password)
    request = urllib2.Request('https://accountservices.betfair.com/account/login/LoginRequest.do')
    request.add_header('Connection', 'keep-alive')
    request.add_header('User-agent', self.USER_AGENT)
    request.add_header('Referer', self.REFERER)
    request.add_header('Content-Length',len(data))
    request.add_data(data)
    try:
      response = urllib2.urlopen(request)
    except urllib2.HTTPError, err:
      print mx.DateTime.localtime(), 'WebAPI login failed:', err.code
      raise Error(0)
    except urllib2.URLError, err:
      print mx.DateTime.localtime(), 'WebAPI login failed:', err.reason
      raise Error(1)
    else:
      if 'success' in response.geturl():
        print mx.DateTime.localtime(), 'WebAPI login successful.'
      else:
        print mx.DateTime.localtime(), 'WebAPI login failed: Unknown Reason.'
        raise Error(1)  

class Error(Exception):

  def __init__(self, value):
    self.value = value

  def __str__ (self):
    return repr(self.value)

webAPI = webapi.WebAPI(0)
webAPI.login('LOGIN', 'PASS')

Quote    Reply   
avatar

traderbetf

rookie botter

Posts: 3 Member Since:February 22, 2011

#3 [url]

February 23, 2011 14:36:42

Thank you for the replies. I managed to log in using VB6 by changing 'birchy''s code for the new urls. Cobbling something today in that before trying to do it in python in a few weeks. With the api operating at variable and slow speeds I have come to the conclusion that a hybrid robot is the way to go.

Quote    Reply   
avatar

otter

rookie botter

Posts: 8 Member Since:October 3, 2009

#4 [url]

February 23, 2011 15:22:55

Hi Traderbetf,

I am interested in why you think scraping the web-site is going to be better than the API. 

Do you have any reason (or even better, measurements) to suggest that it is faster or less jittery than the API?

Otter

p.s. Someone has published the source to Python BetFair API, but it is a little buggy, not complete and doesn't seem to be maintained, which is a shame.
http://code.google.com/p/pythonbetfairlibrary/

Quote    Reply   
avatar

traderbetf

rookie botter

Posts: 3 Member Since:February 22, 2011

#5 [url]

February 23, 2011 16:39:06

I can only speak from my own experience the last few weeks. I have found response times quicker from the website than the api. Of course there could be other times that api is faster. Monitoring response times and using the quickest way to get the data is important for a system that relies in getting a high proportion of fill or kill bets matched. Otherwise you only match the losers. The website also has different data available e.g. prices and p&l available in one call (allbeit calls are weighted 2 rather than one).

Quote    Reply   
Remove this ad
avatar

jimmybob

rookie botter

Posts: 15 Member Since:August 28, 2013

#8 [url]

June 27, 2014 20:49:43

You mean a solution to automatically logging on to the Betfair website? I think nearly everyone here is doing it in some way or another. Do you want to use the official API? If so, the new one (API-NG) or the old soap interface? If not, why not?

Quote    Reply   
Add Reply

Quick Reply

bbcode help