import requests import tkinter as tk from tkinter import filedialog import re # Thay thế các giá trị sau bằng thông tin thật của bạn access_token = "FACE_BOOK API" group_id = "GROUP_ID # Nhập từ khóa từ người dùng keyword = input("Nhập từ khóa: ") # Loại bỏ ký tự đặc biệt và dấu cách trong từ khóa keyword = re.sub(r'[^a-zA-Z0-9 ]', '', keyword) keyword = keyword.lower() # Lấy danh sách bài viết trong nhóm group_feed_url = f"https://graph.facebook.com/{group_id}/feed" group_feed_params = { "access_token": access_token, "fields": "id,message", "limit": 5000, } response = requests.get(group_feed_url, params=group_feed_params, timeout=60) data = response.json() if "data" in data: # Lưu comment của những bài viết chứa từ khóa vào biến comments_text comments_text = "" for post in data["data"]: if "message" in post: cleaned_message = re.sub(r'[^a-zA-Z0-9 ]', '', post["message"].lower()) if keyword in cleaned_message: post_id = post["id"] comments_url = f"https://graph.facebook.com/{post_id}/comments" comments_params = {"access_token": access_token} comments_response = requests.get(comments_url, params=comments_params) comments_data = comments_response.json() comments_text += f"Post: {post['message']}\n" for comment in comments_data["data"]: comments_text += f"Comment: {comment['message']}\n" # Hiển thị hộp thoại để chọn nơi lưu file root = tk.Tk() root.withdraw() # Ẩn cửa sổ chính của tkinter file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")]) if file_path: with open(file_path, "w", encoding="utf-8") as file: file.write(comments_text) print(f"Đã lưu file tại: {file_path}") else: print("Lưu file đã bị hủy.") else: print("Không tìm thấy dữ liệu bài viết trong nhóm.")