修复发视频后无法获取到链接和截图失败的问题
This commit is contained in:
@@ -6,18 +6,18 @@ import sys
|
|||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
import pyotp
|
import pyotp
|
||||||
import pywintypes
|
import pywintypes
|
||||||
import requests
|
import requests
|
||||||
from fake_useragent import UserAgent # 导入 UserAgent
|
|
||||||
import win32api
|
import win32api
|
||||||
import win32con
|
import win32con
|
||||||
from PIL import ImageGrab
|
from PIL import ImageGrab
|
||||||
|
from fake_useragent import UserAgent # 导入 UserAgent
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from playwright._impl._page import Page
|
from playwright._impl._page import Page
|
||||||
from playwright.sync_api import sync_playwright, Error, TimeoutError
|
from playwright.sync_api import sync_playwright, Error, TimeoutError
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
|
||||||
|
|
||||||
from const import BUCKET, BASE_PATH
|
from const import BUCKET, BASE_PATH
|
||||||
from exceptions import AuthException, OperationFailed
|
from exceptions import AuthException, OperationFailed
|
||||||
@@ -231,6 +231,55 @@ def playwright_like(cookies, target_url):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# 获取个个人主页视频数量
|
||||||
|
def get_video_count(page, cookies):
|
||||||
|
# 进入个人主页视频页面获取最新视频链接
|
||||||
|
cookies = {i['name']: i['value'] for i in parse_cookies(cookies)}
|
||||||
|
uid = cookies['c_user']
|
||||||
|
videos_url = f"https://www.facebook.com/profile.php?id={uid}&sk=videos" # 替换为实际的个人主页URL
|
||||||
|
retry_goto(page, videos_url)
|
||||||
|
videos = page.query_selector_all('//img[@alt="View photo"]')
|
||||||
|
return len(videos)
|
||||||
|
|
||||||
|
|
||||||
|
def retry_get_new_video(page, cookies, video_count):
|
||||||
|
"""
|
||||||
|
每分钟重试一次,5分钟内没获取到新视频则抛出Timeout异常
|
||||||
|
|
||||||
|
Args:
|
||||||
|
page: Playwright页面对象
|
||||||
|
cookies: Cookies
|
||||||
|
video_count: 初始视频数量
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
函数执行结果或抛出Timeout(如果超时)
|
||||||
|
"""
|
||||||
|
max_duration = 5 * 60 # 5分钟(秒)
|
||||||
|
start_time = time.time()
|
||||||
|
attempt = 1
|
||||||
|
|
||||||
|
while time.time() - start_time < max_duration:
|
||||||
|
# 获取当前视频数量
|
||||||
|
new_videO_count = get_video_count(page, cookies)
|
||||||
|
# 如果新视频数量大于初始视频数量,则表示有新视频上传
|
||||||
|
if new_videO_count > video_count:
|
||||||
|
page.query_selector_all('//img[@alt="View photo"]')[0].click()
|
||||||
|
page.reload()
|
||||||
|
return page.url
|
||||||
|
|
||||||
|
# 计算下一次重试时间
|
||||||
|
elapsed = time.time() - start_time
|
||||||
|
if elapsed < max_duration:
|
||||||
|
sleep_time = 60 - (elapsed % 60) # 确保每分钟执行一次
|
||||||
|
print(f"等待 {sleep_time:.1f} 秒后重试...")
|
||||||
|
time.sleep(sleep_time)
|
||||||
|
|
||||||
|
attempt += 1
|
||||||
|
|
||||||
|
print("5分钟超时,退出重试")
|
||||||
|
raise TimeoutError("未获取到新视频(可能视频上传失败),已超时5分钟")
|
||||||
|
|
||||||
|
|
||||||
def playwright_post(cookies, content, image_key=None):
|
def playwright_post(cookies, content, image_key=None):
|
||||||
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
|
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
|
||||||
with lock:
|
with lock:
|
||||||
@@ -244,9 +293,15 @@ def playwright_post(cookies, content, image_key=None):
|
|||||||
page = context.new_page()
|
page = context.new_page()
|
||||||
|
|
||||||
check_account_status(page, parse_cookies(cookies))
|
check_account_status(page, parse_cookies(cookies))
|
||||||
|
# 声明默认发布视频数量
|
||||||
|
video_count = 0
|
||||||
|
|
||||||
url = 'https://facebook.com'
|
url = 'https://facebook.com'
|
||||||
try:
|
try:
|
||||||
|
# 先获取视频数量
|
||||||
|
if ".mp4" in image_key:
|
||||||
|
video_count = get_video_count(page, cookies)
|
||||||
|
|
||||||
retry_goto(page, url)
|
retry_goto(page, url)
|
||||||
time.sleep(random.randint(3, 10))
|
time.sleep(random.randint(3, 10))
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
@@ -272,6 +327,9 @@ def playwright_post(cookies, content, image_key=None):
|
|||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
page.reload()
|
page.reload()
|
||||||
post_url = page.url
|
post_url = page.url
|
||||||
|
# 视频格式要单独去获取链接
|
||||||
|
if ".mp4" in image_key:
|
||||||
|
post_url = retry_get_new_video(page, cookies, video_count)
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
raise OperationFailed(f'操作超时,请重试{e}')
|
raise OperationFailed(f'操作超时,请重试{e}')
|
||||||
|
|||||||
Reference in New Issue
Block a user