Uniaff commited on
Commit
8a1bc73
·
verified ·
1 Parent(s): f739e90

Update func_facebook.py

Browse files
Files changed (1) hide show
  1. func_facebook.py +10 -11
func_facebook.py CHANGED
@@ -4,6 +4,7 @@ from func_ai import analyze_sentiment
4
 
5
  GRAPH_API_URL = 'https://graph.facebook.com/v20.0'
6
 
 
7
  def hide_negative_comments(token):
8
  def init_facebook_client(token):
9
  print("Инициализация клиента Facebook.")
@@ -16,7 +17,7 @@ def hide_negative_comments(token):
16
  "limit": 30
17
  }
18
  graph = init_facebook_client(token)
19
-
20
  user_posts_data = graph.get_object(id='me/posts', fields='id,message,created_time')
21
  user_posts = user_posts_data.get('data', [])
22
  print(f"Найдено {len(user_posts)} пользовательских постов.")
@@ -28,12 +29,12 @@ def hide_negative_comments(token):
28
  except facebook.GraphAPIError as e:
29
  print(f"Ошибка при получении рекламных постов: {e}")
30
  ads_posts = []
31
-
32
  all_posts = user_posts + ads_posts
33
  unique_posts_dict = {post['id']: post for post in all_posts}
34
  unique_posts = list(unique_posts_dict.values())
35
  print(f"Всего уникальных постов: {len(unique_posts)}.")
36
-
37
  return unique_posts
38
 
39
  def get_comments_for_post(post_id):
@@ -129,7 +130,6 @@ def hide_negative_comments(token):
129
  return hidden_comments_per_post
130
 
131
 
132
-
133
  def get_page_id(page_access_token):
134
  print("Получение ID страницы.")
135
  url = f"{GRAPH_API_URL}/me"
@@ -154,9 +154,9 @@ def get_posts(page_id, page_access_token):
154
  "fields": "id,message",
155
  "limit": 30
156
  }
157
-
158
  posts = []
159
-
160
  while True:
161
  response = requests.get(url, params=params)
162
  data = response.json()
@@ -164,7 +164,7 @@ def get_posts(page_id, page_access_token):
164
  if 'error' in data:
165
  print(f"Ошибка при получении постов: {data['error']}")
166
  break
167
-
168
  posts.extend(data.get("data", []))
169
  print(f"Получено {len(data.get('data', []))} постов.")
170
 
@@ -174,10 +174,10 @@ def get_posts(page_id, page_access_token):
174
  if 'error' in data_ads:
175
  print(f"Ошибка при получении рекламных постов: {data_ads['error']}")
176
  break
177
-
178
  posts.extend(data_ads.get("data", []))
179
  print(f"Получено {len(data_ads.get('data', []))} рекламных постов.")
180
-
181
  if 'paging' in data and 'next' in data['paging']:
182
  url = data['paging']['next']
183
  params = {}
@@ -248,7 +248,6 @@ def has_page_replied(comment_id, page_id, page_access_token):
248
  return False
249
 
250
 
251
-
252
  def get_unanswered_comments(page_access_token):
253
  page_id = get_page_id(page_access_token)
254
  if not page_id:
@@ -297,4 +296,4 @@ def reply_comment(comment_id, message, token):
297
  return True
298
  else:
299
  print(f"Ошибка при отправке ответа: {response.text}")
300
- return False
 
4
 
5
  GRAPH_API_URL = 'https://graph.facebook.com/v20.0'
6
 
7
+
8
  def hide_negative_comments(token):
9
  def init_facebook_client(token):
10
  print("Инициализация клиента Facebook.")
 
17
  "limit": 30
18
  }
19
  graph = init_facebook_client(token)
20
+
21
  user_posts_data = graph.get_object(id='me/posts', fields='id,message,created_time')
22
  user_posts = user_posts_data.get('data', [])
23
  print(f"Найдено {len(user_posts)} пользовательских постов.")
 
29
  except facebook.GraphAPIError as e:
30
  print(f"Ошибка при получении рекламных постов: {e}")
31
  ads_posts = []
32
+
33
  all_posts = user_posts + ads_posts
34
  unique_posts_dict = {post['id']: post for post in all_posts}
35
  unique_posts = list(unique_posts_dict.values())
36
  print(f"Всего уникальных постов: {len(unique_posts)}.")
37
+
38
  return unique_posts
39
 
40
  def get_comments_for_post(post_id):
 
130
  return hidden_comments_per_post
131
 
132
 
 
133
  def get_page_id(page_access_token):
134
  print("Получение ID страницы.")
135
  url = f"{GRAPH_API_URL}/me"
 
154
  "fields": "id,message",
155
  "limit": 30
156
  }
157
+
158
  posts = []
159
+
160
  while True:
161
  response = requests.get(url, params=params)
162
  data = response.json()
 
164
  if 'error' in data:
165
  print(f"Ошибка при получении постов: {data['error']}")
166
  break
167
+
168
  posts.extend(data.get("data", []))
169
  print(f"Получено {len(data.get('data', []))} постов.")
170
 
 
174
  if 'error' in data_ads:
175
  print(f"Ошибка при получении рекламных постов: {data_ads['error']}")
176
  break
177
+
178
  posts.extend(data_ads.get("data", []))
179
  print(f"Получено {len(data_ads.get('data', []))} рекламных постов.")
180
+
181
  if 'paging' in data and 'next' in data['paging']:
182
  url = data['paging']['next']
183
  params = {}
 
248
  return False
249
 
250
 
 
251
  def get_unanswered_comments(page_access_token):
252
  page_id = get_page_id(page_access_token)
253
  if not page_id:
 
296
  return True
297
  else:
298
  print(f"Ошибка при отправке ответа: {response.text}")
299
+ return False