更改任务为队列+并发的格式, 登录,获取账号配置, 更新账号配置功能为单独的锁能并发两次. 更改账号被检测到自动化时自动进行解锁账号
This commit is contained in:
@@ -17,6 +17,7 @@ from PIL import ImageGrab
|
||||
from loguru import logger
|
||||
from playwright._impl._page import Page
|
||||
from playwright.sync_api import sync_playwright, Error, TimeoutError
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from const import BUCKET, BASE_PATH
|
||||
from exceptions import AuthException, OperationFailed
|
||||
@@ -826,9 +827,19 @@ def check_account_status(page, cookies):
|
||||
|
||||
# 校验Cookies 是否失效
|
||||
# 登录页面判断
|
||||
retry_goto(page, 'https://www.facebook.com')
|
||||
time.sleep(3)
|
||||
|
||||
login_btn = page.query_selector_all('//button[@name="login"]')
|
||||
if login_btn:
|
||||
raise AuthException('该账户登录状态失效', 'invalid')
|
||||
|
||||
# 判断是否被检测到自动化,这种情况只需要点击按钮就可以继续
|
||||
if page.query_selector('//span[text()="We suspect automated behavior on your account"]') is not None:
|
||||
page.click('//span[text()="Dismiss"]')
|
||||
time.sleep(3)
|
||||
retry_goto(page, 'https://www.facebook.com')
|
||||
|
||||
# 判断是否为英文
|
||||
lang = page.locator('html').get_attribute('lang')
|
||||
if lang != "en":
|
||||
@@ -843,6 +854,7 @@ class RLock(threading._RLock):
|
||||
|
||||
|
||||
lock = RLock()
|
||||
login_semaphore = threading.Semaphore(2)
|
||||
|
||||
|
||||
def playwright_like(cookies, target_url):
|
||||
@@ -856,12 +868,14 @@ def playwright_like(cookies, target_url):
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
url = 'https://facebook.com'
|
||||
try:
|
||||
|
||||
page.goto(url)
|
||||
time.sleep(random.randint(3, 10))
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
if 'permalink.php?story_fbid' in target_url or '/posts/' in target_url:
|
||||
# 文字或图片类型
|
||||
button_xpath = '//div[@class="__fb-light-mode x1n2onr6 x1vjfegm"]//span[@data-ad-rendering-role="like_button"]'
|
||||
@@ -917,11 +931,13 @@ def playwright_post(cookies, content, image_key=None):
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
url = 'https://facebook.com'
|
||||
try:
|
||||
page.goto(url)
|
||||
time.sleep(random.randint(3, 10))
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
time.sleep(5)
|
||||
|
||||
if image_key:
|
||||
@@ -973,14 +989,11 @@ def playwright_comment(cookies, target_url, content, image_key=None):
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
url = 'https://facebook.com'
|
||||
try:
|
||||
retry_goto(page, url)
|
||||
time.sleep(random.randint(3, 10))
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
time.sleep(5)
|
||||
|
||||
page.goto(target_url)
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
try:
|
||||
retry_goto(page, target_url)
|
||||
sleep(1, 2)
|
||||
|
||||
if 'permalink.php?story_fbid' in target_url or '/posts/' in target_url or "/permalink/" in target_url:
|
||||
@@ -1038,7 +1051,7 @@ def playwright_comment(cookies, target_url, content, image_key=None):
|
||||
|
||||
def playwright_get_user_profile(cookies, username=None):
|
||||
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
|
||||
with lock:
|
||||
with login_semaphore:
|
||||
with sync_playwright() as playwright:
|
||||
update_windows_distinguish()
|
||||
browser = playwright.chromium.launch(
|
||||
@@ -1047,13 +1060,14 @@ def playwright_get_user_profile(cookies, username=None):
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
url = 'https://facebook.com'
|
||||
try:
|
||||
page.goto(url)
|
||||
retry_goto(page, url)
|
||||
time.sleep(random.randint(3, 10))
|
||||
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
profile_pic_url = page.locator(
|
||||
'//div[@aria-label="Shortcuts"]//li[1]//*[@preserveAspectRatio="xMidYMid slice"]').get_attribute(
|
||||
'xlink:href')
|
||||
@@ -1094,13 +1108,10 @@ def playwright_set_user_profile(cookies, username=None, first_name=None, last_na
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
url = 'https://www.facebook.com'
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
try:
|
||||
page.goto(url)
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
url = 'https://accountscenter.facebook.com/?entry_point=app_settings'
|
||||
page.goto(url)
|
||||
retry_goto(page, url)
|
||||
page.locator('//div[@role="list"]/div').first.click()
|
||||
|
||||
if username:
|
||||
@@ -1148,7 +1159,7 @@ def playwright_set_user_profile(cookies, username=None, first_name=None, last_na
|
||||
|
||||
def playwright_check_account_cookies(cookies):
|
||||
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
|
||||
with lock:
|
||||
with login_semaphore:
|
||||
with sync_playwright() as playwright:
|
||||
update_windows_distinguish()
|
||||
browser = playwright.chromium.launch(
|
||||
@@ -1157,10 +1168,8 @@ def playwright_check_account_cookies(cookies):
|
||||
context = browser.new_context(no_viewport=True)
|
||||
context.add_cookies(parse_cookies(cookies))
|
||||
page = context.new_page()
|
||||
url = 'https://www.facebook.com'
|
||||
page.goto(url)
|
||||
time.sleep(3)
|
||||
check_account_status(page, parse_cookies(cookies))
|
||||
|
||||
context.close()
|
||||
browser.close()
|
||||
return {}
|
||||
@@ -1224,7 +1233,7 @@ 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 login_semaphore:
|
||||
with sync_playwright() as playwright:
|
||||
update_windows_distinguish()
|
||||
browser = playwright.chromium.launch(
|
||||
|
||||
Reference in New Issue
Block a user