Spaces:
Paused
Paused
Update func_facebook.py
Browse files- func_facebook.py +57 -47
func_facebook.py
CHANGED
@@ -15,15 +15,23 @@ def hide_negative_comments(token):
|
|
15 |
posts_data = graph.get_object(id='me/posts', fields='id,message,created_time')
|
16 |
return posts_data.get('data', [])
|
17 |
|
18 |
-
|
19 |
-
|
|
|
20 |
graph = init_facebook_client(token)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def filter_comments(comments, sentiments):
|
29 |
print("Фильтруем негативные комментарии.")
|
@@ -48,40 +56,35 @@ def hide_negative_comments(token):
|
|
48 |
print("Нет постов для обработки.")
|
49 |
return []
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
})
|
67 |
|
68 |
-
return
|
69 |
-
|
70 |
-
|
71 |
-
def get_facebook_comments(post_id, ACCESS_TOKEN):
|
72 |
-
print(f"Получаем комментарии для поста: {post_id}")
|
73 |
-
url = f"{GRAPH_API_URL}/{post_id}/comments"
|
74 |
-
params = {
|
75 |
-
'access_token': ACCESS_TOKEN,
|
76 |
-
'filter': 'stream'
|
77 |
-
}
|
78 |
-
response = requests.get(url, params=params)
|
79 |
-
if response.status_code == 200:
|
80 |
-
print("Комментарии успешно получены.")
|
81 |
-
return response.json().get('data', [])
|
82 |
-
else:
|
83 |
-
print(f"Ошибка при получении комментариев: {response.text}")
|
84 |
-
return []
|
85 |
|
86 |
|
87 |
def get_page_id(page_access_token):
|
@@ -102,8 +105,7 @@ def get_posts(page_id, page_access_token):
|
|
102 |
url = f"{GRAPH_API_URL}/{page_id}/posts"
|
103 |
params = {
|
104 |
"access_token": page_access_token,
|
105 |
-
"fields": "id"
|
106 |
-
# Максимальное количество постов за запрос
|
107 |
}
|
108 |
posts = []
|
109 |
while True:
|
@@ -126,7 +128,6 @@ def get_comments(post_id, page_access_token):
|
|
126 |
params = {
|
127 |
"access_token": page_access_token,
|
128 |
"fields": "id,from,message,is_hidden",
|
129 |
-
|
130 |
}
|
131 |
comments = []
|
132 |
while True:
|
@@ -138,7 +139,7 @@ def get_comments(post_id, page_access_token):
|
|
138 |
comments.extend(data.get("data", []))
|
139 |
if 'paging' in data and 'next' in data['paging']:
|
140 |
url = data['paging']['next']
|
141 |
-
params = {}
|
142 |
else:
|
143 |
break
|
144 |
return comments
|
@@ -149,7 +150,6 @@ def has_page_replied(comment_id, page_id, page_access_token):
|
|
149 |
params = {
|
150 |
"access_token": page_access_token,
|
151 |
"fields": "from{id}",
|
152 |
-
|
153 |
}
|
154 |
while True:
|
155 |
response = requests.get(url, params=params)
|
@@ -175,13 +175,16 @@ def get_unanswered_comments(page_access_token):
|
|
175 |
|
176 |
print(f"ID Страницы: {page_id}")
|
177 |
posts = get_posts(page_id, page_access_token)
|
178 |
-
|
179 |
|
180 |
for post in posts:
|
181 |
post_id = post['id']
|
|
|
182 |
print(f"Обработка поста: {post_id}")
|
183 |
comments = get_comments(post_id, page_access_token)
|
184 |
|
|
|
|
|
185 |
for comment in comments:
|
186 |
if comment.get('is_hidden', False):
|
187 |
continue
|
@@ -190,7 +193,14 @@ def get_unanswered_comments(page_access_token):
|
|
190 |
if not has_page_replied(comment_id, page_id, page_access_token):
|
191 |
unanswered_comments.append(comment)
|
192 |
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
|
195 |
|
196 |
def reply_comment(comment_id, message, token):
|
|
|
15 |
posts_data = graph.get_object(id='me/posts', fields='id,message,created_time')
|
16 |
return posts_data.get('data', [])
|
17 |
|
18 |
+
|
19 |
+
|
20 |
+
def get_comments_for_post(post_id):
|
21 |
graph = init_facebook_client(token)
|
22 |
+
comments = []
|
23 |
+
url = f'{post_id}/comments'
|
24 |
+
params = {'fields': 'id,message,is_hidden'}
|
25 |
+
while True:
|
26 |
+
comments_data = graph.get_object(id=url, **params)
|
27 |
+
visible_comments = [comment for comment in comments_data.get('data', []) if not comment.get('is_hidden', False)]
|
28 |
+
comments.extend(visible_comments)
|
29 |
+
if 'paging' in comments_data and 'next' in comments_data['paging']:
|
30 |
+
url = comments_data['paging']['next']
|
31 |
+
params = {} # The 'next' URL already includes access token and fields
|
32 |
+
else:
|
33 |
+
break
|
34 |
+
return comments
|
35 |
|
36 |
def filter_comments(comments, sentiments):
|
37 |
print("Фильтруем негативные комментарии.")
|
|
|
56 |
print("Нет постов для обработки.")
|
57 |
return []
|
58 |
|
59 |
+
hidden_comments_per_post = []
|
60 |
+
for post in posts:
|
61 |
+
post_id = post['id']
|
62 |
+
post_message = post.get('message', '')
|
63 |
+
comments = get_comments_for_post(post_id)
|
64 |
+
if not comments:
|
65 |
+
print(f"Нет комментариев для поста {post_id}.")
|
66 |
+
continue
|
67 |
+
|
68 |
+
comments_text = [comment['message'] for comment in comments]
|
69 |
+
sentiments = analyze_sentiment(comments_text)
|
70 |
+
negative_comments = filter_comments(comments, sentiments)
|
71 |
+
|
72 |
+
hidden_comments = []
|
73 |
+
for comment in negative_comments:
|
74 |
+
if hide_comment(comment['id']):
|
75 |
+
hidden_comments.append({
|
76 |
+
'id': comment['id'],
|
77 |
+
'message': comment['message']
|
78 |
+
})
|
79 |
+
|
80 |
+
if hidden_comments:
|
81 |
+
hidden_comments_per_post.append({
|
82 |
+
'post_id': post_id,
|
83 |
+
'post_message': post_message,
|
84 |
+
'hidden_comments': hidden_comments
|
85 |
})
|
86 |
|
87 |
+
return hidden_comments_per_post
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
|
90 |
def get_page_id(page_access_token):
|
|
|
105 |
url = f"{GRAPH_API_URL}/{page_id}/posts"
|
106 |
params = {
|
107 |
"access_token": page_access_token,
|
108 |
+
"fields": "id,message"
|
|
|
109 |
}
|
110 |
posts = []
|
111 |
while True:
|
|
|
128 |
params = {
|
129 |
"access_token": page_access_token,
|
130 |
"fields": "id,from,message,is_hidden",
|
|
|
131 |
}
|
132 |
comments = []
|
133 |
while True:
|
|
|
139 |
comments.extend(data.get("data", []))
|
140 |
if 'paging' in data and 'next' in data['paging']:
|
141 |
url = data['paging']['next']
|
142 |
+
params = {}
|
143 |
else:
|
144 |
break
|
145 |
return comments
|
|
|
150 |
params = {
|
151 |
"access_token": page_access_token,
|
152 |
"fields": "from{id}",
|
|
|
153 |
}
|
154 |
while True:
|
155 |
response = requests.get(url, params=params)
|
|
|
175 |
|
176 |
print(f"ID Страницы: {page_id}")
|
177 |
posts = get_posts(page_id, page_access_token)
|
178 |
+
posts_with_unanswered_comments = []
|
179 |
|
180 |
for post in posts:
|
181 |
post_id = post['id']
|
182 |
+
post_message = post.get('message', '')
|
183 |
print(f"Обработка поста: {post_id}")
|
184 |
comments = get_comments(post_id, page_access_token)
|
185 |
|
186 |
+
unanswered_comments = []
|
187 |
+
|
188 |
for comment in comments:
|
189 |
if comment.get('is_hidden', False):
|
190 |
continue
|
|
|
193 |
if not has_page_replied(comment_id, page_id, page_access_token):
|
194 |
unanswered_comments.append(comment)
|
195 |
|
196 |
+
if unanswered_comments:
|
197 |
+
posts_with_unanswered_comments.append({
|
198 |
+
'post_id': post_id,
|
199 |
+
'post_message': post_message,
|
200 |
+
'unanswered_comments': unanswered_comments
|
201 |
+
})
|
202 |
+
|
203 |
+
return posts_with_unanswered_comments
|
204 |
|
205 |
|
206 |
def reply_comment(comment_id, message, token):
|