Skip to content
This repository has been archived by the owner on May 19, 2019. It is now read-only.

Commit

Permalink
DetailedSurebet (#79)
Browse files Browse the repository at this point in the history
* Added class DetailedSurebet, funcs _filter, _convert_to_detailed, convert_to_detailed.
In bookmakers.py: replaced sign > to >=.

* Updated main.py

* Updated main.py

* Updated main.py

* Refactoring

* Requested changes
  • Loading branch information
nadyr-mg authored and korovkinand committed Feb 9, 2018
1 parent ba7266e commit b695fd7
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 8 deletions.
2 changes: 1 addition & 1 deletion surebet/bookmakers.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def wrapper():
if not isinstance(err, KeyboardInterrupt):
# if first error and current error occurred within forbidden interval
if default_timer() - self.first_occurred < FORBIDDEN_INTERVAL:
if self.error_cnt > MAX_ERR_CNT:
if self.error_cnt >= MAX_ERR_CNT:
raise
else:
self.error_cnt += 1
Expand Down
88 changes: 88 additions & 0 deletions surebet/handling/detailed_surebets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from functools import cmp_to_key
from surebet.handling.surebets import *

MIN_PROFIT = 2.5
MAX_PROFIT = 10

MIN_LIFETIME = 10


class DetailedSurebet:
"""
Contain all information about surebet: bookmakers between which surebet is occurred,
teams for first and second event, part of event and wagers, profit, lifetime of surebet
"""

def __init__(self, books: BookSurebets, sport: str, events: EventSurebets, part: PartSurebets,
surebet: TimedSurebet):
"""
:param books: bookmakers between which surebet is occurred
:param sport: name of sport
:param events: events between which surebet is occurred
:param part: common part of events
"""
self.book1, self.book2 = books.book1, books.book2
self.sport = sport
self.teams1, self.teams2 = events.teams1, events.teams2
self.part = part.part
self.w1, self.w2 = surebet.w1, surebet.w2
self.profit = surebet.profit
self.lifetime = surebet.get_lifetime()

def __str__(self):
teams_sep = " vs "
event_name1 = teams_sep.join(self.teams1)
event_name2 = teams_sep.join(self.teams2)

common_pattern = "{sport:<6} | {book:<6} | {ev_name:<60} | part #{part} | {wager}"

str_form = "profit: {profit:<8} | ".format(profit=self.profit) + common_pattern.format(
sport=self.sport, book=self.book1, ev_name=event_name1, part=self.part, wager=self.w1) + "\n"
str_form += "lifetime: {time:<6} | ".format(time=self.lifetime) + common_pattern.format(
sport=self.sport, book=self.book2, ev_name=event_name2, part=self.part, wager=self.w2)
return str_form


def convert_to_detailed(surebets: Surebets) -> list:
"""Convert surebets to list of DetailedSurebet, filter that list and sort"""
detailed_surebets = _convert_to_detailed(surebets)

detailed_surebets = _filter(detailed_surebets)

_sort_detailed(detailed_surebets)

return detailed_surebets


def _filter(detailed_surebets) -> list:
"""Filter detailed_surebets by profit's lower and upper limits and by lifetime's lower limit"""

def predicate(item):
return MIN_PROFIT <= item.profit <= MAX_PROFIT and item.lifetime >= MIN_LIFETIME

return list(filter(predicate, detailed_surebets))


def _convert_to_detailed(surebets: Surebets) -> list:
"""Construct objects of class DetailedSurebet and put them in list detailed_surebets"""
detailed_surebets = []
for book in surebets.books_surebets:
for sport_name, sport in book.attrs_dict().items():
for event in sport:
for part in event.parts:
for surebet in part.surebets:
detailed_surebets.append(DetailedSurebet(book, sport_name, event, part, surebet))
return detailed_surebets


def _sort_detailed(detailed_surebets):
detailed_surebets.sort(key=cmp_to_key(_detailed_cmp), reverse=True)


def _detailed_cmp(first: DetailedSurebet, second: DetailedSurebet):
"""Custom comparator to sort detailed_surebets firstly by profit casted to int and secondly by lifetime"""
first_profit, second_profit = int(first.profit), int(second.profit)
if first_profit != second_profit:
return first_profit - second_profit
else:
return first.lifetime - second.lifetime
25 changes: 18 additions & 7 deletions surebet/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from surebet.bookmakers import Posit, Fonbet, Marat, Olimp
from surebet.handling.detailed_surebets import convert_to_detailed
from surebet.handling.excluding import exclude_posit
from surebet.handling.searching import find_surebets
from surebet.handling.surebets import Surebets
from surebet.loading.selenium import SeleniumService
from surebet.parsing.bets import Bookmakers

Expand All @@ -11,15 +13,24 @@ def start_scanning():
marat = Marat()
olimp = Olimp()

bookmakers = Bookmakers()
fonbet.load_events(bookmakers.fonbet)
marat.load_events(bookmakers.marat)
olimp.load_events(bookmakers.olimp)
old_surebets = Surebets()
for i in range(3):
bookmakers = Bookmakers()
fonbet.load_events(bookmakers.fonbet)
marat.load_events(bookmakers.marat)
olimp.load_events(bookmakers.olimp)

found_surebets = find_surebets(bookmakers)
posit_surebets = posit.load_events()
posit_surebets = posit.load_events()
surebets = find_surebets(bookmakers)

exclude_posit(found_surebets, posit_surebets)
exclude_posit(surebets, posit_surebets)

surebets.set_timestamps(old_surebets)
old_surebets = surebets

detailed_surebets = convert_to_detailed(surebets)
for detailed_surebet in detailed_surebets:
print(detailed_surebet)

SeleniumService.quit()

Expand Down

0 comments on commit b695fd7

Please sign in to comment.