|
@@ -17,6 +17,8 @@ class DatabaseOperations:
|
|
|
def __init__(self, mode, platform):
|
|
|
self.mysql = MysqlHelper(mode=mode, platform=platform)
|
|
|
self.LocalLog = Local.logger(platform, mode)
|
|
|
+ self.mode = mode
|
|
|
+ self.platform = platform
|
|
|
|
|
|
def check_user_id(self, uid):
|
|
|
"""
|
|
@@ -26,7 +28,7 @@ class DatabaseOperations:
|
|
|
:return:如果用户ID存在于表中返回True,否则返回False
|
|
|
"""
|
|
|
try:
|
|
|
- query_sql = f""" SELECT uid FROM zqkd_uid WHERE uid = "{uid}"; """
|
|
|
+ query_sql = f""" SELECT uid FROM zqkd_user WHERE uid = "{uid}"; """
|
|
|
result = self.mysql.select(sql=query_sql)
|
|
|
return bool(result)
|
|
|
except Exception as e:
|
|
@@ -44,7 +46,7 @@ class DatabaseOperations:
|
|
|
:return:如果更新操作成功,返回更新操作的结果(通常是影响的行数),失败则返回None或抛出异常
|
|
|
"""
|
|
|
try:
|
|
|
- update_sql = f""" UPDATE zqkd_uid SET avatar_url = "{avatar_url}", user_name = "{user_name}" WHERE uid = "{uid}"; """
|
|
|
+ update_sql = f""" UPDATE zqkd_user SET avatar_url = "{avatar_url}", user_name = "{user_name}" WHERE uid = "{uid}"; """
|
|
|
return self.mysql.update(sql=update_sql)
|
|
|
except Exception as e:
|
|
|
tb = traceback.format_exc()
|
|
@@ -53,32 +55,55 @@ class DatabaseOperations:
|
|
|
|
|
|
def insert_user(self, uid, user_name, avatar_url):
|
|
|
"""
|
|
|
- 向数据库的zqkd_uid表中插入新的用户信息,包含用户ID、用户名、头像URL和当前时间。
|
|
|
+ 向数据库的zqkd_user表中插入或更新用户信息
|
|
|
|
|
|
- :param uid:新用户的ID
|
|
|
- :param user_name:新用户的用户名
|
|
|
- :param avatar_url:新用户的头像URL
|
|
|
- :return:如果插入操作成功,返回插入操作的结果(通常是影响的行数),失败则返回None或抛出异常
|
|
|
+ :param uid: 用户ID(数值类型)
|
|
|
+ :param user_name: 用户名
|
|
|
+ :param avatar_url: 头像URL
|
|
|
+ :return: 成功返回影响的行数,失败返回None
|
|
|
"""
|
|
|
try:
|
|
|
- current_time = datetime.now()
|
|
|
- formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
- insert_sql = f""" INSERT INTO zqkd_uid (uid, avatar_url, user_name, data_time) VALUES ('{uid}', '{avatar_url}', '{user_name}', '{formatted_time}'); """
|
|
|
+ # 直接拼接SQL(不推荐,有SQL注入风险)
|
|
|
+ insert_sql = f"""
|
|
|
+ INSERT INTO zqkd_user (uid, avatar_url, user_name)
|
|
|
+ VALUES ({uid}, '{avatar_url.replace("'", "''")}', '{user_name.replace("'", "''")}')
|
|
|
+ ON DUPLICATE KEY UPDATE
|
|
|
+ user_name = '{user_name.replace("'", "''")}',
|
|
|
+ avatar_url = '{avatar_url.replace("'", "''")}'
|
|
|
+ """
|
|
|
return self.mysql.update(sql=insert_sql)
|
|
|
except Exception as e:
|
|
|
tb = traceback.format_exc()
|
|
|
self.LocalLog.error(f"插入用户信息失败: {e}\n{tb}")
|
|
|
return None
|
|
|
-
|
|
|
+ def get_today_videos(self):
|
|
|
+ try:
|
|
|
+ # 手动转义单引号(仅缓解部分风险)
|
|
|
+
|
|
|
+ sql = """
|
|
|
+ SELECT count(*) as cnt
|
|
|
+ FROM crawler_video
|
|
|
+ WHERE create_time >= CURDATE()
|
|
|
+ AND create_time < CURDATE() + INTERVAL 1 DAY
|
|
|
+ AND platform = %s
|
|
|
+ AND strategy = %s
|
|
|
+ """
|
|
|
+ result = self.mysql.select_params(sql, (self.platform,self.mode))
|
|
|
+ if result and len(result) > 0:
|
|
|
+ return result[0][0] # 返回第一行第一列的计数值
|
|
|
+ return 0 # 无结果时返回0
|
|
|
+ except Exception as e:
|
|
|
+ self.LocalLog.error(f"查询失败: {e}")
|
|
|
+ return 0
|
|
|
def select_user(self, last_scanned_id=0):
|
|
|
"""
|
|
|
- 根据last_scanned_id分页查询用户数据
|
|
|
+ 根据last_scanned_id查询用户数据
|
|
|
:param last_scanned_id: 上次扫描的ID,0表示从头开始
|
|
|
:return: 查询结果列表
|
|
|
"""
|
|
|
try:
|
|
|
# 构建查询(根据last_scanned_id过滤)
|
|
|
- query = "SELECT id, uid FROM zqkd_uid"
|
|
|
+ query = "SELECT id, uid FROM zqkd_user"
|
|
|
if last_scanned_id > 0:
|
|
|
query += f" WHERE id > {last_scanned_id}"
|
|
|
query += " ORDER BY id ASC"
|
|
@@ -211,6 +236,5 @@ class RedisOperations:
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- db = DatabaseOperations("12", "123")
|
|
|
- user = db.select_user(10000)
|
|
|
- print(user)
|
|
|
+ db = DatabaseOperations("recommend", "zhongqingkandian")
|
|
|
+ print(db.get_today_videos())
|