forked from Rosadojonathan/facebook-comment-fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commentFetcher.py
116 lines (92 loc) · 4.13 KB
/
commentFetcher.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
import facebook
from facebookads.api import FacebookAdsApi
from facebookads.adobjects.adaccount import AdAccount
from facebookads.adobjects.campaign import Campaign
from facebookads.adobjects.adset import AdSet
from facebookads.adobjects.adcreative import AdCreative
from facebookads.adobjects.adcreativeobjectstoryspec import AdCreativeObjectStorySpec
from facebookads.adobjects.ad import Ad
from facebookads import adobjects
import requests
import json
import datetime
import shelve
from credentials import credentials
from config import pages_id, page_names, pages_actual_names, accounts_ids, slack_tokens, params, fields
from sentimentanalyzer import sentimentAnalyzer
from error_handling import exception_catcher
from send_message_to_slack import send_message_to_slack
slack_token = slack_tokens['#mk_moderation']
app_id = credentials['app_id']
app_secret = credentials['app_secret']
access_token = credentials['access_token']
client_token= credentials['client_token']
FacebookAdsApi.init(app_id=app_id, app_secret=app_secret, access_token=access_token,api_version="v3.0")
account = AdAccount(accounts_ids['live_booker'])
graph = facebook.GraphAPI(access_token=access_token, version = 3.0)
ad_ids_list = []
def ad_ids_fetcher(account,params,fields):
"""
fetches ad ids from active campaigns for the selected ad account
"""
account = AdAccount(account)
try:
adset_insights = account.get_insights(fields=fields, params=params)
except Exception:
exception_catcher('/facebook_ads_comments_analyzer/')
ad_ids_list = [insight['ad_id'] for insight in adset_insights]
return ad_ids_list
def page_id_post_id_fetcher(ad_id):
"""
pass ad_id to fetch page_id_post_id which is needed to fetch the comments from a Facebook post or ad
"""
try:
s = requests.get('https://graph.facebook.com/v3.1/'+ ad_id + '?fields=creative{effective_object_story_id}&access_token=' + access_token)
except Exception:
exception_catcher('/facebook_ads_comments_analyzer/')
s = s.json()
print(s)
print(s['creative']['effective_object_story_id'])
return s['creative']['effective_object_story_id']
def comment_fetcher(page_id_post_id):
"""
pass page-id_post-id to fetch comments
"""
try:
comments = graph.get_connections(id=page_id_post_id,connection_name='comments')
except Exception:
exception_catcher('/facebook_ads_comments_analyzer/')
return comments
def ad_comments_sentiment_analyzer(account):
"""
main function, gets ad comments and sends them through sentiment analysis to parse them and check if they're
negative comments if they haven't been parsed already
"""
already_parsed = shelve.open('already_parsed')
ad_ids = ad_ids_fetcher(account=account,params=params,fields=fields)
number_of_comments = 0
for ad in ad_ids:
page_id_post_id = page_id_post_id_fetcher(ad)
comments = comment_fetcher(page_id_post_id)
for comment in comments['data']:
number_of_comments += 1
if comment['id'] not in already_parsed.keys():
already_parsed[comment['id']] = True
message = comment['message']
try:
snt = sentimentAnalyzer(message)
if snt['compound'] < 0:
print(message)
print(snt)
post_id = page_id_post_id.split('_')[1]
comment_id = comment['id'].split('_')[1]
link_to_comment = 'https://www.facebook.com/' + page_names[account] + '/posts/' + post_id + '?comment_id=' + comment_id
page_name = pages_actual_names[account]
send_message_to_slack(slack_token, page_name, message,link_to_comment)
except Exception:
exception_catcher('/facebook_ads_comments_analyzer/')
print('Number of comments analyzed: ' + str(number_of_comments))
already_parsed.close()
accounts_to_iterate = ['live_booker','next_concert']
for account_iterated in accounts_to_iterate:
ad_comments_sentiment_analyzer(accounts_ids[account_iterated])