-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram.py
More file actions
70 lines (57 loc) · 2.03 KB
/
instagram.py
File metadata and controls
70 lines (57 loc) · 2.03 KB
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
import requests
class Instagram:
"""Link to docs: https://developers.facebook.com/docs/instagram-platform/instagram-graph-api/reference/ig-user/media"""
base_url = "https://graph.instagram.com/v23.0"
def __init__(self, ig_id, token):
self.ig_id = ig_id
self.token = token
self.headers = {"Authorization": f"Bearer {self.token}"}
def create_container(
self, url: str, is_carousel=False, caption=None, user_tag=None, alt_text=None
) -> dict:
data = {"image_url": url, "is_carousel_item": is_carousel}
if caption:
data["caption"] = caption
if alt_text:
data["alt_text"] = alt_text
if user_tag:
data["user_tags"] = [
{
"username": user_tag,
"x": 0.3,
"y": 0.8,
}
]
res = requests.post(
f"{self.base_url}/{self.ig_id}/media", json=data, headers=self.headers
).json()
if "error" in res and "user_tags" in data:
print(res)
del data["user_tags"]
res = requests.post(
f"{self.base_url}/{self.ig_id}/media", json=data, headers=self.headers
).json()
return res
def create_carousel(
self, children: list, caption: str, user_tag: str = None
) -> dict:
data = {
"media_type": "CAROUSEL",
"children": children,
"caption": caption,
"collaborators": ["pinkbuttocks"],
}
if user_tag:
data["collaborators"].append(user_tag)
res = requests.post(
f"{self.base_url}/{self.ig_id}/media", json=data, headers=self.headers
).json()
return res
def post_media(self, creation_id: int) -> dict:
data = {"creation_id": creation_id}
res = requests.post(
f"{self.base_url}/{self.ig_id}/media_publish",
data=data,
headers=self.headers,
).json()
return res