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

Commit

Permalink
HandlingErrors (#72)
Browse files Browse the repository at this point in the history
* Added class ErrorHandler: counts how many times error occurred and
if it was too often raises error.
Added logging error in try_load, try_parse functions

* Toggled to default value

* Removed constant HANDLE_ERRORS. Refactoring

* Refactoring

* Removed raise_error

* Removed variable result

* Refactoring

* Refactoring
  • Loading branch information
nadyr-mg authored and korovkinand committed Feb 9, 2018
1 parent 4a1b29a commit ba7266e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 18 deletions.
112 changes: 96 additions & 16 deletions surebet/bookmakers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from time import sleep
from requests import Session

from surebet import find_in_iter
from surebet.handling.surebets import *
from surebet.loading import try_load
from surebet.parsing import try_parse
Expand All @@ -16,13 +15,18 @@
INC_EVERY = 3
THRESHOLD_INC = 2

FORBIDDEN_INTERVAL = 30 * 60 # time in seconds

MAX_ERR_CNT = 5


class Posit:
def __init__(self, account=default_account):
from surebet.loading.posit import load, name
self.err_handler = ErrorHandler()

self.session = Session()
try_load(load, name, session=self.session, account=account)
self.account = account
self.load()

self.surebets = Surebets()

Expand All @@ -41,19 +45,35 @@ def __init__(self, account=default_account):
cur_iter += 1
sleep(LOAD_INTERVAL) # wait for positive to auto refresh page

def _add_new_surebets(self) -> int: # returns amount of newly added surebets
from surebet.loading.posit import load_events, name
from surebet.parsing.posit import parse
def load(self):
from surebet.loading.posit import load, name

@self.err_handler.handle_error
def _load():
try_load(load, name, session=self.session, account=self.account)

sample = try_load(load_events, name, session=self.session)
new_surebets = try_parse(parse, sample, name)
return _load()

def _add_new_surebets(self) -> int: # returns amount of newly added surebets
new_surebets = self.get_new_surebets()

self._decrease_marks()
new_added = self._merge_surebets(new_surebets)
self.surebets.format()

return new_added

def get_new_surebets(self):
from surebet.loading.posit import load_events, name
from surebet.parsing.posit import parse

@self.err_handler.handle_error
def _get_new_surebets():
sample = try_load(load_events, name, session=self.session)
return try_parse(parse, sample, name)

return _get_new_surebets()

def _decrease_marks(self):
for book in self.surebets.books_surebets:
for sport in book.attrs_dict().values():
Expand All @@ -68,6 +88,9 @@ def _decrease_marks(self):
def _merge_surebets(self, new_surebets) -> int: # returns amount of newly added surebets
new_added = 0

if new_surebets is None:
return new_added

for new_book in new_surebets.books_surebets:
book = find_in_iter(self.surebets.books_surebets, new_book)

Expand Down Expand Up @@ -103,32 +126,89 @@ def load_events(self):

class Fonbet:
def __init__(self):
from surebet.loading.fonbet import load, name
self.err_handler = ErrorHandler()

self.selenium = SeleniumService().new_instance()
try_load(load, name, browser=self.selenium.browser)
self.load()

def load(self):
from surebet.loading.fonbet import load, name

@self.err_handler.handle_error
def _load():
try_load(load, name, browser=self.selenium.browser)

return _load()

def load_events(self, bookmaker):
from surebet.loading.fonbet import load_events, name
from surebet.parsing.fonbet import parse

sample = try_load(load_events, name, browser=self.selenium.browser)
try_parse(parse, sample, name, bookmaker=bookmaker)
@self.err_handler.handle_error
def _load_events():
sample = try_load(load_events, name, browser=self.selenium.browser)
try_parse(parse, sample, name, bookmaker=bookmaker)

return _load_events()


class Marat:
def __init__(self):
self.err_handler = ErrorHandler()

def load_events(self, bookmaker):
from surebet.loading.marat import load_events, name
from surebet.parsing.marat import parse

sample = try_load(load_events, name)
try_parse(parse, sample, name, bookmaker=bookmaker)
@self.err_handler.handle_error
def _load_events():
sample = try_load(load_events, name)
try_parse(parse, sample, name, bookmaker=bookmaker)

return _load_events()


class Olimp:
def __init__(self):
self.err_handler = ErrorHandler()

def load_events(self, bookmaker):
from surebet.loading.olimp import load_events, name
from surebet.parsing.olimp import parse

sample = try_load(load_events, name)
try_parse(parse, sample, name, bookmaker=bookmaker)
@self.err_handler.handle_error
def _load_events():
sample = try_load(load_events, name)
try_parse(parse, sample, name, bookmaker=bookmaker)

return _load_events()


class ErrorHandler:
def __init__(self):
self.error_cnt = 0
self.first_occurred = 0

def handle_error(self, func):
def wrapper():
try:
return func()
except Exception as err:
# if it wasn't a forced stop of a program
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:
raise
else:
self.error_cnt += 1
else:
# refresh counter and measure time of first occurrence
self.error_cnt = 1
self.first_occurred = default_timer()
else:
raise

return None

return wrapper
4 changes: 3 additions & 1 deletion surebet/loading/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def try_load(load_func, site_name, **kwargs):
try:
result = load_func(**kwargs)
except Exception as err:
if not isinstance(err, KeyboardInterrupt): # if that wasn't a forced stopping of a program
if not isinstance(err, KeyboardInterrupt): # if it wasn't a forced stop of a program
logging.info("error occurred in loading({}): {}".format(site_name, str(err)))

filename = os.path.join(project_dir, "error-loading-{}".format(site_name))
with open(filename, "w") as out:
out.write(format_exc())
Expand Down
5 changes: 4 additions & 1 deletion surebet/parsing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from re import search

import os
import logging
from traceback import format_exc

from surebet import project_dir
Expand All @@ -11,7 +12,9 @@ def try_parse(parse_func, source, site_name, **kwargs):
try:
result = parse_func(source, **kwargs)
except Exception as err:
if not isinstance(err, KeyboardInterrupt): # if that wasn't a forced stopping of a program
if not isinstance(err, KeyboardInterrupt): # if it wasn't a forced stop of a program
logging.info("error occurred in parsing({}): {}".format(site_name, str(err)))

filename = os.path.join(project_dir, "error-parsing-{}".format(site_name))
with open(filename, "w") as out:
out.write(format_exc())
Expand Down

0 comments on commit ba7266e

Please sign in to comment.