-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstagram-discord.py
126 lines (99 loc) · 4.08 KB
/
instagram-discord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
# Copyright (c) 2020 Fernando
# Url: https://github.com/fernandod1/
# License: MIT
# DESCRIPTION:
# This script executes 2 actions:
# 1.) Monitors for new image posted in a instagram account.
# 2.) If found new image, a bot posts new instagram image in a discord channel.
# 3.) Repeat after set interval.
# REQUIREMENTS:
# - Python v3
# - Python module re, json, requests
import json
import requests
import os
import time
# USAGE:
# Set Environment Variables:
# Set IG_USERNAME to username account you want to monitor. Example - ladygaga
# Set WEBHOOK_URL to Discord account webhook url. To know how, just Google: "how to create webhook discord".
# Set TIME_INTERVAL to the time in seconds in between each check for a new post. Example - 1.5, 600 (default=600)
# Help: https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-set-environment-variables-in-linux/
INSTAGRAM_USERNAME = os.environ.get("IG_USERNAME")
# ----------------------- 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 = {"embeds": []}
embed = {
"color": 15467852,
"title": "New pic of @" + INSTAGRAM_USERNAME + "",
"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",
}
return requests.get(
"https://www.instagram.com/" + INSTAGRAM_USERNAME + "/feed/?__a=1",
headers=headers,
)
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("WEBHOOK_URL"), get_instagram_html(INSTAGRAM_USERNAME)
)
except Exception as e:
print(e)
if __name__ == "__main__":
if os.environ.get("IG_USERNAME") is None or os.environ.get("WEBHOOK_URL") is None:
print("Please configure environment variables properly!")
else:
while True:
main()
time.sleep(
float(os.environ.get("TIME_INTERVAL") or 600)
) # 600 = 10 minutes