feat: implement FastAPI service with structured logging and automated script modification utility

This commit is contained in:
Victor Phan
2026-07-21 10:16:15 +07:00
parent adbfb43a48
commit 0c587685a5
3 changed files with 34 additions and 10 deletions
+11 -10
View File
@@ -1,12 +1,13 @@
import logging
import uiautomator2 as u2 import uiautomator2 as u2
import time import time
import re import re
class ShopeeBot: class ShopeeBot:
def __init__(self, device_ip: str): 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) 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: def get_commission_rate(self, url: str) -> str:
try: try:
@@ -14,7 +15,7 @@ class ShopeeBot:
self.d.screen_on() self.d.screen_on()
# 2. Khởi động lại Shopee từ đầu để tránh lỗi luồng cũ # 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) self.d.app_start("com.shopee.vn", stop=True)
time.sleep(6) # Chờ app khởi động hoàn toàn time.sleep(6) # Chờ app khởi động hoàn toàn
@@ -24,7 +25,7 @@ class ShopeeBot:
time.sleep(1) time.sleep(1)
# 3. Chuyển sang tab "Tôi" # 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: if self.d(text="Tôi").exists:
self.d(text="Tôi").click() self.d(text="Tôi").click()
else: else:
@@ -32,7 +33,7 @@ class ShopeeBot:
time.sleep(2) time.sleep(2)
# 4. Tìm và ấn "Shopee Tiếp Thị Liên Kết" # 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 # 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") 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 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 # 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: if self.d(text="Tài khoản").exists:
self.d(text="Tài khoản").click() self.d(text="Tài khoản").click()
else: else:
@@ -52,7 +53,7 @@ class ShopeeBot:
time.sleep(2) time.sleep(2)
# 5.1. Bấm vào "Chuyển đổi liên kết" trong danh sách # 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: if self.d(text="Chuyển đổi liên kết").exists:
self.d(text="Chuyển đổi liên kết").click() self.d(text="Chuyển đổi liên kết").click()
else: else:
@@ -60,7 +61,7 @@ class ShopeeBot:
time.sleep(2) time.sleep(2)
# 6. Nhập link vào ô Text # 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 # 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: if self.d(text="Xóa tất cả").exists:
self.d(text="Xóa tất cả").click() self.d(text="Xóa tất cả").click()
@@ -75,7 +76,7 @@ class ShopeeBot:
time.sleep(1) time.sleep(1)
# 7. Bấm nút Chuyển Đổi màu cam # 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: if self.d(text="Chuyển Đổi").exists:
self.d(text="Chuyển Đổi").click() self.d(text="Chuyển Đổi").click()
else: else:
@@ -84,7 +85,7 @@ class ShopeeBot:
time.sleep(4) # Chờ Shopee convert link và hiện kết quả 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á # 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 json
import re import re
+11
View File
@@ -4,6 +4,17 @@ from automation import ShopeeBot
from typing import Optional from typing import Optional
import logging import logging
import os 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") app = FastAPI(title="n8n Shopee Bridge API")
+12
View File
@@ -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")