From a69997a4b81eb0efa2034988ecb68135d810241e Mon Sep 17 00:00:00 2001 From: sarthakparashar Date: Sun, 7 Jul 2024 10:47:46 +0530 Subject: [PATCH] Error handeling and code optimization --- bot.py | 2 +- botscripts/action.py | 8 +++++--- botscripts/login.py | 2 +- scripts/template_subs.py | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index ee13a0d..01fb985 100644 --- a/bot.py +++ b/bot.py @@ -14,7 +14,7 @@ # Check whether the scriptname exist in the argument or not if len(sys.argv) < 2: - print('Script Name is missing: Try \'python3 bot.py scriptname\'') + print(f"Script Name is missing: Try 'python3 bot.py scriptname'") else: if sys.argv[1] == 'template_subs': diff --git a/botscripts/action.py b/botscripts/action.py index 1197554..ee9436e 100644 --- a/botscripts/action.py +++ b/botscripts/action.py @@ -21,8 +21,9 @@ def get_crsf_token(self): data = response.json() try: return data['query']['tokens']['csrftoken'] - except: - return None + except KeyError: + print("Error: CSRF token not found in the response.") + return None def get_pagecontent(self, page): """ Function to get the wikitext of Wikipage """ @@ -43,7 +44,8 @@ def get_pagecontent(self, page): else: print('\n' + page + ' - ' + data['error']['info']) return None - except: + except KeyError: + print(f"Error: Unexpected response format for page '{page}'.") return None diff --git a/botscripts/login.py b/botscripts/login.py index 6230d1b..9262614 100644 --- a/botscripts/login.py +++ b/botscripts/login.py @@ -46,7 +46,7 @@ def login(): data = response.json() if data['clientlogin']['status'] == 'PASS': - print('Login success! Welcome, ' + data['clientlogin']['username'] + '!') + print(f'Login success! Welcome, {data["clientlogin"]["username"]}!') return Ses else: print('Oops! Something went wrong -- ' + data['clientlogin']['messagecode']) diff --git a/scripts/template_subs.py b/scripts/template_subs.py index 2c5eb83..8b7f860 100644 --- a/scripts/template_subs.py +++ b/scripts/template_subs.py @@ -52,7 +52,7 @@ def run_template_subs(session): if oldtext != text: # Make and print the diff - print('\nDiff of Wikipage - ' + wikipage) + print(f"\nDiff of Wikipage - {wikipage}") diff(oldtext, text) if apply_all is False: