Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Refactor and improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
philippnormann committed Oct 27, 2020
1 parent 21c7135 commit edbe723
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
16 changes: 12 additions & 4 deletions sniper/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,22 @@ def read_config():
return notification_config, customer


async def main():
colorama.init()
print(const.HEADER)
log_format = '%(asctime)s nvidia-sniper: %(message)s'
def setup_logging():
log_format = '%(asctime)s %(name)s: %(message)s'
fh = logging.FileHandler('sniper.log', encoding='utf-8')
sh = logging.StreamHandler(sys.stdout)
logging.getLogger().name = 'nvidia-sniper'
logging.basicConfig(level=logging.INFO,
format=log_format, handlers=[fh, sh])
logging.getLogger('WDM').disabled = True
logging.getLogger('apprise').disabled = True
logging.getLogger('apprise.URLBase').disabled = True


async def main():
colorama.init()
print(const.HEADER)
setup_logging()

notification_config, customer = read_config()

Expand Down
2 changes: 1 addition & 1 deletion sniper/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, notification_config, notification_queue, target_gpu):

def send_notifications(self, notification_type):
for name, service in self.config['services'].items():
logging.info(f'Sending notifications to {name}')
logging.info(f'Sending {notification_type} notification to {name}...')
apobj = apprise.Apprise()
apobj.add(service['url'])
title = f"{notification_type.replace('-',' ').title()} - {self.gpu['name']}"
Expand Down
40 changes: 24 additions & 16 deletions sniper/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,31 @@
from webdriver_manager.firefox import GeckoDriverManager
except Exception:
logging.error(
'Could not import all required modules. '\
'Please run the following command again:\n\n'\
'Could not import all required modules. '
'Please run the following command again:\n\n'
'\tpipenv install\n')
exit()

def get_default_profile():

def get_profile_path():
if platform == 'linux' or platform == 'linux2':
mozilla_profile = Path(os.getenv('HOME')) / '.mozilla' / 'firefox'
profile_path = Path(os.getenv('HOME')) / '.mozilla' / 'firefox'
elif platform == 'darwin':
mozilla_profile = Path(os.getenv('HOME')) / \
profile_path = Path(os.getenv('HOME')) / \
'Library' / 'Application Support' / 'Firefox'
elif platform == 'win32':
mozilla_profile = Path(os.getenv('APPDATA')) / 'Mozilla' / 'Firefox'
if not mozilla_profile.exists():
raise FileNotFoundError("Mozilla profile doesn't exist and/or can't be located on this machine.")
profile_path = Path(os.getenv('APPDATA')) / 'Mozilla' / 'Firefox'
if not profile_path.exists():
raise FileNotFoundError(
"Mozilla profile doesn't exist and/or can't be located on this machine.")
return profile_path


mozilla_profile_ini = mozilla_profile / 'profiles.ini'
def get_default_profile(profile_path):
mozilla_profile_ini = profile_path / 'profiles.ini'
profile = configparser.ConfigParser()
profile.read(mozilla_profile_ini)
return mozilla_profile / profile.get('Profile0', 'Path')
return profile.get('Profile0', 'Path')


def prepare_sniper_profile(default_profile_path):
Expand All @@ -42,13 +47,16 @@ def prepare_sniper_profile(default_profile_path):


def create():
default_profile_path = get_default_profile()
profile = prepare_sniper_profile(default_profile_path)
driver = webdriver.Firefox(firefox_profile=profile, executable_path=GeckoDriverManager().install())
profile_path = get_profile_path()
default_profile = get_default_profile(profile_path)
logging.info(f'Launching Firefox using default profile: {default_profile}')
profile = prepare_sniper_profile(profile_path / default_profile)
driver = webdriver.Firefox(
firefox_profile=profile, executable_path=GeckoDriverManager().install())
if os.path.isfile('./recaptcha_solver-5.7-fx.xpi'):
logging.info('ReCaptcha solver detected, enabled')
logging.info('ReCaptcha solver extension detected and installed')
extension_path = os.path.abspath("recaptcha_solver-5.7-fx.xpi")
driver.install_addon(extension_path, temporary=True)
else:
logging.info('ReCaptcha solver not found')
return driver
logging.info('ReCaptcha solver extension not found')
return driver

0 comments on commit edbe723

Please sign in to comment.