diff --git a/docs/source/getting-started.rst b/docs/source/getting-started.rst index 9434871..293548e 100644 --- a/docs/source/getting-started.rst +++ b/docs/source/getting-started.rst @@ -76,3 +76,22 @@ Now, whenever the Instagram account `@raenlua` posts a new photo, it will be sen .. image:: _static/webhook-message.png For more information about using InstaWebhooks, see the `usage guide `_. + +Setting up InstaWebhooks for multiple Instagram accounts or webhooks +-------------------------------------------------------------------- + +You can also set up InstaWebhooks to monitor multiple Instagram accounts or send new posts to multiple Discord webhooks. + +Replace ````, ````, etc. with the usernames of the Instagram accounts you want to monitor and ````, ````, etc. with the Discord webhook URLs you want to send new posts to. + +.. code:: console + + $ instawebhooks ... ... + +It should look something like this: + +.. code:: console + + $ instawebhooks raenlua anotheruser https://discord.com/api/webhooks/0123456789/abcdefghijklmnopqrstuvwxyz https://discord.com/api/webhooks/9876543210/zyxwvutsrqponmlkjihgfedcba + +Now, whenever the Instagram accounts `@raenlua` or `@anotheruser` post a new photo, it will be sent to the specified Discord webhooks. diff --git a/docs/source/usage.rst b/docs/source/usage.rst index 73d7370..ffe2b62 100644 --- a/docs/source/usage.rst +++ b/docs/source/usage.rst @@ -40,6 +40,12 @@ Examples $ instawebhooks -e -c "New post from {owner_name}: {post_url}" +* Send new posts for multiple Instagram usernames and Discord webhook URLs: + +.. code:: console + + $ instawebhooks ... ... + Reference --------- diff --git a/src/instawebhooks/__main__.py b/src/instawebhooks/__main__.py index 09bbab2..d9b76cf 100644 --- a/src/instawebhooks/__main__.py +++ b/src/instawebhooks/__main__.py @@ -145,24 +145,25 @@ def format_message(post: Post): async def send_to_discord(post: Post): """Send a new Instagram post to Discord using a webhook""" - webhook = SyncWebhook.from_url(args.discord_webhook_url) + for webhook_url in args.discord_webhook_url: + webhook = SyncWebhook.from_url(webhook_url) - if args.message_content: - format_message(post) + if args.message_content: + format_message(post) - logger.debug("Sending post sent to Discord...") + logger.debug("Sending post sent to Discord...") - if not args.no_embed: - embed, post_image_file, profile_pic_file = await create_embed(post) - webhook.send( - content=args.message_content, - embed=embed, - files=[post_image_file, profile_pic_file], - ) - else: - webhook.send(content=args.message_content) + if not args.no_embed: + embed, post_image_file, profile_pic_file = await create_embed(post) + webhook.send( + content=args.message_content, + embed=embed, + files=[post_image_file, profile_pic_file], + ) + else: + webhook.send(content=args.message_content) - logger.info("New post sent to Discord successfully.") + logger.info("New post sent to Discord successfully.") async def check_for_new_posts(catchup: int = args.catchup): @@ -170,40 +171,41 @@ async def check_for_new_posts(catchup: int = args.catchup): logger.info("Checking for new posts") - posts = Profile.from_username( - Instaloader().context, args.instagram_username - ).get_posts() - since = datetime.now() until = datetime.now() - timedelta(seconds=args.refresh_interval) - new_posts_found = False - - async def send_post(post: Post): - logger.info("New post found: https://www.instagram.com/p/%s", post.shortcode) - await send_to_discord(post) - - if catchup > 0: - logger.info("Sending last %s posts on startup...", catchup) - posts_to_send: List[Post] = [] - for post in takewhile(lambda _: catchup > 0, posts): - posts_to_send.append(post) - catchup -= 1 - - # Reverse the posts to send oldest first - for post in reversed(posts_to_send): + for username in args.instagram_username: + posts = Profile.from_username(Instaloader().context, username).get_posts() + + new_posts_found = False + + async def send_post(post: Post): + logger.info( + "New post found: https://www.instagram.com/p/%s", post.shortcode + ) + await send_to_discord(post) + + if catchup > 0: + logger.info("Sending last %s posts on startup...", catchup) + posts_to_send: List[Post] = [] + for post in takewhile(lambda _: catchup > 0, posts): + posts_to_send.append(post) + catchup -= 1 + + # Reverse the posts to send oldest first + for post in reversed(posts_to_send): + await send_post(post) + sleep(2) # Avoid 30 requests per minute rate limit + + for post in takewhile( + lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts) + ): + new_posts_found = True await send_post(post) sleep(2) # Avoid 30 requests per minute rate limit - for post in takewhile( - lambda p: p.date > until, dropwhile(lambda p: p.date > since, posts) - ): - new_posts_found = True - await send_post(post) - sleep(2) # Avoid 30 requests per minute rate limit - - if not new_posts_found: - logger.info("No new posts found.") + if not new_posts_found: + logger.info("No new posts found.") def main(): diff --git a/src/instawebhooks/parser.py b/src/instawebhooks/parser.py index 4ff7d2d..8365f7d 100644 --- a/src/instawebhooks/parser.py +++ b/src/instawebhooks/parser.py @@ -33,15 +33,17 @@ def closure_check_regex(arg_value: str): login_group = parser.add_mutually_exclusive_group() parser.add_argument( "instagram_username", - help="the Instagram username to monitor for new posts", + help="the Instagram usernames to monitor for new posts", type=regex(r"^[a-zA-Z_](?!.*?\.{2})[\w.]{1,28}[\w]$"), + nargs="+", ) parser.add_argument( "discord_webhook_url", - help="the Discord webhook URL to send new posts to", + help="the Discord webhook URLs to send new posts to", type=regex( r"^.*(discord|discordapp)\.com\/api\/webhooks\/([\d]+)\/([a-zA-Z0-9_.-]*)$" ), + nargs="+", ) logging_group.add_argument( "-q", "--quiet", help="hide all logging", action="store_true"