diff --git a/src/instawebhooks.py b/src/instawebhooks.py index a52b9f7..d7ca49b 100644 --- a/src/instawebhooks.py +++ b/src/instawebhooks.py @@ -1,10 +1,13 @@ """ -Placeholder +Get new Instagram posts from any Instagram profile and send them to Discord using webhooks. """ import re import logging import argparse +from time import sleep +from instaloader import Instaloader, Profile +import requests def instagram_username(arg_value): @@ -12,7 +15,7 @@ def instagram_username(arg_value): pattern = re.compile(r'^[a-zA-Z_](?!.*?\.{2})[\w.]{1,28}[\w]$') if not pattern.match(arg_value): raise argparse.ArgumentTypeError( - f"invalid value: '{arg_value}': must meet Instagram username requirements") + f"invalid username value: '{arg_value}': must meet Instagram username requirements") return arg_value @@ -22,7 +25,7 @@ def discord_webhook_url(arg_value): r'^.*(discord|discordapp)\.com\/api\/webhooks\/([\d]+)\/([a-zA-Z0-9_.-]*)$') if not pattern.match(arg_value): raise argparse.ArgumentTypeError( - f"invalid value: '{arg_value}': must be a valid Discord webhook URL") + f"invalid url value: '{arg_value}': must be a valid Discord webhook URL") return arg_value @@ -37,7 +40,7 @@ def discord_webhook_url(arg_value): parser = argparse.ArgumentParser( prog='instawebhooks', description='Monitor Instagram accounts for new posts and send them to a Discord webhook', - epilog='More documentation can be found at: https://github.com/RaenLua/InstaWebhooks') + epilog='Documentation: https://github.com/RaenLua/InstaWebhooks') parser.add_argument("instagram_username", help="the Instagram username to monitor for new posts", type=instagram_username) @@ -46,6 +49,9 @@ def discord_webhook_url(arg_value): type=discord_webhook_url) parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") +parser.add_argument("-i", "--refresh-interval", + help="time in seconds to wait before checking for new posts again", + type=int, default=3600) args = parser.parse_args() # Set the logger to debug if verbose is enabled @@ -53,5 +59,71 @@ def discord_webhook_url(arg_value): logger.setLevel(logging.DEBUG) logger.debug("Verbose output enabled.") -logger.info("Starting InstaWebhooks for %s on %s", +logger.info("Starting InstaWebhooks for https://www.instagram.com/%s on %s", args.instagram_username, args.discord_webhook_url) + +# Instaloader instance +L = Instaloader() +profile = Profile.from_username(L.context, args.instagram_username) + + +def send_to_discord(post): + """Send a new Instagram post to Discord using a webhook.""" + + post_url = "https://instagram.com/p/" + post.shortcode + "/" + image_url = post.url + author_name = profile.username + author_icon_url = profile.profile_pic_url + post_description = post.caption + post_timestamp = post.date + author_fullname = profile.full_name + + icon_url = "https://www.instagram.com/static/images/ico/favicon-192.png/68d99ba29cc8.png" + data = { + "content": f"{post_url}", + "embeds": [ + { + "title": author_fullname, + "description": post_description, + "url": post_url, + "color": 13500529, + "timestamp": post_timestamp.strftime("%Y-%m-%dT%H:%M:%S"), + "author": { + "name": author_name, + "url": f"https://www.instagram.com/{author_name}/", + "icon_url": author_icon_url + }, + "footer": { + "text": "Instagram", + "icon_url": icon_url + }, + "image": { + "url": image_url + } + } + ], + "attachments": [] + } + + response = requests.post(args.discord_webhook_url, json=data, timeout=10) + logger.info("Post sent to Discord: %s", response.status_code) + + +def check_for_new_posts(): + """Check for new Instagram posts and send them to Discord.""" + + logger.info('Checking for new posts: https://www.instagram.com/%s', + args.instagram_username) + + posts = profile.get_posts() + + for post in posts: + logger.info('New post found: https://instagram.com/p/%s', + post.shortcode) + send_to_discord(post) + break + + +if __name__ == "__main__": + check_for_new_posts() + sleep(args.refresh_interval)