From 853574bebc35fdbf8d9d9efe3e0ae6e6936dc0fb Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Sun, 18 Aug 2024 20:33:11 +0000 Subject: [PATCH 1/2] Improve logic for login errors --- src/instawebhooks/__main__.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index 218bce2..eac2a22 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -14,7 +14,8 @@ try: from aiohttp import ClientSession except ModuleNotFoundError as exc: - raise SystemExit("Aiohttp not found.\n pip install [--user] aiohttp") from exc + raise SystemExit( + "Aiohttp not found.\n pip install [--user] aiohttp") from exc try: from discord import Embed, File, SyncWebhook @@ -93,7 +94,8 @@ def closure_check_regex(arg_value): help="don't show the post embed and only send message content", action="store_true", ) -parser.add_argument("--version", action="version", version="%(prog)s " + version) +parser.add_argument("--version", action="version", + version="%(prog)s " + version) args = parser.parse_args() # Set the logger to debug if verbose is enabled @@ -104,21 +106,10 @@ def closure_check_regex(arg_value): # Log the start of the program logger.info("Starting InstaWebhooks...") -# Check if we need to sign in to access the Instagram profile -try: - Profile.from_username(Instaloader().context, args.instagram_username).get_posts() -except KeyboardInterrupt: - print("\nInterrupted by user.") - sys.exit(0) -except LoginRequiredException as exc: - logger.critical("instaloader: error: %s", exc) - raise SystemExit( - "Not logged into Instaloader.\n instaloader --login YOUR-USERNAME" - ) from exc - # Ensure that a message content is provided if no embed is enabled if args.no_embed and args.message_content == "": - logger.critical("error: Cannot send an empty message. No message content provided.") + logger.critical( + "error: Cannot send an empty message. No message content provided.") raise SystemExit( "Please provide a message content with the --message-content flag." ) @@ -233,7 +224,8 @@ async def check_for_new_posts(): lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts) ): new_posts_found = True - logger.debug("New post found: https://www.instagram.com/p/%s", post.shortcode) + logger.debug( + "New post found: https://www.instagram.com/p/%s", post.shortcode) await send_to_discord(post) sleep(2) # Avoid 30 requests per minute rate limit @@ -255,6 +247,11 @@ def main(): while True: asyncio.run(check_for_new_posts()) sleep(args.refresh_interval) + except LoginRequiredException as login_exc: + logger.critical("instaloader: error: %s", login_exc) + raise SystemExit( + "Not logged into Instaloader.\n instaloader --login YOUR-USERNAME" + ) from login_exc except KeyboardInterrupt: print("\nInterrupted by user.") sys.exit(0) From b367364762adfbcb01c9373a0a0d9c286af0c3f0 Mon Sep 17 00:00:00 2001 From: Ryan Luu Date: Sun, 18 Aug 2024 20:33:47 +0000 Subject: [PATCH 2/2] Fix formatting --- src/instawebhooks/__main__.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index eac2a22..51d4352 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -14,8 +14,7 @@ try: from aiohttp import ClientSession except ModuleNotFoundError as exc: - raise SystemExit( - "Aiohttp not found.\n pip install [--user] aiohttp") from exc + raise SystemExit("Aiohttp not found.\n pip install [--user] aiohttp") from exc try: from discord import Embed, File, SyncWebhook @@ -94,8 +93,7 @@ def closure_check_regex(arg_value): help="don't show the post embed and only send message content", action="store_true", ) -parser.add_argument("--version", action="version", - version="%(prog)s " + version) +parser.add_argument("--version", action="version", version="%(prog)s " + version) args = parser.parse_args() # Set the logger to debug if verbose is enabled @@ -108,8 +106,7 @@ def closure_check_regex(arg_value): # Ensure that a message content is provided if no embed is enabled if args.no_embed and args.message_content == "": - logger.critical( - "error: Cannot send an empty message. No message content provided.") + logger.critical("error: Cannot send an empty message. No message content provided.") raise SystemExit( "Please provide a message content with the --message-content flag." ) @@ -224,8 +221,7 @@ async def check_for_new_posts(): lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts) ): new_posts_found = True - logger.debug( - "New post found: https://www.instagram.com/p/%s", post.shortcode) + logger.debug("New post found: https://www.instagram.com/p/%s", post.shortcode) await send_to_discord(post) sleep(2) # Avoid 30 requests per minute rate limit