字段对齐

This commit is contained in:
SwZ
2025-04-14 14:45:16 +08:00
parent 3fe8e66c4f
commit 4dad69c84e
2 changed files with 58 additions and 38 deletions

View File

@@ -763,7 +763,7 @@ def parse_cookies(cookies):
except ValueError:
c = []
for i in cookies.split(';'):
kv = i.split('=')
kv = i.strip().split('=')
if kv:
k, v = kv[0], kv[1]
c.append({'name': k, 'value': v, 'domain': '.facebook.com', 'path': '/'})
@@ -800,7 +800,7 @@ class RLock(threading._RLock):
lock = RLock()
def playwright_like(cookies, post_id):
def playwright_like(cookies, target_url):
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
with lock:
with sync_playwright() as playwright:
@@ -818,19 +818,19 @@ def playwright_like(cookies, post_id):
page.goto(url)
_change_language(page)
if 'permalink.php?story_fbid' in post_id or '/posts/' in post_id:
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"]'
elif 'watch/?v' in post_id:
elif 'watch/?v' in target_url:
# 视频类型
button_xpath = '//span[@data-ad-rendering-role="like_button"][1]'
elif '/reel/' in post_id:
elif '/reel/' in target_url:
# 短视频类型
button_xpath = '//div[@class="__fb-dark-mode x1afcbsf x1uhb9sk x1swf91x"]//div[@aria-label="Like"]//div[@aria-label="Like"]'
else:
raise OperationFailed(f'不支持的帖子类型POST: {post_id}')
raise OperationFailed(f'不支持的帖子类型POST: {target_url}')
page.goto(post_id)
page.goto(target_url)
sleep(1, 2)
page.click(button_xpath)
time.sleep(10)
@@ -838,12 +838,15 @@ def playwright_like(cookies, post_id):
check_account_status(page)
raise OperationFailed(f'操作超时,请重试{e}')
content = _full_screenshot()
screenshot_content = _full_screenshot()
context.close()
browser.close()
key = f'{uuid.uuid4()}.png'
put_object(key, content)
return {'screenshot_key': key}
put_object(key, screenshot_content)
return {
'response_url': target_url,
'screenshot_key': [key]
}
def playwright_post(cookies, content, image_key=None):
@@ -894,18 +897,18 @@ def playwright_post(cookies, content, image_key=None):
check_account_status(page)
raise OperationFailed(f'操作超时,请重试{e}')
content = _full_screenshot()
screenshot_content = _full_screenshot()
if image_key:
os.remove(file_path)
context.close()
browser.close()
key = f'{uuid.uuid4()}.png'
put_object(key, content)
return {'response_url': post_url, 'screenshot_key': key}
put_object(key, screenshot_content)
return {'response_url': post_url, 'screenshot_key': [key]}
def playwright_comment(cookies, post_id, message, image_key=None):
def playwright_comment(cookies, target_url, content, image_key=None):
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
with lock:
with sync_playwright() as playwright:
@@ -924,22 +927,22 @@ def playwright_comment(cookies, post_id, message, image_key=None):
_change_language(page)
time.sleep(5)
page.goto(post_id)
page.goto(target_url)
sleep(1, 2)
if 'permalink.php?story_fbid' in post_id or '/posts/' in post_id:
if 'permalink.php?story_fbid' in target_url or '/posts/' in target_url:
# 文字或图片类型
input_xpath = '//div[@aria-label="Write a comment…"]'
attach_xpath = '//div[@id="focused-state-actions-list"]//div[@aria-label="Attach a photo or video"]'
comment_xpath = '//div[@aria-label="Comment"]'
page.click(input_xpath)
sleep(1, 2)
elif 'watch/?v' in post_id:
elif 'watch/?v' in target_url:
# 视频类型
input_xpath = '//div[@aria-label="Write a comment…"]'
attach_xpath = '//div[@aria-label="Attach a photo or video"]'
comment_xpath = '//div[@aria-label="Comment"]'
elif '/reel/' in post_id:
elif '/reel/' in target_url:
# 短视频类型
input_xpath = '//div[@aria-label="Write a comment…"]'
attach_xpath = '//div[@aria-label="Attach a photo or video"]'
@@ -947,7 +950,7 @@ def playwright_comment(cookies, post_id, message, image_key=None):
page.click('//div[@aria-label="Comment"][1]')
sleep(1, 2)
else:
raise OperationFailed(f'不支持的帖子类型POST: {post_id}')
raise OperationFailed(f'不支持的帖子类型POST: {target_url}')
if image_key:
filename = image_key.split('/')[-1]
@@ -960,22 +963,25 @@ def playwright_comment(cookies, post_id, message, image_key=None):
file_chooser.set_files(file_path)
time.sleep(5)
page.type(input_xpath, message)
page.type(input_xpath, content)
page.click(comment_xpath)
time.sleep(5)
except Error as e:
check_account_status(page)
raise OperationFailed(f'操作超时,请重试{e}')
content = _full_screenshot()
screenshot_content = _full_screenshot()
if image_key:
os.remove(file_path)
context.close()
browser.close()
key = f'{uuid.uuid4()}.png'
put_object(key, content)
return {'screenshot_key': key}
put_object(key, screenshot_content)
return {
'response_url': target_url,
'screenshot_key': [key]
}
def playwright_get_user_profile(cookies, username=None):
@@ -1020,8 +1026,8 @@ def playwright_get_user_profile(cookies, username=None):
return {'avatar_key': key, 'username': username, 'first_name': firstname, 'last_name': lastname}
def playwright_set_user_profile(cookies, firstname=None, lastname=None, image_key=None):
if not firstname and not lastname and not image_key:
def playwright_set_user_profile(cookies, username=None, first_name=None, last_name=None, avatar_key=None):
if not first_name and not last_name and not avatar_key and not username:
return
path = os.path.join(BASE_PATH, 'chrome', '130-0008', 'chrome.exe')
@@ -1045,23 +1051,28 @@ def playwright_set_user_profile(cookies, firstname=None, lastname=None, image_ke
page.goto(url)
page.locator('//div[@role="list"]/div').first.click()
if firstname or lastname:
if firstname and lastname:
if username:
page.click('//a[@aria-label="Username"]')
page.locator('//input').first.fill(username)
page.click('//span[text()="Done"]')
if first_name or last_name:
if first_name and last_name:
# 修改名称
page.click('//a[@aria-label="Name"]')
page.locator('//input').first.fill(firstname)
page.locator('//input').last.fill(lastname)
page.locator('//input').first.fill(first_name)
page.locator('//input').last.fill(last_name)
page.locator('//div[@role="button"]').last.click()
page.click('//span[text()="Done"]')
else:
raise OperationFailed('名称中必须有First name和Last name')
if image_key:
if avatar_key:
# 修改头像
page.click('//a[@aria-label="Profile picture"]')
filename = image_key.split('/')[-1]
filename = avatar_key.split('/')[-1]
file_path = os.path.join(BASE_PATH, 'files', filename)
client.fget_object(BUCKET, image_key, file_path)
client.fget_object(BUCKET, avatar_key, file_path)
sleep(1, 2)
with page.expect_file_chooser() as fc_info:
page.click('//div[text()="Upload new photo"]')
@@ -1076,7 +1087,12 @@ def playwright_set_user_profile(cookies, firstname=None, lastname=None, image_ke
context.close()
browser.close()
return {}
return {
'username': username,
'first_name': first_name,
'last_name': last_name,
'avatar_key': avatar_key,
}
def playwright_check_account_cookies(cookies):
@@ -1103,7 +1119,7 @@ def playwright_check_account_cookies(cookies):
if __name__ == '__main__':
# cookies = 'sb=mC6pxQuLvNLPTNpF-b9Tk8tK;c_user=61570961343759;xs=18:fBZ4XJkFA69uNg:2:1734940655:-1:-1;fr=0JjwzD0HEedbQSHGt.AWUHNQcfxnkbZ3j5rVd1NgTTGhA.BnaRfv..AAA.0.0.BnaRfv.AWWF3uukqmQ;datr=7xdpZzxiItbht8A5aCDBAhQU'
cookies = 'datr=UdBVZy_TbLb_SPycLuUOVfpB;sb=UdBVZ509Gl0kpUXQ_P_pcmy2;m_pixel_ratio=2;wd=360x590;fr=0HivUSkxlYXi0Scep.AWXQIH6ShK78BdDqLHVgAuV2cIkb3765V9gKDw.BnVdBR..AAA.0.0.BnvaSE.AWWFQz-vcoU;locale=en_US;c_user=61570226580861;xs=50%3APFRJIPR2ekAw8g%3A2%3A1740481664%3A-1%3A-1'
cookies = 'c_user=61573698353746;xs=2:fEV887K0i6K7kA:2:1743959713:-1:-1;fr=0W6Rif6GTiEeVWr1z.AWfSxIttggk4A9uSoQhL0TsOnL0uGWTTz8tAf53hslLXxeNaq4E.Bn8ram..AAA.0.0.Bn8ram.AWcw8GPv2RuqrHRC03M2PJo6wN8;datr=oLbyZ5IUZm0W5Tcz-HYNb3Jk'
# cookies = {
# 'datr': '4MXgZ3twsUMLaR7_yYPjboTs',
# 'sb': '4MXgZ5AKdd6AiVqGy_N0-cpe',
@@ -1119,7 +1135,7 @@ if __name__ == '__main__':
# like(cookies, 'ZmVlZGJhY2s6MTIyMTA5NjE0NjU0NzkzNzc5')
# comment(cookies, 'ZmVlZGJhY2s6MTIyMTA5NjE0NjU0NzkzNzc5', 'game la', 'xzpq.mp4')
# playwright_like(cookies, 'https://www.facebook.com/watch/?v=1007800324567828')
# print(playwright_post(cookies, '2025-3-230~like'))
# print(playwright_post(cookie, '2025-3-230~like'))
# playwright_post(cookies, '2025-3-26~like', 'rg.jpg')
# playwright_comment(
# cookies,
@@ -1133,9 +1149,10 @@ if __name__ == '__main__':
# print(playwright_get_user_profile(cookies))
# playwright_set_user_profile(
# cookies,
# firstname='Lisa',
# lastname='Keals',
# image_key='rg.jpg'
# username='facebaby66'
# # firstname='Lisa',
# # lastname='Keals',
# # image_key='rg.jpg'
# )
# playwright_check_account_cookies(cookies)