-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
78 lines (65 loc) · 2.33 KB
/
main.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
import os
import requests
from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()
FACEBOOK_HOSTNAME = os.environ.get("HOSTNAME", "graph.facebook.com")
FACEBOOK_API = "https://" + FACEBOOK_HOSTNAME + "/v14.0"
FACEBOOK_ACCESS_TOKEN = os.environ["FACEBOOK_ACCESS_TOKEN"]
FACEBOOK_POST_ID = os.environ["FACEBOOK_POST_ID"]
def get_post_data(post_id):
"""Get post data from Facebook API
Args:
post_id (str): Facebook post id. format: "pageid_postid"
Returns:
dict: Facebook get post data response
Examples:
>>> get_post_data("123456789_123456789")
{
"reactions":{
"data":[],
"summary":{
"total_count":2
}
},
"comments":{
"data":[],
"summary":{
"total_count":0
}
},
"shares":{
"count":1
},
"id":"103630545568311_169549302309768"
}
"""
response = requests.get(
FACEBOOK_API + f"/{post_id}",
params={
"fields": "reactions.summary(total_count),comments.filter(stream).summary(total_count),shares",
"access_token": FACEBOOK_ACCESS_TOKEN,
},
)
return response.json()
def update_post_data(post_id, message):
response = requests.post(
FACEBOOK_API + f"/{post_id}",
params={"message": message, "access_token": FACEBOOK_ACCESS_TOKEN},
)
return response.json()
@sched.scheduled_job("interval", minutes=int(os.environ.get("SCHEDULE_INTERVAL", 3)))
def main():
post_data = get_post_data(FACEBOOK_POST_ID)
print(post_data)
shares = post_data["shares"]["count"] if "shares" in post_data else 0
comments = post_data["comments"]["summary"]["total_count"]
reactions = post_data["reactions"]["summary"]["total_count"]
message = (
f"{reactions} REACTIONS, {comments} COMMENTS, {shares} SHARES\n\n"
"If you REACT, COMMENT, or SHARE"
" on this post, It will be updated automatically a few minutes later!"
"\n\nCheckout https://youtu.be/3HvzgDzrG0c on how this works!"
)
print(update_post_data(FACEBOOK_POST_ID, message))
# main()
sched.start()