fernandod1 / Instagram-to-discord

Monitor instagram user account and automatically post new images to discord channel via a webhook. Working 2022!
https://www.fernando.info
Other
127 stars 63 forks source link

Please configure environment variables correctly #24

Closed Swastik2442 closed 9 months ago

Swastik2442 commented 3 years ago

I am getting the error "Please configure environment variables correctly" even after I am configured them correctly. Please help.

Swastik2442 commented 3 years ago

`#!/usr/bin/python

INSTAGRAM_USERNAME = os.environ.get('ig_loki2442')

----------------------- Do not modify under this line -----------------------

def get_user_fullname(html): return html.json()["graphql"]["user"]["full_name"]

def get_total_photos(html): return int(html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["count"])

def get_last_publication_url(html): return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["shortcode"]

def get_last_photo_url(html): return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["display_url"]

def get_last_thumb_url(html): return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["thumbnail_src"]

def get_description_photo(html): return html.json()["graphql"]["user"]["edge_owner_to_timeline_media"]["edges"][0]["node"]["edge_media_to_caption"]["edges"][0]["node"]["text"]

def webhook(webhook_url, html):

for all params, see https://discordapp.com/developers/docs/resources/webhook#execute-webhook

# for all params, see https://discordapp.com/developers/docs/resources/channel#embed-object
data = {}
data["embeds"] = []
embed = {}
embed["color"] = 15467852
embed["title"] = "New pic of @"+INSTAGRAM_USERNAME+""
embed["url"] = "https://www.instagram.com/p/" + \
    get_last_publication_url(html)+"/"
embed["description"] = get_description_photo(html)
# embed["image"] = {"url":get_last_thumb_url(html)} # unmark to post bigger image
embed["thumbnail"] = {"url": get_last_thumb_url(html)}
data["embeds"].append(embed)
result = requests.post(webhook_url, data=json.dumps(
    data), headers={"Content-Type": "application/json"})
try:
    result.raise_for_status()
except requests.exceptions.HTTPError as err:
    print(err)
else:
    print("Image successfully posted in Discord, code {}.".format(
        result.status_code))

def get_instagram_html(INSTAGRAM_USERNAME): headers = { "Host": "www.instagram.com", "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11" } html = requests.get("https://www.instagram.com/" + INSTAGRAM_USERNAME + "/feed/?__a=1", headers=headers) return html

def main(): try: html = get_instagram_html(INSTAGRAM_USERNAME) if(os.environ.get("LAST_IMAGE_ID") == get_last_publication_url(html)): print("Not new image to post in discord.") else: os.environ["LAST_IMAGE_ID"] = get_last_publication_url(html) print("New image to post in discord.") webhook(os.environ.get("https://discord.com/api/webhooks/840585579439718410/Xcmt2qD7lu0FtsE5Sa8uOrMasZI-9T99Y85CObkMPEq3sL_qHOPaIiViEbu42hOZhuJ5"), get_instagram_html(INSTAGRAM_USERNAME)) except Exception as e: print(e)

if name == "main": if os.environ.get('ig_loki2442') != None and os.environ.get('https://discord.com/api/webhooks/840585579439718410/Xcmt2qD7lu0FtsE5Sa8uOrMasZI-9T99Y85CObkMPEq3sL_qHOPaIiViEbu42hOZhuJ5') != None: while True: main() time.sleep(float(os.environ.get('1.5') or 600)) # 600 = 10 minutes else: print('Please configure environment variables properly!') `