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

Commit edbe723

Browse files
Refactor and improve logging
1 parent 21c7135 commit edbe723

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

sniper/__main__.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,22 @@ def read_config():
6868
return notification_config, customer
6969

7070

71-
async def main():
72-
colorama.init()
73-
print(const.HEADER)
74-
log_format = '%(asctime)s nvidia-sniper: %(message)s'
71+
def setup_logging():
72+
log_format = '%(asctime)s %(name)s: %(message)s'
7573
fh = logging.FileHandler('sniper.log', encoding='utf-8')
7674
sh = logging.StreamHandler(sys.stdout)
75+
logging.getLogger().name = 'nvidia-sniper'
7776
logging.basicConfig(level=logging.INFO,
7877
format=log_format, handlers=[fh, sh])
78+
logging.getLogger('WDM').disabled = True
79+
logging.getLogger('apprise').disabled = True
80+
logging.getLogger('apprise.URLBase').disabled = True
81+
82+
83+
async def main():
84+
colorama.init()
85+
print(const.HEADER)
86+
setup_logging()
7987

8088
notification_config, customer = read_config()
8189

sniper/notifications.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, notification_config, notification_queue, target_gpu):
2424

2525
def send_notifications(self, notification_type):
2626
for name, service in self.config['services'].items():
27-
logging.info(f'Sending notifications to {name}')
27+
logging.info(f'Sending {notification_type} notification to {name}...')
2828
apobj = apprise.Apprise()
2929
apobj.add(service['url'])
3030
title = f"{notification_type.replace('-',' ').title()} - {self.gpu['name']}"

sniper/webdriver.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,31 @@
1111
from webdriver_manager.firefox import GeckoDriverManager
1212
except Exception:
1313
logging.error(
14-
'Could not import all required modules. '\
15-
'Please run the following command again:\n\n'\
14+
'Could not import all required modules. '
15+
'Please run the following command again:\n\n'
1616
'\tpipenv install\n')
1717
exit()
1818

19-
def get_default_profile():
19+
20+
def get_profile_path():
2021
if platform == 'linux' or platform == 'linux2':
21-
mozilla_profile = Path(os.getenv('HOME')) / '.mozilla' / 'firefox'
22+
profile_path = Path(os.getenv('HOME')) / '.mozilla' / 'firefox'
2223
elif platform == 'darwin':
23-
mozilla_profile = Path(os.getenv('HOME')) / \
24+
profile_path = Path(os.getenv('HOME')) / \
2425
'Library' / 'Application Support' / 'Firefox'
2526
elif platform == 'win32':
26-
mozilla_profile = Path(os.getenv('APPDATA')) / 'Mozilla' / 'Firefox'
27-
if not mozilla_profile.exists():
28-
raise FileNotFoundError("Mozilla profile doesn't exist and/or can't be located on this machine.")
27+
profile_path = Path(os.getenv('APPDATA')) / 'Mozilla' / 'Firefox'
28+
if not profile_path.exists():
29+
raise FileNotFoundError(
30+
"Mozilla profile doesn't exist and/or can't be located on this machine.")
31+
return profile_path
32+
2933

30-
mozilla_profile_ini = mozilla_profile / 'profiles.ini'
34+
def get_default_profile(profile_path):
35+
mozilla_profile_ini = profile_path / 'profiles.ini'
3136
profile = configparser.ConfigParser()
3237
profile.read(mozilla_profile_ini)
33-
return mozilla_profile / profile.get('Profile0', 'Path')
38+
return profile.get('Profile0', 'Path')
3439

3540

3641
def prepare_sniper_profile(default_profile_path):
@@ -42,13 +47,16 @@ def prepare_sniper_profile(default_profile_path):
4247

4348

4449
def create():
45-
default_profile_path = get_default_profile()
46-
profile = prepare_sniper_profile(default_profile_path)
47-
driver = webdriver.Firefox(firefox_profile=profile, executable_path=GeckoDriverManager().install())
50+
profile_path = get_profile_path()
51+
default_profile = get_default_profile(profile_path)
52+
logging.info(f'Launching Firefox using default profile: {default_profile}')
53+
profile = prepare_sniper_profile(profile_path / default_profile)
54+
driver = webdriver.Firefox(
55+
firefox_profile=profile, executable_path=GeckoDriverManager().install())
4856
if os.path.isfile('./recaptcha_solver-5.7-fx.xpi'):
49-
logging.info('ReCaptcha solver detected, enabled')
57+
logging.info('ReCaptcha solver extension detected and installed')
5058
extension_path = os.path.abspath("recaptcha_solver-5.7-fx.xpi")
5159
driver.install_addon(extension_path, temporary=True)
5260
else:
53-
logging.info('ReCaptcha solver not found')
54-
return driver
61+
logging.info('ReCaptcha solver extension not found')
62+
return driver

0 commit comments

Comments
 (0)