diff --git a/serverPython/automation.py b/serverPython/automation.py index 527e7e7..675b350 100644 --- a/serverPython/automation.py +++ b/serverPython/automation.py @@ -1,12 +1,13 @@ +import logging import uiautomator2 as u2 import time import re class ShopeeBot: def __init__(self, device_ip: str): - print(f"Đang kết nối tới thiết bị {device_ip}...") + logging.info(f"Đang kết nối tới thiết bị {device_ip}...") self.d = u2.connect(device_ip) - print("Kết nối thành công!") + logging.info("Kết nối thành công!") def get_commission_rate(self, url: str) -> str: try: @@ -14,7 +15,7 @@ class ShopeeBot: self.d.screen_on() # 2. Khởi động lại Shopee từ đầu để tránh lỗi luồng cũ - print("Khởi động Shopee...") + logging.info("Khởi động Shopee...") self.d.app_start("com.shopee.vn", stop=True) time.sleep(6) # Chờ app khởi động hoàn toàn @@ -24,7 +25,7 @@ class ShopeeBot: time.sleep(1) # 3. Chuyển sang tab "Tôi" - print("Vào tab Tôi...") + logging.info("Vào tab Tôi...") if self.d(text="Tôi").exists: self.d(text="Tôi").click() else: @@ -32,7 +33,7 @@ class ShopeeBot: time.sleep(2) # 4. Tìm và ấn "Shopee Tiếp Thị Liên Kết" - print("Tìm mục Shopee Tiếp Thị Liên Kết...") + logging.info("Tìm mục Shopee Tiếp Thị Liên Kết...") # Cuộn xuống cho đến khi thấy chữ này self.d(scrollable=True).scroll.to(textContains="Tiếp Thị Liên Kết") @@ -44,7 +45,7 @@ class ShopeeBot: time.sleep(6) # Mục này có thể tải hơi lâu vì gọi API # 5. Ấn tab "Tài khoản" ở thanh menu dưới cùng của Affiliate - print("Vào tab Tài khoản...") + logging.info("Vào tab Tài khoản...") if self.d(text="Tài khoản").exists: self.d(text="Tài khoản").click() else: @@ -52,7 +53,7 @@ class ShopeeBot: time.sleep(2) # 5.1. Bấm vào "Chuyển đổi liên kết" trong danh sách - print("Bấm vào mục Chuyển đổi liên kết...") + logging.info("Bấm vào mục Chuyển đổi liên kết...") if self.d(text="Chuyển đổi liên kết").exists: self.d(text="Chuyển đổi liên kết").click() else: @@ -60,7 +61,7 @@ class ShopeeBot: time.sleep(2) # 6. Nhập link vào ô Text - print("Nhập link sản phẩm...") + logging.info("Nhập link sản phẩm...") # Nếu có nút "Xóa tất cả" link cũ thì xóa trước if self.d(text="Xóa tất cả").exists: self.d(text="Xóa tất cả").click() @@ -75,7 +76,7 @@ class ShopeeBot: time.sleep(1) # 7. Bấm nút Chuyển Đổi màu cam - print("Bấm nút Chuyển Đổi...") + logging.info("Bấm nút Chuyển Đổi...") if self.d(text="Chuyển Đổi").exists: self.d(text="Chuyển Đổi").click() else: @@ -84,7 +85,7 @@ class ShopeeBot: time.sleep(4) # Chờ Shopee convert link và hiện kết quả # 8. Lấy tỷ lệ hoa hồng, link mới và giá - print("Đang đọc kết quả trên màn hình...") + logging.info("Đang đọc kết quả trên màn hình...") import json import re diff --git a/serverPython/main.py b/serverPython/main.py index 7c99e27..f6354dd 100644 --- a/serverPython/main.py +++ b/serverPython/main.py @@ -4,6 +4,17 @@ from automation import ShopeeBot from typing import Optional import logging import os +import sys + +# Cấu hình logging: ghi log ra cả màn hình (console) và file (shopee_bot.log) +logging.basicConfig( + level=logging.INFO, + format='%(asctime)s - %(levelname)s - %(message)s', + handlers=[ + logging.FileHandler("shopee_bot.log", encoding="utf-8"), + logging.StreamHandler(sys.stdout) + ] +) app = FastAPI(title="n8n Shopee Bridge API") diff --git a/serverPython/update_logging.py b/serverPython/update_logging.py new file mode 100644 index 0000000..33072d5 --- /dev/null +++ b/serverPython/update_logging.py @@ -0,0 +1,12 @@ +import os + +file_path = r"c:\Users\NASPC\Documents\botAffilate\serverPython\automation.py" +with open(file_path, "r", encoding="utf-8") as f: + content = f.read() + +content = "import logging\n" + content.replace("print(", "logging.info(") + +with open(file_path, "w", encoding="utf-8") as f: + f.write(content) + +print("Đã thay thế print thành logging.info trong automation.py")