├── README.md ├── LICENSE └── instagram-discord.py /README.md: -------------------------------------------------------------------------------- 1 | # Instagram to discord post images 2 | 3 | This script executes 2 actions: 4 | 5 | 1. Monitors for new image posted in a instagram account. 6 | 2. If found new image, a bot posts new instagram image in a discord channel. 7 | 3. Repeat after set interval. 8 | 9 | ## Requirements: 10 | 11 | - Python v3 12 | - Python module re, json, requests 13 | 14 | ## Usage: 15 | 16 | Set environment variables: 17 | 18 | - Set IG_USERNAME to username account you want to monitor. Example: ladygaga 19 | - Set WEBHOOK_URL to Discord account webhook url. To know how, just Google: "how to create webhook discord". 20 | - Set TIME_INTERVAL to the time in seconds in between each check for a new post. Example: 1.5, 600 (default=600 = 10 minutes) 21 | 22 | How to setup enviroment variables: 23 | https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/ 24 | 25 | ## Collaborations: 26 | 27 | Collaborations to improve script are always welcome. 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Fernando 4 | Url: https://github.com/fernandod1/ 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /instagram-discord.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | # Copyright (c) 2020 Fernando 4 | # Url: https://github.com/fernandod1/ 5 | # License: MIT 6 | 7 | # DESCRIPTION: 8 | # This script executes 2 actions: 9 | # 1.) Monitors for new image posted in a instagram account. 10 | # 2.) If found new image, a bot posts new instagram image in a discord channel. 11 | # 3.) Repeat after set interval. 12 | 13 | # REQUIREMENTS: 14 | # - Python v3 15 | # - Python module re, json, requests 16 | import re 17 | import json 18 | import sys 19 | import requests 20 | import urllib.request 21 | import os 22 | import time 23 | 24 | # USAGE: 25 | # Set Environment Variables: 26 | # Set IG_USERNAME to username account you want to monitor. Example - ladygaga 27 | # Set WEBHOOK_URL to Discord account webhook url. To know how, just Google: "how to create webhook discord". 28 | # Set TIME_INTERVAL to the time in seconds in between each check for a new post. Example - 1.5, 600 (default=600) 29 | # Help: https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/ 30 | 31 | INSTAGRAM_USERNAME = os.environ.get('IG_USERNAME') 32 | 33 | # ----------------------- Do not modify under this line ----------------------- # 34 | 35 | 36 | def get_user_fullname(html): 37 | return html.json()["graphql"]["user"]["full_name"] 38 | 39 | 40 | def get_total_photos(html): 41 | return int(html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["count"]) 42 | 43 | 44 | def get_last_publication_url(html): 45 | return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["shortcode"] 46 | 47 | 48 | def get_last_photo_url(html): 49 | return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["display_url"] 50 | 51 | 52 | def get_last_thumb_url(html): 53 | return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["thumbnail_src"] 54 | 55 | 56 | def get_description_photo(html): 57 | return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"] 58 | 59 | 60 | def webhook(webhook_url, html): 61 | # for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook 62 | # for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object 63 | data = {} 64 | data["embeds"] = [] 65 | embed = {} 66 | embed["color"] = 15467852 67 | embed["title"] = "New pic of @"+INSTAGRAM_USERNAME+"" 68 | embed["url"] = "https://www.instagram.com/p/" + \ 69 | get_last_publication_url(html)+"/" 70 | embed["description"] = get_description_photo(html) 71 | # embed["image"] = {"url":get_last_thumb_url(html)} # unmark to post bigger image 72 | embed["thumbnail"] = {"url": get_last_thumb_url(html)} 73 | data["embeds"].append(embed) 74 | result = requests.post(webhook_url, data=json.dumps( 75 | data), headers={"Content-Type": "application/json"}) 76 | try: 77 | result.raise_for_status() 78 | except requests.exceptions.HTTPError as err: 79 | print(err) 80 | else: 81 | print("Image successfully posted in Discord, code {}.".format( 82 | result.status_code)) 83 | 84 | 85 | def get_instagram_html(INSTAGRAM_USERNAME): 86 | headers = { 87 | "Host": "www.instagram.com", 88 | "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11" 89 | } 90 | html = requests.get("https://www.instagram.com/" + 91 | INSTAGRAM_USERNAME + "/feed/?__a=1", headers=headers) 92 | return html 93 | 94 | 95 | def main(): 96 | try: 97 | html = get_instagram_html(INSTAGRAM_USERNAME) 98 | if(os.environ.get("LAST_IMAGE_ID") == get_last_publication_url(html)): 99 | print("Not new image to post in discord.") 100 | else: 101 | os.environ["LAST_IMAGE_ID"] = get_last_publication_url(html) 102 | print("New image to post in discord.") 103 | webhook(os.environ.get("WEBHOOK_URL"), 104 | get_instagram_html(INSTAGRAM_USERNAME)) 105 | except Exception as e: 106 | print(e) 107 | 108 | 109 | if __name__ == "__main__": 110 | if os.environ.get('IG_USERNAME') != None and os.environ.get('WEBHOOK_URL') != None: 111 | while True: 112 | main() 113 | time.sleep(float(os.environ.get('TIME_INTERVAL') or 600)) # 600 = 10 minutes 114 | else: 115 | print('Please configure environment variables properly!') 116 | --------------------------------------------------------------------------------