Skip to content

Allow multiple webhooks and Instagram accounts #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/source/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <usage.html>`_.

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 ``<INSTAGRAM_USERNAME_1>``, ``<INSTAGRAM_USERNAME_2>``, etc. with the usernames of the Instagram accounts you want to monitor and ``<DISCORD_WEBHOOK_URL_1>``, ``<DISCORD_WEBHOOK_URL_2>``, etc. with the Discord webhook URLs you want to send new posts to.

.. code:: console

$ instawebhooks <INSTAGRAM_USERNAME_1> <INSTAGRAM_USERNAME_2> ... <DISCORD_WEBHOOK_URL_1> <DISCORD_WEBHOOK_URL_2> ...

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.
6 changes: 6 additions & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ Examples

$ instawebhooks -e -c "New post from {owner_name}: {post_url}" <INSTAGRAM_USERNAME> <DISCORD_WEBHOOK_URL>

* Send new posts for multiple Instagram usernames and Discord webhook URLs:

.. code:: console

$ instawebhooks <INSTAGRAM_USERNAME_1> <INSTAGRAM_USERNAME_2> ... <DISCORD_WEBHOOK_URL_1> <DISCORD_WEBHOOK_URL_2> ...

Reference
---------

Expand Down
86 changes: 44 additions & 42 deletions src/instawebhooks/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,65 +145,67 @@ 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):
"""Check for new Instagram posts and send them to Discord"""

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():
Expand Down
6 changes: 4 additions & 2 deletions src/instawebhooks/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down