forked from Jxck-S/plane-notify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta_toolkit.py
51 lines (49 loc) · 2.15 KB
/
meta_toolkit.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
import requests
import json
def post_fb(page_id, file_path, message, access_token):
"""Posts to Facebook with Image"""
import os
file_name = os.path.basename(file_path)
files= {'image':(file_name, open(file_path, 'rb'), "multipart/form-data")}
url = f"https://graph.facebook.com/{page_id}/photos?message={message}&access_token={access_token}"
resp = requests.post(url, files=files)
resp.raise_for_status()
print("Facebook Post Response: ", resp.json())
return resp.json()
def get_fb_post_image_link(post_id, access_token):
"""Returns Highest Resolution image link of a Facebook Post by FBID"""
url = f"https://graph.facebook.com/{post_id}?fields=images&access_token={access_token}"
resp = requests.get(url)
resp.raise_for_status()
image_url = resp.json()['images'][0]['source']
print("Highest Resoulution Image URL for FBID", post_id, "is", image_url)
return image_url
def post_to_instagram(ig_user_id, access_token, image_url, caption):
"""Posts to Instagram"""
post_url = f'https://graph.facebook.com/v13.0/{ig_user_id}/media'
payload = {
'caption': caption,
'access_token': access_token,
'image_url': image_url
}
resp = requests.post(post_url, data=payload)
resp.raise_for_status()
print("IG Media Response:", resp.json())
result = json.loads(resp.text)
if 'id' in result:
creation_id = result['id']
second_url = f'https://graph.facebook.com/v13.0/{ig_user_id}/media_publish'
second_payload = {
'creation_id': creation_id,
'access_token':access_token
}
resp = requests.post(second_url, data=second_payload)
resp.raise_for_status()
print('Posted to Instagram', caption, "IG response:", resp.json())
else:
print('Could not post to Instagram: ', resp.json())
def post_to_meta_both(fb_page_id, ig_user_id, file_path, message, access_token):
"""Posts to Facebook and Instagram"""
post_info = post_fb(fb_page_id, file_path, message, access_token)
fb_image_link = get_fb_post_image_link(post_info['id'], access_token)
post_to_instagram(ig_user_id, access_token, fb_image_link, message)