Skip to content

Commit

Permalink
Add missing code
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanLua authored Jul 14, 2024
1 parent 5a6d5a5 commit 26fbd02
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions src/instawebhooks.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
"""
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):
"""Instagram username type"""
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


Expand All @@ -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


Expand All @@ -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)
Expand All @@ -46,12 +49,81 @@ 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
if args.verbose:
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)

0 comments on commit 26fbd02

Please sign in to comment.