Edan233 commited on
Commit
812bd80
·
1 Parent(s): 0c6942a

feat: 增加 xiaohongshu 的登录和发布相关脚本

Browse files
examples/get_xiaohongshu_cookie.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from pathlib import Path
3
+
4
+ from conf import BASE_DIR
5
+ from uploader.xiaohongshu_uploader.main import xiaohongshu_setup
6
+
7
+ if __name__ == '__main__':
8
+ account_file = Path(BASE_DIR / "cookies" / "xiaohongshu_uploader" / "account.json")
9
+ account_file.parent.mkdir(exist_ok=True)
10
+ cookie_setup = asyncio.run(xiaohongshu_setup(str(account_file), handle=True))
examples/upload_video_to_xiaohongshu.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ from pathlib import Path
3
+
4
+ from conf import BASE_DIR
5
+ from uploader.xiaohongshu_uploader.main import xiaohongshu_setup, XiaoHongShuVideo
6
+ from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags
7
+
8
+
9
+ if __name__ == '__main__':
10
+ filepath = Path(BASE_DIR) / "videos"
11
+ account_file = Path(BASE_DIR / "cookies" / "xiaohongshu_uploader" / "58a391ba-4082-11f0-a321-44e51723d63c.json")
12
+ # 获取视频目录
13
+ folder_path = Path(filepath)
14
+ # 获取文件夹中的所有文件
15
+ files = list(folder_path.glob("*.mp4"))
16
+ file_num = len(files)
17
+ publish_datetimes = generate_schedule_time_next_day(file_num, 1, daily_times=[16])
18
+ cookie_setup = asyncio.run(xiaohongshu_setup(account_file, handle=False))
19
+ for index, file in enumerate(files):
20
+ title, tags = get_title_and_hashtags(str(file))
21
+ thumbnail_path = file.with_suffix('.png')
22
+ # 打印视频文件名、标题和 hashtag
23
+ print(f"视频文件名:{file}")
24
+ print(f"标题:{title}")
25
+ print(f"Hashtag:{tags}")
26
+ # 暂时没有时间修复封面上传,故先隐藏掉该功能
27
+ # if thumbnail_path.exists():
28
+ # app = XiaoHongShuVideo(title, file, tags, publish_datetimes[index], account_file, thumbnail_path=thumbnail_path)
29
+ # else:
30
+ app = XiaoHongShuVideo(title, file, tags, 0, account_file)
31
+ asyncio.run(app.main(), debug=False)
myUtils/auth.py CHANGED
@@ -71,22 +71,36 @@ async def cookie_auth_ks(account_file):
71
  return True
72
 
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  async def check_cookie(type,file_path):
76
  match type:
77
  # 小红书
78
  case 1:
79
- config = configparser.RawConfigParser()
80
- config.read(Path(BASE_DIR / "cookiesFile" / file_path))
81
- cookies = config['account1']['cookies']
82
- xhs_client = XhsClient(cookies, sign=sign_local, timeout=60)
83
- # auth cookie
84
- # 注意:该校验cookie方式可能并没那么准确
85
- try:
86
- xhs_client.get_video_first_frame_image_id("3214")
87
- except:
88
- print("cookie 失效")
89
- return True
90
  # 视频号
91
  case 2:
92
  return await cookie_auth_tencent(Path(BASE_DIR / "cookiesFile" / file_path))
@@ -99,5 +113,5 @@ async def check_cookie(type,file_path):
99
  case _:
100
  return False
101
 
102
- # a = asyncio.run(check_cookie(4,"3a6cfdc0-3d51-11f0-8507-44e51723d63c.json"))
103
  # print(a)
 
71
  return True
72
 
73
 
74
+ async def cookie_auth_xhs(account_file):
75
+ async with async_playwright() as playwright:
76
+ browser = await playwright.chromium.launch(headless=True)
77
+ context = await browser.new_context(storage_state=account_file)
78
+ context = await set_init_script(context)
79
+ # 创建一个新的页面
80
+ page = await context.new_page()
81
+ # 访问指定的 URL
82
+ await page.goto("https://creator.xiaohongshu.com/creator-micro/content/upload")
83
+ try:
84
+ await page.wait_for_url("https://creator.xiaohongshu.com/creator-micro/content/upload", timeout=5000)
85
+ except:
86
+ print("[+] 等待5秒 cookie 失效")
87
+ await context.close()
88
+ await browser.close()
89
+ return False
90
+ # 2024.06.17 抖音创作者中心改版
91
+ if await page.get_by_text('手机号登录').count() or await page.get_by_text('扫码登录').count():
92
+ print("[+] 等待5秒 cookie 失效")
93
+ return False
94
+ else:
95
+ print("[+] cookie 有效")
96
+ return True
97
+
98
 
99
  async def check_cookie(type,file_path):
100
  match type:
101
  # 小红书
102
  case 1:
103
+ return await cookie_auth_xhs(Path(BASE_DIR / "cookiesFile" / file_path))
 
 
 
 
 
 
 
 
 
 
104
  # 视频号
105
  case 2:
106
  return await cookie_auth_tencent(Path(BASE_DIR / "cookiesFile" / file_path))
 
113
  case _:
114
  return False
115
 
116
+ # a = asyncio.run(check_cookie(1,"3a6cfdc0-3d51-11f0-8507-44e51723d63c.json"))
117
  # print(a)
myUtils/login.py CHANGED
@@ -217,6 +217,77 @@ async def get_ks_cookie(id,status_queue):
217
  conn.commit()
218
  print("✅ 用户状态已记录")
219
  status_queue.put("200")
220
- #
221
- # a = asyncio.run(douyin_cookie_gen(2,None))
222
- # print(a)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  conn.commit()
218
  print("✅ 用户状态已记录")
219
  status_queue.put("200")
220
+
221
+ # 小红书登录
222
+ async def xiaohongshu_cookie_gen(id,status_queue):
223
+ url_changed_event = asyncio.Event()
224
+
225
+ async def on_url_change():
226
+ # 检查是否是主框架的变化
227
+ if page.url != original_url:
228
+ url_changed_event.set()
229
+
230
+ async with async_playwright() as playwright:
231
+ options = {
232
+ 'args': [
233
+ '--lang en-GB'
234
+ ],
235
+ 'headless': False, # Set headless option here
236
+ }
237
+ # Make sure to run headed.
238
+ browser = await playwright.chromium.launch(**options)
239
+ # Setup context however you like.
240
+ context = await browser.new_context() # Pass any options
241
+ context = await set_init_script(context)
242
+ # Pause the page, and start recording manually.
243
+ page = await context.new_page()
244
+ await page.goto("https://creator.xiaohongshu.com/")
245
+ await page.locator('img.css-wemwzq').click()
246
+
247
+ img_locator = page.get_by_role("img").nth(2)
248
+ # 获取 src 属性值
249
+ src = await img_locator.get_attribute("src")
250
+ original_url = page.url
251
+ print("✅ 图片地址:", src)
252
+ status_queue.put(src)
253
+ # 监听页面的 'framenavigated' 事件,只关注主框架的变化
254
+ page.on('framenavigated',
255
+ lambda frame: asyncio.create_task(on_url_change()) if frame == page.main_frame else None)
256
+
257
+ try:
258
+ # 等待 URL 变化或超时
259
+ await asyncio.wait_for(url_changed_event.wait(), timeout=200) # 最多等待 200 秒
260
+ print("监听页面跳转成功")
261
+ except asyncio.TimeoutError:
262
+ status_queue.put("500")
263
+ print("监听页面跳转超时")
264
+ await page.close()
265
+ await context.close()
266
+ await browser.close()
267
+ return None
268
+ uuid_v1 = uuid.uuid1()
269
+ print(f"UUID v1: {uuid_v1}")
270
+ await context.storage_state(path=Path(BASE_DIR / "cookiesFile" / f"{uuid_v1}.json"))
271
+ result = await check_cookie(1, f"{uuid_v1}.json")
272
+ if not result:
273
+ status_queue.put("500")
274
+ await page.close()
275
+ await context.close()
276
+ await browser.close()
277
+ return None
278
+ await page.close()
279
+ await context.close()
280
+ await browser.close()
281
+
282
+ with sqlite3.connect(Path(BASE_DIR / "db" / "database.db")) as conn:
283
+ cursor = conn.cursor()
284
+ cursor.execute('''
285
+ INSERT INTO user_info (type, filePath, userName, status)
286
+ VALUES (?, ?, ?, ?)
287
+ ''', (1, f"{uuid_v1}.json", id, 1))
288
+ conn.commit()
289
+ print("✅ 用户状态已记录")
290
+ status_queue.put("200")
291
+
292
+ # a = asyncio.run(xiaohongshu_cookie_gen(4,None))
293
+ # print(a)
myUtils/postVideo.py CHANGED
@@ -5,6 +5,7 @@ from conf import BASE_DIR
5
  from uploader.douyin_uploader.main import DouYinVideo
6
  from uploader.ks_uploader.main import KSVideo
7
  from uploader.tencent_uploader.main import TencentVideo
 
8
  from utils.constant import TencentZoneTypes
9
  from utils.files_times import generate_schedule_time_next_day
10
 
@@ -65,8 +66,23 @@ def post_video_ks(title,files,tags,account_file,category=TencentZoneTypes.LIFEST
65
  app = KSVideo(title, str(file), tags, publish_datetimes[index], cookie)
66
  asyncio.run(app.main(), debug=False)
67
 
68
- # def post_every_type(list):
69
- # for task_dist in list:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
 
 
5
  from uploader.douyin_uploader.main import DouYinVideo
6
  from uploader.ks_uploader.main import KSVideo
7
  from uploader.tencent_uploader.main import TencentVideo
8
+ from uploader.xiaohongshu_uploader.main import XiaoHongShuVideo
9
  from utils.constant import TencentZoneTypes
10
  from utils.files_times import generate_schedule_time_next_day
11
 
 
66
  app = KSVideo(title, str(file), tags, publish_datetimes[index], cookie)
67
  asyncio.run(app.main(), debug=False)
68
 
69
+ def post_video_xhs(title,files,tags,account_file,category=TencentZoneTypes.LIFESTYLE.value,enableTimer=False,videos_per_day = 1, daily_times=None,start_days = 0):
70
+ # 生成文件的完整路径
71
+ account_file = [Path(BASE_DIR / "cookiesFile" / file) for file in account_file]
72
+ files = [Path(BASE_DIR / "videoFile" / file) for file in files]
73
+ file_num = len(files)
74
+ if enableTimer:
75
+ publish_datetimes = generate_schedule_time_next_day(file_num, videos_per_day, daily_times,start_days)
76
+ else:
77
+ publish_datetimes = 0
78
+ for index, file in enumerate(files):
79
+ for cookie in account_file:
80
+ # 打印视频文件名、标题和 hashtag
81
+ print(f"视频文件名:{file}")
82
+ print(f"标题:{title}")
83
+ print(f"Hashtag:{tags}")
84
+ app = XiaoHongShuVideo(title, file, tags, publish_datetimes, cookie)
85
+ asyncio.run(app.main(), debug=False)
86
 
87
 
88
 
uploader/ks_uploader/main.py CHANGED
@@ -89,8 +89,6 @@ class KSVideo(object):
89
  ) # 创建一个浏览器上下文,使用指定的 cookie 文件
90
  context = await browser.new_context(storage_state=f"{self.account_file}")
91
  context = await set_init_script(context)
92
- context.on("close", lambda: context.storage_state(path=self.account_file))
93
-
94
  # 创建一个新的页面
95
  page = await context.new_page()
96
  # 访问指定的 URL
 
89
  ) # 创建一个浏览器上下文,使用指定的 cookie 文件
90
  context = await browser.new_context(storage_state=f"{self.account_file}")
91
  context = await set_init_script(context)
 
 
92
  # 创建一个新的页面
93
  page = await context.new_page()
94
  # 访问指定的 URL
uploader/tencent_uploader/main.py CHANGED
@@ -188,7 +188,7 @@ class TencentVideo(object):
188
  publish_buttion = page.locator('div.form-btns button:has-text("发表")')
189
  if await publish_buttion.count():
190
  await publish_buttion.click()
191
- await page.wait_for_url("https://channels.weixin.qq.com/platform/post/list", timeout=1500)
192
  tencent_logger.success(" [-]视频发布成功")
193
  break
194
  except Exception as e:
 
188
  publish_buttion = page.locator('div.form-btns button:has-text("发表")')
189
  if await publish_buttion.count():
190
  await publish_buttion.click()
191
+ await page.wait_for_url("https://channels.weixin.qq.com/platform/post/list", timeout=5000)
192
  tencent_logger.success(" [-]视频发布成功")
193
  break
194
  except Exception as e:
uploader/xiaohongshu_uploader/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ from conf import BASE_DIR
4
+
5
+ Path(BASE_DIR / "cookies" / "xiaohongshu_uploader").mkdir(exist_ok=True)
uploader/xiaohongshu_uploader/main.py ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ from datetime import datetime
3
+
4
+ from playwright.async_api import Playwright, async_playwright, Page
5
+ import os
6
+ import asyncio
7
+
8
+ from conf import LOCAL_CHROME_PATH
9
+ from utils.base_social_media import set_init_script
10
+ from utils.log import xiaohongshu_logger
11
+
12
+
13
+ async def cookie_auth(account_file):
14
+ async with async_playwright() as playwright:
15
+ browser = await playwright.chromium.launch(headless=True)
16
+ context = await browser.new_context(storage_state=account_file)
17
+ context = await set_init_script(context)
18
+ # 创建一个新的页面
19
+ page = await context.new_page()
20
+ # 访问指定的 URL
21
+ await page.goto("https://creator.xiaohongshu.com/creator-micro/content/upload")
22
+ try:
23
+ await page.wait_for_url("https://creator.xiaohongshu.com/creator-micro/content/upload", timeout=5000)
24
+ except:
25
+ print("[+] 等待5秒 cookie 失效")
26
+ await context.close()
27
+ await browser.close()
28
+ return False
29
+ # 2024.06.17 抖音创作者中心改版
30
+ if await page.get_by_text('手机号登录').count() or await page.get_by_text('扫码登录').count():
31
+ print("[+] 等待5秒 cookie 失效")
32
+ return False
33
+ else:
34
+ print("[+] cookie 有效")
35
+ return True
36
+
37
+
38
+ async def xiaohongshu_setup(account_file, handle=False):
39
+ if not os.path.exists(account_file) or not await cookie_auth(account_file):
40
+ if not handle:
41
+ # Todo alert message
42
+ return False
43
+ xiaohongshu_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件')
44
+ await xiaohongshu_cookie_gen(account_file)
45
+ return True
46
+
47
+
48
+ async def xiaohongshu_cookie_gen(account_file):
49
+ async with async_playwright() as playwright:
50
+ options = {
51
+ 'headless': False
52
+ }
53
+ # Make sure to run headed.
54
+ browser = await playwright.chromium.launch(**options)
55
+ # Setup context however you like.
56
+ context = await browser.new_context() # Pass any options
57
+ context = await set_init_script(context)
58
+ # Pause the page, and start recording manually.
59
+ page = await context.new_page()
60
+ await page.goto("https://creator.xiaohongshu.com/")
61
+ await page.pause()
62
+ # 点击调试器的继续,保存cookie
63
+ await context.storage_state(path=account_file)
64
+
65
+
66
+ class XiaoHongShuVideo(object):
67
+ def __init__(self, title, file_path, tags, publish_date: datetime, account_file, thumbnail_path=None):
68
+ self.title = title # 视频标题
69
+ self.file_path = file_path
70
+ self.tags = tags
71
+ self.publish_date = publish_date
72
+ self.account_file = account_file
73
+ self.date_format = '%Y年%m月%d日 %H:%M'
74
+ self.local_executable_path = LOCAL_CHROME_PATH
75
+ self.thumbnail_path = thumbnail_path
76
+
77
+ async def set_schedule_time_xiaohongshu(self, page, publish_date):
78
+ print(" [-] 正在设置定时发布时间...")
79
+ print(f"publish_date: {publish_date}")
80
+
81
+ # 使用文本内容定位元素
82
+ # element = await page.wait_for_selector(
83
+ # 'label:has-text("定时发布")',
84
+ # timeout=5000 # 5秒超时时间
85
+ # )
86
+ # await element.click()
87
+
88
+ # # 选择包含特定文本内容的 label 元素
89
+ label_element = page.locator("label:has-text('定时发布')")
90
+ # # 在选中的 label 元素下点击 checkbox
91
+ await label_element.click()
92
+ await asyncio.sleep(1)
93
+ publish_date_hour = publish_date.strftime("%Y-%m-%d %H:%M")
94
+ print(f"publish_date_hour: {publish_date_hour}")
95
+
96
+ await asyncio.sleep(1)
97
+ await page.locator('.el-input__inner[placeholder="选择日期和时间"]').click()
98
+ await page.keyboard.press("Control+KeyA")
99
+ await page.keyboard.type(str(publish_date_hour))
100
+ await page.keyboard.press("Enter")
101
+
102
+ await asyncio.sleep(1)
103
+
104
+ async def handle_upload_error(self, page):
105
+ xiaohongshu_logger.info('视频出错了,重新上传中')
106
+ await page.locator('div.progress-div [class^="upload-btn-input"]').set_input_files(self.file_path)
107
+
108
+ async def upload(self, playwright: Playwright) -> None:
109
+ # 使用 Chromium 浏览器启动一个浏览器实例
110
+ if self.local_executable_path:
111
+ browser = await playwright.chromium.launch(headless=False, executable_path=self.local_executable_path)
112
+ else:
113
+ browser = await playwright.chromium.launch(headless=False)
114
+ # 创建一个浏览器上下文,使用指定的 cookie 文件
115
+ context = await browser.new_context(
116
+ viewport={"width": 1600, "height": 900},
117
+ storage_state=f"{self.account_file}"
118
+ )
119
+ context = await set_init_script(context)
120
+
121
+ # 创建一个新的页面
122
+ page = await context.new_page()
123
+ # 访问指定的 URL
124
+ await page.goto("https://creator.xiaohongshu.com/publish/publish?from=homepage&target=video")
125
+ xiaohongshu_logger.info(f'[+]正在上传-------{self.title}.mp4')
126
+ # 等待页面跳转到指定的 URL,没进入,则自动等待到超时
127
+ xiaohongshu_logger.info(f'[-] 正在打开主页...')
128
+ await page.wait_for_url("https://creator.xiaohongshu.com/publish/publish?from=homepage&target=video")
129
+ # 点击 "上传视频" 按钮
130
+ await page.locator("div[class^='upload-content'] input[class='upload-input']").set_input_files(self.file_path)
131
+
132
+ # 等待页面跳转到指定的 URL 2025.01.08修改在原有基础上兼容两种页面
133
+ while True:
134
+ try:
135
+ # 等待upload-input元素出现
136
+ upload_input = await page.wait_for_selector('input.upload-input', timeout=3000)
137
+ # 获取下一个兄弟元素
138
+ preview_new = await upload_input.query_selector(
139
+ 'xpath=following-sibling::div[contains(@class, "preview-new")]')
140
+ if preview_new:
141
+ # 在preview-new元素中查找包含"上传成功"的stage元素
142
+ stage_elements = await preview_new.query_selector_all('div.stage')
143
+ upload_success = False
144
+ for stage in stage_elements:
145
+ text_content = await page.evaluate('(element) => element.textContent', stage)
146
+ if '上传成功' in text_content:
147
+ upload_success = True
148
+ break
149
+ if upload_success:
150
+ xiaohongshu_logger.info("[+] 检测到上传成功标识!")
151
+ break # 成功检测到上传成功后跳出循环
152
+ else:
153
+ print(" [-] 未找到上传成功标识,继续等待...")
154
+ else:
155
+ print(" [-] 未找到预览元素,继续等待...")
156
+ await asyncio.sleep(1)
157
+ except Exception as e:
158
+ print(f" [-] 检测过程出错: {str(e)},重新尝试...")
159
+ await asyncio.sleep(0.5) # 等待0.5秒后重新尝试
160
+
161
+ # 填充标题和话题
162
+ # 检查是否存在包含输入框的元素
163
+ # 这里为了避免页面变化,故使用相对位置定位:作品标题父级右侧第一个元素的input子元素
164
+ await asyncio.sleep(1)
165
+ xiaohongshu_logger.info(f' [-] 正在填充标题和话题...')
166
+ title_container = page.locator('div.input.titleInput').locator('input.d-text')
167
+ if await title_container.count():
168
+ await title_container.fill(self.title[:30])
169
+ else:
170
+ titlecontainer = page.locator(".notranslate")
171
+ await titlecontainer.click()
172
+ await page.keyboard.press("Backspace")
173
+ await page.keyboard.press("Control+KeyA")
174
+ await page.keyboard.press("Delete")
175
+ await page.keyboard.type(self.title)
176
+ await page.keyboard.press("Enter")
177
+ css_selector = ".ql-editor" # 不能加上 .ql-blank 属性,这样只能获取第一次非空状态
178
+ for index, tag in enumerate(self.tags, start=1):
179
+ await page.type(css_selector, "#" + tag)
180
+ await page.press(css_selector, "Space")
181
+ xiaohongshu_logger.info(f'总共添加{len(self.tags)}个话题')
182
+
183
+ # while True:
184
+ # # 判断重新上传按钮是否存在,如果不存在,代表视频正在上传,则等待
185
+ # try:
186
+ # # 新版:定位重新上传
187
+ # number = await page.locator('[class^="long-card"] div:has-text("重新上传")').count()
188
+ # if number > 0:
189
+ # xiaohongshu_logger.success(" [-]视频上传完毕")
190
+ # break
191
+ # else:
192
+ # xiaohongshu_logger.info(" [-] 正在上传视频中...")
193
+ # await asyncio.sleep(2)
194
+
195
+ # if await page.locator('div.progress-div > div:has-text("上传失败")').count():
196
+ # xiaohongshu_logger.error(" [-] 发现上传出错了... 准备重试")
197
+ # await self.handle_upload_error(page)
198
+ # except:
199
+ # xiaohongshu_logger.info(" [-] 正在上传视频中...")
200
+ # await asyncio.sleep(2)
201
+
202
+ # 上传视频封面
203
+ # await self.set_thumbnail(page, self.thumbnail_path)
204
+
205
+ # 更换可见元素
206
+ # await self.set_location(page, "青岛市")
207
+
208
+ # # 頭條/西瓜
209
+ # third_part_element = '[class^="info"] > [class^="first-part"] div div.semi-switch'
210
+ # # 定位是否有第三方平台
211
+ # if await page.locator(third_part_element).count():
212
+ # # 检测是否是已选中状态
213
+ # if 'semi-switch-checked' not in await page.eval_on_selector(third_part_element, 'div => div.className'):
214
+ # await page.locator(third_part_element).locator('input.semi-switch-native-control').click()
215
+
216
+ if self.publish_date != 0:
217
+ await self.set_schedule_time_xiaohongshu(page, self.publish_date)
218
+
219
+ # 判断视频是否发布成功
220
+ while True:
221
+ try:
222
+ # 等待包含"定时发布"文本的button元素出现并点击
223
+ if self.publish_date != 0:
224
+ await page.locator('button:has-text("定时发布")').click()
225
+ else:
226
+ await page.locator('button:has-text("发布")').click()
227
+ await page.wait_for_url(
228
+ "https://creator.xiaohongshu.com/publish/success?**",
229
+ timeout=3000
230
+ ) # 如果自动跳转到作品页面,则代表发布成功
231
+ xiaohongshu_logger.success(" [-]视频发布成功")
232
+ break
233
+ except:
234
+ xiaohongshu_logger.info(" [-] 视频正在发布中...")
235
+ await page.screenshot(full_page=True)
236
+ await asyncio.sleep(0.5)
237
+
238
+ await context.storage_state(path=self.account_file) # 保存cookie
239
+ xiaohongshu_logger.success(' [-]cookie更新完毕!')
240
+ await asyncio.sleep(2) # 这里延迟是为了方便眼睛直观的观看
241
+ # 关闭浏览器上下文和浏览器实例
242
+ await context.close()
243
+ await browser.close()
244
+
245
+ async def set_thumbnail(self, page: Page, thumbnail_path: str):
246
+ if thumbnail_path:
247
+ await page.click('text="选择封面"')
248
+ await page.wait_for_selector("div.semi-modal-content:visible")
249
+ await page.click('text="设置竖封面"')
250
+ await page.wait_for_timeout(2000) # 等待2秒
251
+ # 定位到上传区域并点击
252
+ await page.locator("div[class^='semi-upload upload'] >> input.semi-upload-hidden-input").set_input_files(thumbnail_path)
253
+ await page.wait_for_timeout(2000) # 等待2秒
254
+ await page.locator("div[class^='extractFooter'] button:visible:has-text('完成')").click()
255
+ # finish_confirm_element = page.locator("div[class^='confirmBtn'] >> div:has-text('完成')")
256
+ # if await finish_confirm_element.count():
257
+ # await finish_confirm_element.click()
258
+ # await page.locator("div[class^='footer'] button:has-text('完成')").click()
259
+
260
+ async def set_location(self, page: Page, location: str = "青岛市"):
261
+ print(f"开始设置位置: {location}")
262
+
263
+ # 点击地点输入框
264
+ print("等待地点输入框加载...")
265
+ loc_ele = await page.wait_for_selector('div.d-text.d-select-placeholder.d-text-ellipsis.d-text-nowrap')
266
+ print(f"已定位到地点输入框: {loc_ele}")
267
+ await loc_ele.click()
268
+ print("点击地点输入框完成")
269
+
270
+ # 输入位置名称
271
+ print(f"等待1秒后输入位置名称: {location}")
272
+ await page.wait_for_timeout(1000)
273
+ await page.keyboard.type(location)
274
+ print(f"位置名称输入完成: {location}")
275
+
276
+ # 等待下拉列表加载
277
+ print("等待下拉列表加载...")
278
+ dropdown_selector = 'div.d-popover.d-popover-default.d-dropdown.--size-min-width-large'
279
+ await page.wait_for_timeout(3000)
280
+ try:
281
+ await page.wait_for_selector(dropdown_selector, timeout=3000)
282
+ print("下拉列表已加载")
283
+ except:
284
+ print("下拉列表未按预期显示,可能结构已变化")
285
+
286
+ # 增加等待时间以确保内容加载完成
287
+ print("额外等待1秒确保内容渲染完成...")
288
+ await page.wait_for_timeout(1000)
289
+
290
+ # 尝试更灵活的XPath选择器
291
+ print("尝试使用更灵活的XPath选择器...")
292
+ flexible_xpath = (
293
+ f'//div[contains(@class, "d-popover") and contains(@class, "d-dropdown")]'
294
+ f'//div[contains(@class, "d-options-wrapper")]'
295
+ f'//div[contains(@class, "d-grid") and contains(@class, "d-options")]'
296
+ f'//div[contains(@class, "name") and text()="{location}"]'
297
+ )
298
+ await page.wait_for_timeout(3000)
299
+
300
+ # 尝试定位元素
301
+ print(f"尝试定位包含'{location}'的选项...")
302
+ try:
303
+ # 先尝试使用更灵活的选择器
304
+ location_option = await page.wait_for_selector(
305
+ flexible_xpath,
306
+ timeout=3000
307
+ )
308
+
309
+ if location_option:
310
+ print(f"使用灵活选择器定位成功: {location_option}")
311
+ else:
312
+ # 如果灵活选择器失败,再尝试原选择器
313
+ print("灵活选择器未找到元素,尝试原始选择器...")
314
+ location_option = await page.wait_for_selector(
315
+ f'//div[contains(@class, "d-popover") and contains(@class, "d-dropdown")]'
316
+ f'//div[contains(@class, "d-options-wrapper")]'
317
+ f'//div[contains(@class, "d-grid") and contains(@class, "d-options")]'
318
+ f'/div[1]//div[contains(@class, "name") and text()="{location}"]',
319
+ timeout=2000
320
+ )
321
+
322
+ # 滚动到元素并点击
323
+ print("滚动到目标选项...")
324
+ await location_option.scroll_into_view_if_needed()
325
+ print("元素已滚动到视图内")
326
+
327
+ # 增加元素可见性检查
328
+ is_visible = await location_option.is_visible()
329
+ print(f"目标选项是否可见: {is_visible}")
330
+
331
+ # 点击元素
332
+ print("准备点击目标选项...")
333
+ await location_option.click()
334
+ print(f"成功选择位置: {location}")
335
+ return True
336
+
337
+ except Exception as e:
338
+ print(f"定位位置失败: {e}")
339
+
340
+ # 打印更多调试信息
341
+ print("尝试获取下拉列表中的所有选项...")
342
+ try:
343
+ all_options = await page.query_selector_all(
344
+ '//div[contains(@class, "d-popover") and contains(@class, "d-dropdown")]'
345
+ '//div[contains(@class, "d-options-wrapper")]'
346
+ '//div[contains(@class, "d-grid") and contains(@class, "d-options")]'
347
+ '/div'
348
+ )
349
+ print(f"找到 {len(all_options)} 个选项")
350
+
351
+ # 打印前3个选项的文本内容
352
+ for i, option in enumerate(all_options[:3]):
353
+ option_text = await option.inner_text()
354
+ print(f"选项 {i+1}: {option_text.strip()[:50]}...")
355
+
356
+ except Exception as e:
357
+ print(f"获取选项列表失败: {e}")
358
+
359
+ # 截图保存(取消注释使用)
360
+ # await page.screenshot(path=f"location_error_{location}.png")
361
+ return False
362
+
363
+ async def main(self):
364
+ async with async_playwright() as playwright:
365
+ await self.upload(playwright)
366
+
367
+
utils/files_times.py CHANGED
@@ -38,7 +38,7 @@ def get_title_and_hashtags(filename):
38
  return title, hashtags
39
 
40
 
41
- def generate_schedule_time_next_day(total_videos, videos_per_day, daily_times=None, timestamps=False, start_days=0):
42
  """
43
  Generate a schedule for video uploads, starting from the next day.
44
 
 
38
  return title, hashtags
39
 
40
 
41
+ def generate_schedule_time_next_day(total_videos, videos_per_day = 1, daily_times=None, timestamps=False, start_days=0):
42
  """
43
  Generate a schedule for video uploads, starting from the next day.
44
 
utils/log.py CHANGED
@@ -50,3 +50,4 @@ tiktok_logger = create_logger('tiktok', 'logs/tiktok.log')
50
  bilibili_logger = create_logger('bilibili', 'logs/bilibili.log')
51
  kuaishou_logger = create_logger('kuaishou', 'logs/kuaishou.log')
52
  baijiahao_logger = create_logger('baijiahao', 'logs/baijiahao.log')
 
 
50
  bilibili_logger = create_logger('bilibili', 'logs/bilibili.log')
51
  kuaishou_logger = create_logger('kuaishou', 'logs/kuaishou.log')
52
  baijiahao_logger = create_logger('baijiahao', 'logs/baijiahao.log')
53
+ xiaohongshu_logger = create_logger('xiaohongshu', 'logs/xiaohongshu.log')