单独封装检查是否冻结函数, 登录前校验账号是否冻结. 减少登录时间, 登录后判断cookies中是否有c_user字段。

This commit is contained in:
work
2025-05-23 15:20:16 +08:00
parent bd65dfaf8f
commit 2b857b2685

View File

@@ -782,7 +782,7 @@ def parse_cookies(cookies):
return cookies
def check_account_status(page, cookies):
def check_freeze_account(uid):
# 检查是否冻结
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
@@ -806,14 +806,20 @@ def check_account_status(page, cookies):
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
'viewport-width': '743',
}
cookies = {i['name']: i['value'] for i in cookies}
uid = cookies['c_user']
url = f"https://graph.facebook.com/{uid}/picture?type=normal"
response = requests.get(url, headers=headers, allow_redirects=False)
if response.status_code == 302:
if response.headers.get('Location') == 'https://static.xx.fbcdn.net/rsrc.php/v1/yh/r/C5yt7Cqf3zU.jpg':
raise AuthException('该账号已被冻结', 'frozen')
def check_account_status(page, cookies):
# 检查是否冻结
cookies = {i['name']: i['value'] for i in cookies}
uid = cookies['c_user']
check_freeze_account(uid)
# 校验Cookies 是否失效
# 登录页面判断
login_btn = page.query_selector_all('//button[@name="login"]')
@@ -1164,6 +1170,9 @@ def get_login_continue_btn(page):
def playwright_login(username, password, code_2fa=None):
# 检查是否冻结
check_freeze_account(username)
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
with lock:
with sync_playwright() as playwright:
@@ -1177,14 +1186,14 @@ def playwright_login(username, password, code_2fa=None):
url = 'https://www.facebook.com'
page.goto(url)
time.sleep(random.randint(1, 10))
page.locator('//input[@id="email"]').type(username, delay=100)
time.sleep(random.randint(1, 10))
page.locator('//input[@id="pass"]').type(password, delay=100)
time.sleep(random.randint(1, 10))
page.locator('//input[@id="email"]').type(username, delay=30)
time.sleep(random.randint(1, 3))
page.locator('//input[@id="pass"]').type(password, delay=30)
time.sleep(random.randint(1, 3))
page.click('//button[@name="login"]')
page.wait_for_load_state()
time.sleep(random.randint(3, 10))
time.sleep(random.randint(3, 5))
# 设置语言为英文
context.add_cookies([
{
@@ -1197,9 +1206,9 @@ def playwright_login(username, password, code_2fa=None):
"secure": False,
},
])
time.sleep(random.randint(1, 10))
time.sleep(random.randint(1, 3))
page.reload()
time.sleep(random.randint(1, 10))
time.sleep(random.randint(1, 3))
captcha_img = page.query_selector_all('//img[contains(@src, "captcha")]')
if captcha_img:
@@ -1221,8 +1230,9 @@ def playwright_login(username, password, code_2fa=None):
page.locator('//img[contains(@src, "captcha")]/parent::div/parent::div/div').nth(4).click()
else:
raise OperationFailed('验证码解析错误')
time.sleep(8)
time.sleep(3)
page.wait_for_load_state()
# 检查是否还有验证码
h2 = page.query_selector("//h2/span")
if h2 is None:
raise OperationFailed('页面有误')
@@ -1251,7 +1261,8 @@ def playwright_login(username, password, code_2fa=None):
time.sleep(1)
page.locator('//label[text()="Code"]/preceding-sibling::input').fill(auth_code)
page.click('//span[text()="Continue"]')
# 这里验证可能会很慢, 做显示等待
time.sleep(10)
save_profile = page.query_selector('//span[text()="Save"]')
if save_profile:
save_profile.click()
@@ -1259,8 +1270,11 @@ def playwright_login(username, password, code_2fa=None):
if trust_device_select:
trust_device_select.click()
time.sleep(10)
time.sleep(3)
c = {i['name']: i['value'] for i in context.cookies()}
if c["c_user"] is None:
raise OperationFailed("操作失败")
context.close()
browser.close()
return {'cookies': json.dumps(c)}
@@ -1301,6 +1315,6 @@ if __name__ == '__main__':
# 视频链接的帖子点赞
print(playwright_like(cookies, "https://www.facebook.com/watch/?v=693587939886449"))
# cookies = playwright_login('61575975148121', 'Mu1711rstu1999', 'JNCCTXB34EP5ME6RU6RND6SHBQ6KATAR')
cookies = playwright_login('61575975148121', 'Mu1711rstu1999', 'JNCCTXB34EP5ME6RU6RND6SHBQ6KATAR')
# print(cookies)
pass