feat: implement Facebook bot automation with UI interaction and image attachment capabilities
This commit is contained in:
@@ -31,7 +31,7 @@ class FacebookBot:
|
||||
time.sleep(2)
|
||||
return True
|
||||
|
||||
def post_comment(self, group_id: str, content: str, local_image_path: str = None) -> dict:
|
||||
def post_comment(self, group_id: str, content: str, local_image_path: str = None, post_index: int = 1) -> dict:
|
||||
try:
|
||||
self.d.screen_on()
|
||||
|
||||
@@ -55,10 +55,11 @@ class FacebookBot:
|
||||
return {"status": "error", "message": "Group không tồn tại hoặc không có quyền truy cập."}
|
||||
|
||||
# Quét nút Bình luận, nếu không thấy thì vuốt xuống (vì ảnh bìa group che mất)
|
||||
logging.info("Tìm nút Bình luận của bài viết đầu tiên...")
|
||||
logging.info(f"Tìm nút Bình luận của bài viết thứ {post_index}...")
|
||||
comment_btn = None
|
||||
current_post_count = 1
|
||||
|
||||
for _ in range(7): # Thử vuốt tối đa 7 lần
|
||||
for _ in range(30): # Tăng số lần vuốt tối đa lên để có thể cuộn qua nhiều bài
|
||||
# Sửa lỗi: Nút Thích có description là "bày tỏ cảm xúc về bình luận" nên Regex .*bình luận.* sẽ bắt nhầm nút Thích!
|
||||
# Do đó ta chỉ tìm đúng chữ Bình luận đứng đầu, hoặc chữ Comment.
|
||||
c_desc_vn = self.d(descriptionMatches="(?i)^bình luận$")
|
||||
@@ -66,12 +67,25 @@ class FacebookBot:
|
||||
c_desc_en = self.d(descriptionMatches="(?i)^comment$")
|
||||
c_text_en = self.d(textMatches="(?i)^comment$")
|
||||
|
||||
if c_desc_vn.exists: comment_btn = c_desc_vn; break
|
||||
if c_text_vn.exists: comment_btn = c_text_vn; break
|
||||
if c_desc_en.exists: comment_btn = c_desc_en; break
|
||||
if c_text_en.exists: comment_btn = c_text_en; break
|
||||
btn = None
|
||||
if c_desc_vn.exists: btn = c_desc_vn
|
||||
elif c_text_vn.exists: btn = c_text_vn
|
||||
elif c_desc_en.exists: btn = c_desc_en
|
||||
elif c_text_en.exists: btn = c_text_en
|
||||
|
||||
# Vuốt lên một chút
|
||||
if btn and btn.exists:
|
||||
if current_post_count == post_index:
|
||||
comment_btn = btn
|
||||
break
|
||||
else:
|
||||
logging.info(f"Đã thấy bài viết thứ {current_post_count}, đang bỏ qua để tìm bài {post_index}...")
|
||||
# Vuốt qua bài viết hiện tại bằng 1 cú vuốt dài (70% màn hình)
|
||||
self.d.swipe_ext("up", scale=0.7)
|
||||
time.sleep(2)
|
||||
current_post_count += 1
|
||||
continue
|
||||
|
||||
# Nếu không thấy nút nào, vuốt lên một chút để tìm tiếp
|
||||
self.d.swipe_ext("up", scale=0.5)
|
||||
time.sleep(1.5)
|
||||
self.dump_ui_for_debug("fb_step2_scrolling.xml")
|
||||
|
||||
Reference in New Issue
Block a user