喵视频等显示时机

This commit is contained in:
ucmao
2026-03-03 14:09:08 +08:00
parent d1c48d9fbf
commit 899601ea7d
3 changed files with 54 additions and 24 deletions
+31 -21
View File
@@ -32,39 +32,49 @@ def refresh_video():
# 获取封面 视频 和标题
title = cover_url = video_url = None
if platform == '小红书':
max_attempts = 5
attempts = 0
while attempts < max_attempts:
try:
if platform == '小红书':
max_attempts = 5
attempts = 0
while attempts < max_attempts:
downloader = DownloaderFactory.create_downloader(platform, real_url)
title = downloader.get_title_content()
video_url = downloader.get_real_video_url()
cover_url = downloader.get_cover_photo_url()
if video_url:
break
attempts += 1
logger.debug(f"Attempt {attempts} failed. Retrying...")
if not video_url:
logger.error("Failed to retrieve video URL after 5 attempts.")
else:
downloader = DownloaderFactory.create_downloader(platform, real_url)
title = downloader.get_title_content()
video_url = downloader.get_real_video_url()
cover_url = downloader.get_cover_photo_url()
if video_url:
break
attempts += 1
logger.debug(f"Attempt {attempts} failed. Retrying...")
if not video_url:
logger.error("Failed to retrieve video URL after 5 attempts.")
else:
downloader = DownloaderFactory.create_downloader(platform, real_url)
title = downloader.get_title_content()
video_url = downloader.get_real_video_url()
cover_url = downloader.get_cover_photo_url()
except Exception as downloader_error:
logger.error(f"Downloader Error: {downloader_error}")
video_url = None # 确保 video_url 为 None 以进入失败逻辑
updated_video_url = UrlParser.convert_to_https(video_url)
updated_cover_url = UrlParser.convert_to_https(cover_url)
if updated_video_url:
# 成功获取视频,重置失败次数
DataStorageManager.reset_refresh_fail_count(request_video_id)
try:
DataStorageManager.reset_refresh_fail_count(request_video_id)
except Exception as db_err:
logger.error(f"Database Reset Fail Error: {db_err}")
else:
# 获取失败,增加失败次数
fail_count = DataStorageManager.increment_refresh_fail_count(request_video_id)
logger.warning(f"Video {request_video_id} refresh failed, count: {fail_count}")
if fail_count >= 3:
DataStorageManager.delete_video(request_video_id)
logger.info(f"Video {request_video_id} deleted due to {fail_count} refresh failures")
try:
fail_count = DataStorageManager.increment_refresh_fail_count(request_video_id)
logger.warning(f"Video {request_video_id} refresh failed, count: {fail_count}")
if fail_count >= 3:
DataStorageManager.delete_video(request_video_id)
logger.info(f"Video {request_video_id} deleted due to {fail_count} refresh failures")
except Exception as db_err:
logger.error(f"Database Fail Count Error: {db_err}")
return make_response(200, '视频获取中,请稍后再试', None, None, False), 200
+11
View File
@@ -458,6 +458,13 @@ class DBManager:
self.conn.commit()
return result[0] if result else 0
except mysql.connector.Error as e:
self.conn.rollback()
if e.errno == 1054: # Column not found
logger.error("Column 'refresh_fail_count' is missing in 'parse_library' table. Please run the ALTER TABLE SQL.")
else:
logger.error(f"MySQL Error in increment_refresh_fail_count: {e}")
raise e
except Exception as e:
self.conn.rollback()
logger.error(f"增加视频[{video_id}]失败次数失败:{str(e)}", exc_info=True)
@@ -478,6 +485,10 @@ class DBManager:
cursor.execute(sql, (video_id,))
self.conn.commit()
return cursor.rowcount > 0
except mysql.connector.Error as e:
self.conn.rollback()
logger.error(f"MySQL Error in delete_video_by_id: {e}")
raise e
except Exception as e:
self.conn.rollback()
logger.error(f"删除视频[{video_id}]失败:{str(e)}", exc_info=True)
+12 -3
View File
@@ -51,28 +51,37 @@ class DouyinDownloader(BaseDownloader):
def get_real_video_url(self):
try:
data_dict = self.data
if not data_dict:
return None
play_addr_list = data_dict['aweme_detail']['video']['bit_rate'][0]['play_addr']['url_list']
# play_addr_list[0]:主CDN节点; play_addr_list[1]:备用CDN节点; play_addr_list[2]:抖音官方的源站URL
play_addr = play_addr_list[2]
return play_addr
except (KeyError, json.JSONDecodeError) as e:
except (KeyError, json.JSONDecodeError, TypeError) as e:
logger.warning(f"Failed to parse video URL: {e}")
return None
def get_title_content(self):
try:
data_dict = self.data
if not data_dict:
return None
title_content = data_dict['aweme_detail']['desc']
return title_content
except (KeyError, json.JSONDecodeError) as e:
except (KeyError, json.JSONDecodeError, TypeError) as e:
logger.warning(f"Failed to parse title content: {e}")
return None
def get_cover_photo_url(self):
try:
data_dict = self.data
if not data_dict:
return None
play_cover = data_dict['aweme_detail']['video']['cover_original_scale']['url_list'][0]
return play_cover
except (KeyError, json.JSONDecodeError) as e:
except (KeyError, json.JSONDecodeError, TypeError) as e:
logger.warning(f"Failed to parse cover URL: {e}")
return None
if __name__ == '__main__':