chenjunhao 1 month ago
parent
commit
30816231d0
4 changed files with 65 additions and 52 deletions
  1. 2 3
      mt_V2/config.py
  2. 51 44
      mt_V2/db.py
  3. 12 4
      mt_V2/main.py
  4. 0 1
      mt_V2/yzm.py

+ 2 - 3
mt_V2/config.py

@@ -19,15 +19,14 @@ class Config:
 
     DB_AUTO_DRUG_TABLE = 'mt_auto_drug_test'
 
-
     LOCAL_DB_HOST = os.getenv('LOCAL_DB_HOST', 'localhost')
     LOCAL_DB_PORT = int(os.getenv('LOCAL_DB_PORT', 3306))
     LOCAL_DB_USER = os.getenv('LOCAL_DB_USERNAME', 'root')
     LOCAL_DB_PASSWORD = os.getenv('LOCAL_DB_PASSWORD', 'dfwy2025')
     LOCAL_DB_NAME = os.getenv('LOCAL_DB_DATABASE', 'drug_data')
-    
+
     # 应用配置
-    PACKAGE_NAME = 'com.sankuai.meituan'    
+    PACKAGE_NAME = 'com.sankuai.meituan'
     # 连接池配置
     DB_POOL_SIZE = int(os.getenv('DB_POOL_SIZE', 5))
     DB_POOL_TIMEOUT = int(os.getenv('DB_POOL_TIMEOUT', 30))

+ 51 - 44
mt_V2/db.py

@@ -4,26 +4,23 @@ import threading
 import time
 import atexit
 from collections import deque
-
-
-# simple direct connector (used by some scripts)
 from pymysql.cursors import DictCursor
 
-def get_connection():
-    return pymysql.connect(
-        host="120.24.26.108",
-        port=3307,
-        user="root",
-        password="zhijiayun123456",
-        database="drug_retrieve",
-        charset="utf8mb4",
-        autocommit=False,
-        cursorclass=DictCursor,
-    )
-
-
-# connection pool used by a_mt.py
+# ============================================================
+# 数据库配置
+# ============================================================
+DB_HOST = "120.24.26.108"
+DB_PORT = 3307
+DB_USER = "collect_user"
+DB_PASSWORD = "collect123456"
+DB_NAME = "drug_retrieve"      # 正式库;测试库改 drug_retrieve_test
+DB_CHARSET = "utf8mb4"
+
+# ============================================================
+# 连接池
+# ============================================================
 MYSQL_POOL = None
+POOL_LOCK = threading.Lock()
 
 
 class PooledConnection:
@@ -137,39 +134,49 @@ class MySQLConnectionPool:
             self._total = 0
 
 
+def _pool_kwargs():
+    """连接池参数"""
+    return {
+        "host": DB_HOST,
+        "port": DB_PORT,
+        "user": DB_USER,
+        "password": DB_PASSWORD,
+        "database": DB_NAME,
+        "charset": DB_CHARSET,
+        "cursorclass": pymysql.cursors.Cursor,
+    }
+
+
 def get_mysql():
+    """获取数据库连接(优先连接池,失败则直连)"""
     global MYSQL_POOL
-    if 'MYSQL_POOL' not in globals() or MYSQL_POOL is None:
-        MYSQL_POOL = None
-        try:
-            MYSQL_POOL = MySQLConnectionPool(
-                minconn=1,
-                maxconn=20,
-                host="120.24.26.108",
-                port=3307,
-                user="root",
-                password="zhijiayun123456",
-                database="drug_retrieve",
-                charset="utf8mb4",
-                cursorclass=pymysql.cursors.Cursor,
-            )
-        except Exception:
-            MYSQL_POOL = None
+    with POOL_LOCK:
+        if MYSQL_POOL is None:
+            try:
+                MYSQL_POOL = MySQLConnectionPool(minconn=1, maxconn=20, **_pool_kwargs())
+            except Exception:
+                MYSQL_POOL = None
     if MYSQL_POOL is None:
-        return pymysql.connect(
-            host="120.24.26.108",
-            port=3307,
-            user="root",
-            password="zhijiayun123456",
-            database="drug_retrieve",
-            charset="utf8mb4",
-            cursorclass=pymysql.cursors.Cursor,
-        )
+        return pymysql.connect(**_pool_kwargs())
     return MYSQL_POOL.getconn()
 
 
+def get_connection():
+    """直连数据库(兼容旧脚本,DictCursor 模式)"""
+    return pymysql.connect(
+        host=DB_HOST,
+        port=DB_PORT,
+        user=DB_USER,
+        password=DB_PASSWORD,
+        database=DB_NAME,
+        charset=DB_CHARSET,
+        autocommit=False,
+        cursorclass=DictCursor,
+    )
+
+
 # 在程序退出时确保池中所有物理连接被关闭
 try:
-    atexit.register(lambda: MYSQL_POOL.closeall() if 'MYSQL_POOL' in globals() and MYSQL_POOL is not None else None)
+    atexit.register(lambda: MYSQL_POOL.closeall() if MYSQL_POOL is not None else None)
 except Exception:
-    pass
+    pass

+ 12 - 4
mt_V2/main.py

@@ -8,6 +8,7 @@ import subprocess
 import re
 import random
 import datetime
+import sys
 import json
 import unicodedata
 from aip import AipOcr
@@ -772,7 +773,7 @@ class SpiderMonitor(threading.Thread):
 
 
 class MTScreenshot:
-    def __init__(self, d, oss_config, search_key, title_key, scroll_times=4, compress_quality=7, resize_ratio=0.8,device_id=None,
+    def __init__(self, d, oss_config, search_key, title_key, scroll_times=1, compress_quality=7, resize_ratio=0.8,device_id=None,
                  monitor=None):
         self.device_id = device_id
         # 接收外部已连接好的u2设备实例
@@ -3433,7 +3434,8 @@ class MT:
         print(f"列表当前商品店铺名称:{shop_name}")
 
         if price == '' or shop_name == '':
-            print("列表当前商品价格或店铺名称不存在")
+            print("列表当前商品价格或店铺名称不存在","价格:",price,"店铺名:",shop_name)
+
             return "continue"
 
         scrape_date = self.get_current_date()
@@ -3688,8 +3690,8 @@ def fetch_task_from_scheduler(scheduler, device_id):
     task = scheduler.get_task()
     if not task:
         return None
-    # start_offset = task.get("current_page", 0)  # 移动端起始偏移量,0=从头开始
-    start_offset = 0  # 移动端起始偏移量,0=从头开始
+    start_offset = task.get("current_page", 0)  # 移动端起始偏移量,0=从头开始
+    # start_offset = 0  # 移动端起始偏移量,0=从头开始
     start_page = start_offset if start_offset > 0 else 1
     end_page = task.get("end_page", 0)
     # 转成 page_range 格式,MT.open_product_list_page → move_to_page_range_start 会跳页
@@ -3895,6 +3897,12 @@ def main():
         format='%(asctime)s [%(threadName)s] %(levelname)s: %(message)s'
     )
 
+    # 终端传入设备 ID:python main.py T4VK4LM7AAUOV8AY
+    global DEVICE_ID
+    if len(sys.argv) > 1 and sys.argv[1].strip():
+        DEVICE_ID = sys.argv[1].strip()
+        logging.info(f"使用终端传入设备: {DEVICE_ID}")
+
     # 自动模式:全局只创建一个调度器 + 一个心跳线程,所有任务复用
     scheduler = None
     if not MANUAL_MODE:

+ 0 - 1
mt_V2/yzm.py

@@ -1,5 +1,4 @@
 
-
 import base64
 import io
 import math