betcode-org / betfair

betfairlightweight - Betfair API-NG python wrapper (with streaming)
MIT License
436 stars 152 forks source link

marketType field not always returned from calls to list_market_catalogue() #200

Closed pjmcdermott closed 5 years ago

pjmcdermott commented 5 years ago

Calls to list_market_catalogue() with a large value for max_results results in a exception during processing of the returned data (at least today, 2018-12-15):

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import betfairlightweight
>>> import os
>>> API=betfairlightweight.APIClient(os.environ['BETFAIR_USER'], os.environ['BETFAIR_PASSWORD'],app_key=os.environ['BETFAIR_APPKEY'], cert_files=os.environ['BETFAIR_CERTS'])
>>> API.login()
<LoginResource>
>>> sport_filter=betfairlightweight.filters.market_filter(event_type_ids=[1], in_play_only=False)
>>> markets=API.betting.list_market_catalogue(filter=sport_filter,max_results=100,market_projection=['COMPETITION', 'EVENT', 'EVENT_TYPE', 'MARKET_DESCRIPTION', 'RUNNER_DESCRIPTION'])
Traceback (most recent call last):
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 107, in process_response
    return [resource(elapsed_time=elapsed_time, **x) for x in result]
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 107, in <listcomp>
    return [resource(elapsed_time=elapsed_time, **x) for x in result]
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/resources/bettingresources.py", line 257, in __init__
    kwargs.get('description') else None
TypeError: __init__() missing 1 required positional argument: 'marketType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/betting.py", line 158, in list_market_catalogue
    return self.process_response(response, resources.MarketCatalogue, elapsed_time, lightweight)
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 109, in process_response
    raise InvalidResponse(response=result)
betfairlightweight.exceptions.InvalidResponse: Invalid response received: [{'marketId': '1.145994605', 'marketName': 'Winner 2018/2019', 'description': {'persistenceEnabled': True, 'bspMarket': False, 'marketTime': '2013-09-19T16:00:00.000Z', 'suspendTime': '2013-09-19T16:00:00.000Z', 'bettingType': 'ODDS', 'turnInPlayEnabled': True, 'marketType': 'WINNER', 'regulator': 'GIBRALTAR REGULATOR', 'marketBaseRate': 5.0, 'discountAllowed': True, 'wallet': 'UK wallet', 'rules': '<b>Market Information</b><br>For further information please see <a href=http://content.betfair.com/aboutus/content.asp?sWhichKey=Rules%20and%20Regulations#undefined.do style=color:0163ad; text-decoration: underline; target=_blank>Rules & Regs</a>.<br><br> Which team will win this Cup competition? At the start of the tournament all unmatched bets

betfairlightweight appears to think that the API response is invalid.

Interestingly, using a small number for max_results(=20) appears to work:

>>> markets=API.betting.list_market_catalogue(filter=sport_filter,max_results=20,market_projection=['COMPETITION', 'EVENT', 'EVENT_TYPE', 'MARKET_DESCRIPTION', 'RUNNER_DESCRIPTION'])
>>> len(markets)
20

Possibly the issue is with a response for a specific market?

In fact, the call works for a range of values for max_length up to 39, suggesting that it may be an issue with the response for the 40th market today (2018-12-15 at 20:52 GMT):

>>> markets=API.betting.list_market_catalogue(filter=sport_filter,max_results=39,market_projection=['COMPETITION', 'EVENT', 'EVENT_TYPE', 'MARKET_DESCRIPTION', 'RUNNER_DESCRIPTION'])
>>> len(markets)
39
pjmcdermott commented 5 years ago

Further debugging has narrowed it down to an issue with the MARKET_DESCRIPTION response for market_id '1.152169316':

>>> sport_filter=betfairlightweight.filters.market_filter(event_type_ids=[1], in_play_only=False, market_ids=['1.152169316'])
>>> market=API.betting.list_market_catalogue(filter=sport_filter,market_projection=['MARKET_DESCRIPTION'])
Traceback (most recent call last):
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 107, in process_response
    return [resource(elapsed_time=elapsed_time, **x) for x in result]
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 107, in <listcomp>
    return [resource(elapsed_time=elapsed_time, **x) for x in result]
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/resources/bettingresources.py", line 257, in __init__
    kwargs.get('description') else None
TypeError: __init__() missing 1 required positional argument: 'marketType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/betting.py", line 158, in list_market_catalogue
    return self.process_response(response, resources.MarketCatalogue, elapsed_time, lightweight)
  File "/home/paul/.virtualenvs/tf_betting/lib/python3.6/site-packages/betfairlightweight/endpoints/baseendpoint.py", line 109, in process_response
    raise InvalidResponse(response=result)
betfairlightweight.exceptions.InvalidResponse: Invalid response received: [{'marketId': '1.152169316', 'marketName': 'Top On Christmas Day 2018', 'description': {'persistenceEnabled': True, 'bspMarket': False, 'marketTime': '2018-08-20T18:00:00.000Z', 'suspendTime': '2018-08-20T18:00:00.000Z', 'bettingType': 'ODDS', 'turnInPlayEnabled': True, 'regulator': 'GIBRALTAR REGULATOR', 'marketBaseRate': 5.0, 'discountAllowed': True, 'wallet': 'UK wallet', 'rules': '<b>Market Information</b><br>For further information please see <a href=http://content.betfair.com/aboutus/content.asp?sWhichKey=Rules%20and%20Regulations#undefined.do style=color:0163ad; text-decoration: underline; target=_blank>Rules & Regs</a>.<br><br> Which team will be top of this league on the 25th December? The market will be settled based on league position as of 25th December regardless of games played and any postponements/abandonments. At the start of this league all unmatched bets will be cancelled and the market reopened. Customers are responsible for managing their own positions at all times in this market. Competition Rules Apply.<br><br>', 'rulesHasDate': False, 'priceLadderDescription': {'type': 'CLASSIC'}}, 'totalMatched': 56.58}]

It seems to me the issue is that betfairlightweight assumes that the marketType field is mandatory, when this is not always the case.

liampauling commented 5 years ago

According to the docs it is mandatory field on the response so this looks like a Betfair error but to be honest this isn’t the first time we have had something like this.