Skip to content

Commit

Permalink
Documentation and linting updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartcode authored Jan 22, 2020
2 parents 9041fb7 + b53406d commit 5f11187
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Package to determine sure bets.
"""
3 changes: 3 additions & 0 deletions src/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
API methods.
"""
22 changes: 12 additions & 10 deletions src/api/odds.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@


class OddsAPI:
"""Connect to the-odds-api."""

def __init__(self, config_file: Optional[str] = None):
"""
Initialise odds API.
:param config_file: Path to config file.
"""
config = load_config(config_file if config_file else os.path.join(sys.prefix, 'autobet', 'config.yml'))

self._KEY = config.get('api', {}).get('key', '')
self._URL = config.get('api', {}).get('url', '')
self._REGION = config.get('api', {}).get('region', 'eu')
self._SITES = config.get('api', {}).get('sites', [])
self._key = config.get('api', {}).get('key', '')
self._url = config.get('api', {}).get('url', '')
self._region = config.get('api', {}).get('region', 'eu')
self._sites = config.get('api', {}).get('sites', [])

def request(self, method: str, **params) -> Any:
"""
Request a URL of the API.
:param method: Either odds or sports.
:return: Response dictionary.
"""
LOGGER.info('Called %s with params: %s', os.path.join(self._URL, f'{method}'), params)
LOGGER.info('Called %s with params: %s', os.path.join(self._url, f'{method}'), params)

if method not in ['sports', 'odds']:
raise NotImplementedError(f'Request method {method} does not exist.')

response = requests.get(
os.path.join(self._URL, f'{method}'),
params={**{'api_key': self._KEY}, **params}
os.path.join(self._url, f'{method}'),
params={**{'api_key': self._key}, **params}
)

if response.status_code == 200:
Expand Down Expand Up @@ -71,20 +73,20 @@ def get_odds(self, sport: str) -> List[Odds]:
"""
Get list of odds for a specific sport.
:param sport: Sports keys
:return: List of odds.
:return: List of odds.
"""""
sports_keys = self.get_sports_key(sport)

odds = []

for key in sports_keys:
if input(f'Would you like to retrieve data for {key}? [Y/n]').lower() in ['', 'y', 'yes']:
odds_list = self.request('odds', sport=key, region=self._REGION)
odds_list = self.request('odds', sport=key, region=self._region)

for odds_item in odds_list:
odds_item['sites'] = [Site(**s)
for s in odds_item['sites']
if s['site_key'] in self._SITES or not self._SITES]
if s['site_key'] in self._sites or not self._sites]

odds.append(Odds(**odds_item))

Expand Down
3 changes: 3 additions & 0 deletions src/autobet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Autobet module.
"""
16 changes: 10 additions & 6 deletions src/autobet/autobet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@

@dataclass
class AutoBetStats:
"""Allow easily accessible statistics object creation"""
min_index: int
min_value: float
max_index: int
max_value: float

def __gt__(self, other: AutoBetStats) -> bool:
return self.min_value > other.min_value

def __lt__(self, other: AutoBetStats) -> bool:
return not self.min_value > other.min_value

def sure_bet(self, other: AutoBetStats) -> Tuple[bool, int, int]:
"""Determine whether a sure bet can be found"""
sure_bet_max = 1 / self.max_value + 1 / other.max_value

if sure_bet_max < 1:
Expand All @@ -34,21 +30,29 @@ def sure_bet(self, other: AutoBetStats) -> Tuple[bool, int, int]:
return False, 0, 0

def sure_bet_profit(self, other: AutoBetStats) -> float:
"""Determine the most profitable bet."""
return 1 - (1 / self.max_value + 1 / other.max_value)


class AutoBet:
"""
Automatically find the right bets.
"""

def __init__(self, odds: List[Odds]):
"""Initialise instance."""
self._odds = odds

@staticmethod
def minmax(odds: List[float]) -> AutoBetStats:
"""Find the minimum and maximum values for the odds across all sites."""
min_index, min_value = min(enumerate(odds), key=lambda p: p[1])
max_index, max_value = max(enumerate(odds), key=lambda p: p[1])

return AutoBetStats(min_index, min_value, max_index, max_value)

def make_suggestions(self) -> List[Dict[str, Any]]:
"""Make suggestions for sure bets."""
suggestions = []
for match in self._odds:
if match.sites:
Expand Down
3 changes: 3 additions & 0 deletions src/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Additional required components.
"""
4 changes: 4 additions & 0 deletions src/components/odds.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

@dataclass
class Site:
"""
Site object
"""
site_key: str
site_nice: str
last_update: int
Expand All @@ -32,4 +35,5 @@ class Odds:

@property
def away_team(self):
"""Find away team"""
return self.teams[abs(self.teams.index(self.home_team) - 1)]

0 comments on commit 5f11187

Please sign in to comment.