feat: 增加 xiaohongshu 的登录和发布相关脚本
This commit is contained in:
+74
-3
@@ -217,6 +217,77 @@ async def get_ks_cookie(id,status_queue):
|
||||
conn.commit()
|
||||
print("✅ 用户状态已记录")
|
||||
status_queue.put("200")
|
||||
#
|
||||
# a = asyncio.run(douyin_cookie_gen(2,None))
|
||||
# print(a)
|
||||
|
||||
# 小红书登录
|
||||
async def xiaohongshu_cookie_gen(id,status_queue):
|
||||
url_changed_event = asyncio.Event()
|
||||
|
||||
async def on_url_change():
|
||||
# 检查是否是主框架的变化
|
||||
if page.url != original_url:
|
||||
url_changed_event.set()
|
||||
|
||||
async with async_playwright() as playwright:
|
||||
options = {
|
||||
'args': [
|
||||
'--lang en-GB'
|
||||
],
|
||||
'headless': False, # Set headless option here
|
||||
}
|
||||
# Make sure to run headed.
|
||||
browser = await playwright.chromium.launch(**options)
|
||||
# Setup context however you like.
|
||||
context = await browser.new_context() # Pass any options
|
||||
context = await set_init_script(context)
|
||||
# Pause the page, and start recording manually.
|
||||
page = await context.new_page()
|
||||
await page.goto("https://creator.xiaohongshu.com/")
|
||||
await page.locator('img.css-wemwzq').click()
|
||||
|
||||
img_locator = page.get_by_role("img").nth(2)
|
||||
# 获取 src 属性值
|
||||
src = await img_locator.get_attribute("src")
|
||||
original_url = page.url
|
||||
print("✅ 图片地址:", src)
|
||||
status_queue.put(src)
|
||||
# 监听页面的 'framenavigated' 事件,只关注主框架的变化
|
||||
page.on('framenavigated',
|
||||
lambda frame: asyncio.create_task(on_url_change()) if frame == page.main_frame else None)
|
||||
|
||||
try:
|
||||
# 等待 URL 变化或超时
|
||||
await asyncio.wait_for(url_changed_event.wait(), timeout=200) # 最多等待 200 秒
|
||||
print("监听页面跳转成功")
|
||||
except asyncio.TimeoutError:
|
||||
status_queue.put("500")
|
||||
print("监听页面跳转超时")
|
||||
await page.close()
|
||||
await context.close()
|
||||
await browser.close()
|
||||
return None
|
||||
uuid_v1 = uuid.uuid1()
|
||||
print(f"UUID v1: {uuid_v1}")
|
||||
await context.storage_state(path=Path(BASE_DIR / "cookiesFile" / f"{uuid_v1}.json"))
|
||||
result = await check_cookie(1, f"{uuid_v1}.json")
|
||||
if not result:
|
||||
status_queue.put("500")
|
||||
await page.close()
|
||||
await context.close()
|
||||
await browser.close()
|
||||
return None
|
||||
await page.close()
|
||||
await context.close()
|
||||
await browser.close()
|
||||
|
||||
with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('''
|
||||
INSERT INTO user_info (type, filePath, userName, status)
|
||||
VALUES (?, ?, ?, ?)
|
||||
''', (1, f"{uuid_v1}.json", id, 1))
|
||||
conn.commit()
|
||||
print("✅ 用户状态已记录")
|
||||
status_queue.put("200")
|
||||
|
||||
# a = asyncio.run(xiaohongshu_cookie_gen(4,None))
|
||||
# print(a)
|
||||
|
||||
Reference in New Issue
Block a user