Merge branch 'master' of ssh://gitlab.rinal-li.cn:10022/liufuhua/xj-marketing
# Conflicts: # cli_main.py
Showing
9 changed files
with
416 additions
and
56 deletions
| ... | @@ -167,4 +167,7 @@ youtube_uploader/*.json | ... | @@ -167,4 +167,7 @@ youtube_uploader/*.json |
| 167 | douyin_uploader/*.json | 167 | douyin_uploader/*.json |
| 168 | bilibili_uploader/*.json | 168 | bilibili_uploader/*.json |
| 169 | tk_uploader/*.json | 169 | tk_uploader/*.json |
| 170 | -cookies | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 170 | +cookies | ||
| 171 | +bin | ||
| 172 | +get-pip.py | ||
| 173 | +pyvenv.cfg | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| ... | @@ -9,11 +9,13 @@ from uploader.douyin_uploader.main import douyin_setup, DouYinVideo | ... | @@ -9,11 +9,13 @@ from uploader.douyin_uploader.main import douyin_setup, DouYinVideo |
| 9 | from uploader.ks_uploader.main import ks_setup, KSVideo | 9 | from uploader.ks_uploader.main import ks_setup, KSVideo |
| 10 | from uploader.tencent_uploader.main import weixin_setup, TencentVideo | 10 | from uploader.tencent_uploader.main import weixin_setup, TencentVideo |
| 11 | from uploader.tk_uploader.main_chrome import tiktok_setup, TiktokVideo | 11 | from uploader.tk_uploader.main_chrome import tiktok_setup, TiktokVideo |
| 12 | +from uploader.xhs_uploader.main import xhs_setup, XHSVideo | ||
| 12 | from uploader.wyh_uploader.main import wyh_setup | 13 | from uploader.wyh_uploader.main import wyh_setup |
| 14 | +from uploader.sohu_uploader.main import sohu_setup, SOHUVideo | ||
| 13 | from uploader.toutiao_uploader.main import toutiao_setup | 15 | from uploader.toutiao_uploader.main import toutiao_setup |
| 14 | from utils.base_social_media import get_supported_social_media, get_cli_action, SOCIAL_MEDIA_DOUYIN, \ | 16 | from utils.base_social_media import get_supported_social_media, get_cli_action, SOCIAL_MEDIA_DOUYIN, \ |
| 15 | SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK, SOCIAL_MEDIA_KUAISHOU, SOCIAL_MEDIA_WANGYIHAO, \ | 17 | SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK, SOCIAL_MEDIA_KUAISHOU, SOCIAL_MEDIA_WANGYIHAO, \ |
| 16 | - SOCIAL_MEDIA_XHS, SOCIAL_MEDIA_TOUTIAO, SOCIAL_MEDIA_BAIJIAHAO | 18 | + SOCIAL_MEDIA_XHS, SOCIAL_MEDIA_TOUTIAO, SOCIAL_MEDIA_BAIJIAHAO, SOCIAL_MEDIA_SOHU |
| 17 | from utils.constant import TencentZoneTypes | 19 | from utils.constant import TencentZoneTypes |
| 18 | from utils.files_times import get_title_and_hashtags | 20 | from utils.files_times import get_title_and_hashtags |
| 19 | 21 | ||
| ... | @@ -72,12 +74,19 @@ async def main(): | ... | @@ -72,12 +74,19 @@ async def main(): |
| 72 | elif args.platform == SOCIAL_MEDIA_KUAISHOU: | 74 | elif args.platform == SOCIAL_MEDIA_KUAISHOU: |
| 73 | await ks_setup(str(account_file), handle=True) | 75 | await ks_setup(str(account_file), handle=True) |
| 74 | elif args.platform == SOCIAL_MEDIA_WANGYIHAO: | 76 | elif args.platform == SOCIAL_MEDIA_WANGYIHAO: |
| 77 | +<<<<<<< HEAD | ||
| 75 | await wyh_setup(str(account_file), handle=True, auto=False) | 78 | await wyh_setup(str(account_file), handle=True, auto=False) |
| 76 | # elif args.platform == SOCIAL_MEDIA_XHS: | 79 | # elif args.platform == SOCIAL_MEDIA_XHS: |
| 77 | # await xhs_setup(str(account_file), handle=True) | 80 | # await xhs_setup(str(account_file), handle=True) |
| 81 | +======= | ||
| 82 | + await wyh_setup(str(account_file), handle=True) | ||
| 83 | + elif args.platform == SOCIAL_MEDIA_XHS: | ||
| 84 | + await xhs_setup(str(account_file), handle=True) | ||
| 85 | +>>>>>>> 137c8c81e73faaa366d86e49e291228c805236a3 | ||
| 78 | elif args.platform == SOCIAL_MEDIA_TOUTIAO: | 86 | elif args.platform == SOCIAL_MEDIA_TOUTIAO: |
| 79 | await toutiao_setup(str(account_file), handle=True, auto=False) | 87 | await toutiao_setup(str(account_file), handle=True, auto=False) |
| 80 | elif args.platform == SOCIAL_MEDIA_BAIJIAHAO: | 88 | elif args.platform == SOCIAL_MEDIA_BAIJIAHAO: |
| 89 | +<<<<<<< HEAD | ||
| 81 | await toutiao_setup(str(account_file), handle=True, auto=False) | 90 | await toutiao_setup(str(account_file), handle=True, auto=False) |
| 82 | 91 | ||
| 83 | elif args.action == 'auto_login': | 92 | elif args.action == 'auto_login': |
| ... | @@ -98,6 +107,11 @@ async def main(): | ... | @@ -98,6 +107,11 @@ async def main(): |
| 98 | await toutiao_setup(str(account_file), handle=True, auto=True) | 107 | await toutiao_setup(str(account_file), handle=True, auto=True) |
| 99 | elif args.platform == SOCIAL_MEDIA_BAIJIAHAO: | 108 | elif args.platform == SOCIAL_MEDIA_BAIJIAHAO: |
| 100 | await toutiao_setup(str(account_file), handle=True, auto=True) | 109 | await toutiao_setup(str(account_file), handle=True, auto=True) |
| 110 | +======= | ||
| 111 | + await toutiao_setup(str(account_file), handle=True) | ||
| 112 | + elif args.platform == SOCIAL_MEDIA_SOHU: | ||
| 113 | + await sohu_setup(str(account_file), handle=True) | ||
| 114 | +>>>>>>> 137c8c81e73faaa366d86e49e291228c805236a3 | ||
| 101 | 115 | ||
| 102 | elif args.action == 'upload': | 116 | elif args.action == 'upload': |
| 103 | title, tags = get_title_and_hashtags(args.video_file) | 117 | title, tags = get_title_and_hashtags(args.video_file) |
| ... | @@ -123,6 +137,12 @@ async def main(): | ... | @@ -123,6 +137,12 @@ async def main(): |
| 123 | elif args.platform == SOCIAL_MEDIA_KUAISHOU: | 137 | elif args.platform == SOCIAL_MEDIA_KUAISHOU: |
| 124 | await ks_setup(account_file, handle=True) | 138 | await ks_setup(account_file, handle=True) |
| 125 | app = KSVideo(title, video_file, tags, publish_date, account_file) | 139 | app = KSVideo(title, video_file, tags, publish_date, account_file) |
| 140 | + elif args.platform == SOCIAL_MEDIA_XHS: | ||
| 141 | + await xhs_setup(account_file, handle=True) | ||
| 142 | + app = XHSVideo(title, video_file, tags, publish_date, account_file) | ||
| 143 | + elif args.platform == SOCIAL_MEDIA_SOHU: | ||
| 144 | + await sohu_setup(account_file, handle=True) | ||
| 145 | + app = SOHUVideo(title, video_file, tags, None, account_file) | ||
| 126 | else: | 146 | else: |
| 127 | print("Wrong platform, please check your input") | 147 | print("Wrong platform, please check your input") |
| 128 | exit() | 148 | exit() | ... | ... |
uploader/sohu_uploader/__init__.py
0 → 100644
File mode changed
uploader/sohu_uploader/main.py
0 → 100644
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +from datetime import datetime | ||
| 3 | + | ||
| 4 | +from playwright.async_api import Playwright, async_playwright | ||
| 5 | +import os | ||
| 6 | +import asyncio | ||
| 7 | +import json | ||
| 8 | +from conf import LOCAL_CHROME_PATH | ||
| 9 | +from utils.base_social_media import set_init_script | ||
| 10 | +from utils.files_times import get_absolute_path | ||
| 11 | +from utils.log import sohu_logger | ||
| 12 | + | ||
| 13 | + | ||
| 14 | +async def cookie_auth(account_file): | ||
| 15 | + async with async_playwright() as playwright: | ||
| 16 | + browser = await playwright.chromium.launch(headless=False) | ||
| 17 | + context = await browser.new_context(storage_state=account_file) | ||
| 18 | + await set_init_script(context) | ||
| 19 | + # context = await browser.new_context() | ||
| 20 | + page = await context.new_page() | ||
| 21 | + # saved_cookies = await read_cookies_from_file(account_file) | ||
| 22 | + # await context.add_cookies(saved_cookies) | ||
| 23 | + try: | ||
| 24 | + await page.goto("https://tv.sohu.com/", timeout=5000) | ||
| 25 | + except: | ||
| 26 | + pass | ||
| 27 | + try: | ||
| 28 | + await page.wait_for_selector("div.user-ico", timeout=5000) # 等待5秒 | ||
| 29 | + sohu_logger.success("[+] cookie 有效") | ||
| 30 | + return True | ||
| 31 | + except: | ||
| 32 | + sohu_logger.info("[+] 等待5秒 cookie 失效") | ||
| 33 | + return False | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +async def sohu_setup(account_file, handle=False): | ||
| 37 | + account_file = get_absolute_path(account_file, "sohu_uploader") | ||
| 38 | + if not os.path.exists(account_file) or not await cookie_auth(account_file): | ||
| 39 | + if not handle: | ||
| 40 | + return False | ||
| 41 | + sohu_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件') | ||
| 42 | + await get_sohu_cookie(account_file) | ||
| 43 | + return True | ||
| 44 | + | ||
| 45 | + | ||
| 46 | +async def get_sohu_cookie(account_file): | ||
| 47 | + async with async_playwright() as playwright: | ||
| 48 | + options = { | ||
| 49 | + 'args': [ | ||
| 50 | + '--lang en-GB' | ||
| 51 | + ], | ||
| 52 | + 'headless': False, # Set headless option here | ||
| 53 | + } | ||
| 54 | + # Make sure to run headed. | ||
| 55 | + browser = await playwright.chromium.launch(**options) | ||
| 56 | + # Setup context however you like. | ||
| 57 | + context = await browser.new_context() # Pass any options | ||
| 58 | + context = await set_init_script(context) | ||
| 59 | + # Pause the page, and start recording manually. | ||
| 60 | + page = await context.new_page() | ||
| 61 | + try: | ||
| 62 | + await page.goto("https://tv.sohu.com/", timeout=5000) | ||
| 63 | + except: | ||
| 64 | + pass | ||
| 65 | + await asyncio.sleep(1) | ||
| 66 | + await page.wait_for_selector("div.user-ico", timeout=50000) | ||
| 67 | + sohu_logger.info("搜狐视频登录成功") | ||
| 68 | + await asyncio.sleep(3) | ||
| 69 | + | ||
| 70 | + # await page.pause() | ||
| 71 | + # 点击调试器的继续,保存cookie | ||
| 72 | + # cookies = await context.cookies() | ||
| 73 | + # await write_cookies_to_file(cookies, account_file) | ||
| 74 | + await context.storage_state(path=account_file) | ||
| 75 | + | ||
| 76 | + | ||
| 77 | +class SOHUVideo(object): | ||
| 78 | + def __init__(self, title, file_path, tags, publish_date: datetime, account_file): | ||
| 79 | + self.title = title # 视频标题 | ||
| 80 | + self.file_path = file_path | ||
| 81 | + self.tags = tags | ||
| 82 | + self.publish_date = publish_date | ||
| 83 | + self.account_file = account_file | ||
| 84 | + self.date_format = '%Y-%m-%d %H:%M' | ||
| 85 | + self.local_executable_path = LOCAL_CHROME_PATH | ||
| 86 | + | ||
| 87 | + async def handle_upload_error(self, page): | ||
| 88 | + pass | ||
| 89 | + # sohu_logger.error("视频出错了,重新上传中") | ||
| 90 | + # await page.locator('div.progress-div [class^="upload-btn-input"]').set_input_files(self.file_path) | ||
| 91 | + | ||
| 92 | + async def upload(self, playwright: Playwright) -> None: | ||
| 93 | + # 使用 Chromium 浏览器启动一个浏览器实例 | ||
| 94 | + if self.local_executable_path: | ||
| 95 | + browser = await playwright.chromium.launch( | ||
| 96 | + headless=False, | ||
| 97 | + executable_path=self.local_executable_path, | ||
| 98 | + ) | ||
| 99 | + else: | ||
| 100 | + browser = await playwright.chromium.launch( | ||
| 101 | + headless=False | ||
| 102 | + ) # 创建一个浏览器上下文,使用指定的 cookie 文件 | ||
| 103 | + context = await browser.new_context(storage_state=self.account_file) | ||
| 104 | + page = await context.new_page() | ||
| 105 | + # saved_cookies = await read_cookies_from_file(self.account_file) | ||
| 106 | + # await context.add_cookies(saved_cookies) | ||
| 107 | + # context = await set_init_script(context) | ||
| 108 | + # context.on("close", lambda: ) | ||
| 109 | + | ||
| 110 | + # 创建一个新的页面 | ||
| 111 | + # 访问指定的 URL | ||
| 112 | + try: | ||
| 113 | + await page.goto("https://tv.sohu.com/s/center/index.html#/", timeout=5000) | ||
| 114 | + except: | ||
| 115 | + pass | ||
| 116 | + sohu_logger.info('正在上传-------{}.mp4'.format(self.title)) | ||
| 117 | + # 等待页面跳转到指定的 URL,没进入,则自动等待到超时 | ||
| 118 | + # 点击 "上传视频" 按钮 | ||
| 119 | + | ||
| 120 | + # await page.locator("dev.release-top").set_input_files( | ||
| 121 | + # self.file_path) | ||
| 122 | + await page.locator("input[type='file']").set_input_files( | ||
| 123 | + self.file_path) | ||
| 124 | + | ||
| 125 | + upload_button = page.locator('div.form-button span:has-text("发布")') | ||
| 126 | + await upload_button.wait_for(state='visible') # 确保按钮可见 | ||
| 127 | + await asyncio.sleep(1) | ||
| 128 | + | ||
| 129 | + sohu_logger.info("正在填充标题...") | ||
| 130 | + await page.locator("input.input-topic").nth(0).fill(self.title[:99]) # 搜狐最多100 | ||
| 131 | + await asyncio.sleep(1) | ||
| 132 | + sohu_logger.info("正在填充话题...") | ||
| 133 | + inputTag = page.locator("input.input-topic").nth(1) | ||
| 134 | + await inputTag.click() | ||
| 135 | + for index, tag in enumerate(self.tags[:2], start=1): | ||
| 136 | + await page.keyboard.type(f"#{tag}") | ||
| 137 | + await page.keyboard.press("Enter") | ||
| 138 | + | ||
| 139 | + | ||
| 140 | + sohu_logger.info("已写完tag") | ||
| 141 | + await asyncio.sleep(1) | ||
| 142 | + | ||
| 143 | + sohu_logger.info(self.publish_date) | ||
| 144 | + # 定时任务 搜狐没有定时发布 | ||
| 145 | + # if self.publish_date != 0: | ||
| 146 | + # await self.set_schedule_time(page, self.publish_date) | ||
| 147 | + # upload_button = page.locator('button div span:has-text("定时发布")') | ||
| 148 | + sohu_logger.info("提交") | ||
| 149 | + await upload_button.click() | ||
| 150 | + await page.wait_for_selector('h3:has-text("视频管理")') | ||
| 151 | + await context.storage_state(path=self.account_file) | ||
| 152 | + sohu_logger.info('cookie更新完毕!') | ||
| 153 | + await asyncio.sleep(4) # 这里延迟是为了方便眼睛直观的观看 | ||
| 154 | + # 关闭浏览器上下文和浏览器实例 | ||
| 155 | + await context.close() | ||
| 156 | + await browser.close() | ||
| 157 | + | ||
| 158 | + async def main(self): | ||
| 159 | + async with async_playwright() as playwright: | ||
| 160 | + await self.upload(playwright) | ||
| 161 | + | ||
| 162 | + |
| 1 | -import configparser | 1 | +# -*- coding: utf-8 -*- |
| 2 | +from datetime import datetime | ||
| 3 | + | ||
| 4 | +from playwright.async_api import Playwright, async_playwright | ||
| 5 | +import os | ||
| 6 | +import asyncio | ||
| 2 | import json | 7 | import json |
| 3 | -import pathlib | 8 | +from conf import LOCAL_CHROME_PATH |
| 4 | -from time import sleep | 9 | +from utils.base_social_media import set_init_script |
| 10 | +from utils.files_times import get_absolute_path | ||
| 11 | +from utils.log import xhs_logger | ||
| 12 | + | ||
| 5 | 13 | ||
| 6 | -import requests | 14 | +async def write_cookies_to_file(cookies, file_path): |
| 7 | -from playwright.sync_api import sync_playwright | ||
| 8 | 15 | ||
| 9 | -from conf import BASE_DIR, XHS_SERVER | 16 | + with open(file_path, 'w') as f: |
| 17 | + json.dump(cookies, f) | ||
| 10 | 18 | ||
| 11 | -config = configparser.RawConfigParser() | ||
| 12 | -config.read('accounts.ini') | ||
| 13 | 19 | ||
| 20 | +async def read_cookies_from_file(file_path): | ||
| 21 | + try: | ||
| 22 | + with open(file_path, 'r') as f: | ||
| 23 | + return json.load(f) | ||
| 24 | + except FileNotFoundError: | ||
| 25 | + return [] | ||
| 14 | 26 | ||
| 15 | -def sign_local(uri, data=None, a1="", web_session=""): | 27 | +async def cookie_auth(account_file): |
| 16 | - for _ in range(10): | 28 | + async with async_playwright() as playwright: |
| 29 | + browser = await playwright.chromium.launch(headless=True) | ||
| 30 | + # context = await browser.new_context(storage_state=account_file) | ||
| 31 | + context = await browser.new_context() | ||
| 32 | + page = await context.new_page() | ||
| 33 | + saved_cookies = await read_cookies_from_file(account_file) | ||
| 34 | + await context.add_cookies(saved_cookies) | ||
| 35 | + await page.goto("https://www.xiaohongshu.com") | ||
| 17 | try: | 36 | try: |
| 18 | - with sync_playwright() as playwright: | 37 | + await page.wait_for_selector("div.link-wrapper a.link-wrapper span.channel", timeout=5000) # 等待5秒 |
| 19 | - stealth_js_path = pathlib.Path(BASE_DIR / "utils/stealth.min.js") | 38 | + xhs_logger.success("[+] cookie 有效") |
| 20 | - chromium = playwright.chromium | 39 | + return True |
| 21 | - | 40 | + except: |
| 22 | - # 如果一直失败可尝试设置成 False 让其打开浏览器,适当添加 sleep 可查看浏览器状态 | 41 | + xhs_logger.info("[+] 等待5秒 cookie 失效") |
| 23 | - browser = chromium.launch(headless=True) | 42 | + return False |
| 24 | - | 43 | + |
| 25 | - browser_context = browser.new_context() | 44 | + |
| 26 | - browser_context.add_init_script(path=stealth_js_path) | 45 | +async def xhs_setup(account_file, handle=False): |
| 27 | - context_page = browser_context.new_page() | 46 | + account_file = get_absolute_path(account_file, "xhs_uploader") |
| 28 | - context_page.goto("https://www.xiaohongshu.com") | 47 | + if not os.path.exists(account_file) or not await cookie_auth(account_file): |
| 29 | - browser_context.add_cookies([ | 48 | + if not handle: |
| 30 | - {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}] | 49 | + return False |
| 31 | - ) | 50 | + xhs_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件') |
| 32 | - context_page.reload() | 51 | + await get_xhs_cookie(account_file) |
| 33 | - # 这个地方设置完浏览器 cookie 之后,如果这儿不 sleep 一下签名获取就失败了,如果经常失败请设置长一点试试 | 52 | + return True |
| 34 | - sleep(2) | 53 | + |
| 35 | - encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data]) | 54 | + |
| 36 | - return { | 55 | +async def get_xhs_cookie(account_file): |
| 37 | - "x-s": encrypt_params["X-s"], | 56 | + async with async_playwright() as playwright: |
| 38 | - "x-t": str(encrypt_params["X-t"]) | 57 | + options = { |
| 39 | - } | 58 | + 'args': [ |
| 40 | - except Exception: | 59 | + '--lang en-GB' |
| 41 | - # 这儿有时会出现 window._webmsxyw is not a function 或未知跳转错误,因此加一个失败重试趴 | 60 | + ], |
| 42 | - pass | 61 | + 'headless': False, # Set headless option here |
| 43 | - raise Exception("重试了这么多次还是无法签名成功,寄寄寄") | 62 | + } |
| 44 | - | 63 | + # Make sure to run headed. |
| 45 | - | 64 | + browser = await playwright.chromium.launch(**options) |
| 46 | -def sign(uri, data=None, a1="", web_session=""): | 65 | + # Setup context however you like. |
| 47 | - # 填写自己的 flask 签名服务端口地址 | 66 | + context = await browser.new_context() # Pass any options |
| 48 | - res = requests.post(f"{XHS_SERVER}/sign", | 67 | + context = await set_init_script(context) |
| 49 | - json={"uri": uri, "data": data, "a1": a1, "web_session": web_session}) | 68 | + # Pause the page, and start recording manually. |
| 50 | - signs = res.json() | 69 | + page = await context.new_page() |
| 51 | - return { | 70 | + await page.goto("https://www.xiaohongshu.com") |
| 52 | - "x-s": signs["x-s"], | 71 | + await asyncio.sleep(1) |
| 53 | - "x-t": signs["x-t"] | 72 | + await page.wait_for_selector("div.link-wrapper a.link-wrapper span.channel", timeout=50000) |
| 54 | - } | 73 | + xhs_logger.info("小红书登录成功") |
| 55 | - | 74 | + await asyncio.sleep(3) |
| 56 | - | 75 | + |
| 57 | -def beauty_print(data: dict): | 76 | + # await page.pause() |
| 58 | - print(json.dumps(data, ensure_ascii=False, indent=2)) | 77 | + # 点击调试器的继续,保存cookie |
| 78 | + cookies = await context.cookies() | ||
| 79 | + await write_cookies_to_file(cookies, account_file) | ||
| 80 | + | ||
| 81 | + | ||
| 82 | +class XHSVideo(object): | ||
| 83 | + def __init__(self, title, file_path, tags, publish_date: datetime, account_file): | ||
| 84 | + self.title = title # 视频标题 | ||
| 85 | + self.file_path = file_path | ||
| 86 | + self.tags = tags | ||
| 87 | + self.publish_date = publish_date | ||
| 88 | + self.account_file = account_file | ||
| 89 | + self.date_format = '%Y-%m-%d %H:%M' | ||
| 90 | + self.local_executable_path = LOCAL_CHROME_PATH | ||
| 91 | + | ||
| 92 | + async def handle_upload_error(self, page): | ||
| 93 | + pass | ||
| 94 | + | ||
| 95 | + async def upload(self, playwright: Playwright) -> None: | ||
| 96 | + # 使用 Chromium 浏览器启动一个浏览器实例 | ||
| 97 | + if self.local_executable_path: | ||
| 98 | + browser = await playwright.chromium.launch( | ||
| 99 | + headless=False, | ||
| 100 | + executable_path=self.local_executable_path, | ||
| 101 | + ) | ||
| 102 | + else: | ||
| 103 | + browser = await playwright.chromium.launch( | ||
| 104 | + headless=True | ||
| 105 | + ) # 创建一个浏览器上下文,使用指定的 cookie 文件 | ||
| 106 | + context = await browser.new_context() | ||
| 107 | + page = await context.new_page() | ||
| 108 | + saved_cookies = await read_cookies_from_file(self.account_file) | ||
| 109 | + await context.add_cookies(saved_cookies) | ||
| 110 | + # context = await set_init_script(context) | ||
| 111 | + # context.on("close", lambda: ) | ||
| 112 | + | ||
| 113 | + # 创建一个新的页面 | ||
| 114 | + # 访问指定的 URL | ||
| 115 | + await page.goto("https://creator.xiaohongshu.com/publish/publish?source=official") | ||
| 116 | + xhs_logger.info('正在上传-------{}.mp4'.format(self.title)) | ||
| 117 | + # 等待页面跳转到指定的 URL,没进入,则自动等待到超时 | ||
| 118 | + # 点击 "上传视频" 按钮 | ||
| 119 | + | ||
| 120 | + await page.locator("input.upload-input").set_input_files( | ||
| 121 | + self.file_path) | ||
| 122 | + | ||
| 123 | + upload_button = page.locator('button div span:has-text("发布")') | ||
| 124 | + await upload_button.wait_for(state='visible') # 确保按钮可见 | ||
| 125 | + | ||
| 126 | + | ||
| 127 | + xhs_logger.info("正在填充标题和话题...") | ||
| 128 | + await page.locator("div.titleInput div div input").fill(self.title[:19]) # 小红书最长20 | ||
| 129 | + | ||
| 130 | + inputTag = page.locator('id=quillEditor').locator('div p') | ||
| 131 | + await inputTag.click() | ||
| 132 | + for index, tag in enumerate(self.tags, start=1): | ||
| 133 | + await page.keyboard.type(f"#{tag}") | ||
| 134 | + await page.wait_for_selector("ul.ql-mention-list") | ||
| 135 | + await page.keyboard.press("Enter") | ||
| 136 | + | ||
| 137 | + | ||
| 138 | + xhs_logger.info("已写完tag") | ||
| 139 | + await asyncio.sleep(1) | ||
| 140 | + | ||
| 141 | + xhs_logger.info(self.publish_date) | ||
| 142 | + # 定时任务 | ||
| 143 | + if self.publish_date != 0: | ||
| 144 | + await self.set_schedule_time(page, self.publish_date) | ||
| 145 | + upload_button = page.locator('button div span:has-text("定时发布")') | ||
| 146 | + xhs_logger.info("提交") | ||
| 147 | + await upload_button.click() | ||
| 148 | + await page.wait_for_selector('p:has-text("发布成功")') | ||
| 149 | + await write_cookies_to_file(await context.cookies(), self.account_file) | ||
| 150 | + xhs_logger.info('cookie更新完毕!') | ||
| 151 | + await asyncio.sleep(2) # 这里延迟是为了方便眼睛直观的观看 | ||
| 152 | + # 关闭浏览器上下文和浏览器实例 | ||
| 153 | + await context.close() | ||
| 154 | + await browser.close() | ||
| 155 | + | ||
| 156 | + async def main(self): | ||
| 157 | + async with async_playwright() as playwright: | ||
| 158 | + await self.upload(playwright) | ||
| 159 | + | ||
| 160 | + async def set_schedule_time(self, page, publish_date): | ||
| 161 | + xhs_logger.info("click schedule") | ||
| 162 | + publish_date_hour = publish_date.strftime("%Y-%m-%d %H:%M:%S") | ||
| 163 | + await page.locator("span.el-radio__input").nth(1).click() | ||
| 164 | + await asyncio.sleep(1) | ||
| 165 | + | ||
| 166 | + await page.locator('div.date-picker div div input').click() | ||
| 167 | + await asyncio.sleep(1) | ||
| 168 | + | ||
| 169 | + await page.keyboard.press("Control+KeyA") | ||
| 170 | + await page.keyboard.type(str(publish_date_hour)) | ||
| 171 | + await page.keyboard.press("Enter") | ||
| 172 | + await asyncio.sleep(1) | ... | ... |
uploader/xhs_uploader/main_old.py
0 → 100644
| 1 | +import configparser | ||
| 2 | +import json | ||
| 3 | +import pathlib | ||
| 4 | +from time import sleep | ||
| 5 | + | ||
| 6 | +import requests | ||
| 7 | +from playwright.sync_api import sync_playwright | ||
| 8 | + | ||
| 9 | +from conf import BASE_DIR, XHS_SERVER | ||
| 10 | + | ||
| 11 | +config = configparser.RawConfigParser() | ||
| 12 | +config.read('accounts.ini') | ||
| 13 | + | ||
| 14 | + | ||
| 15 | +def sign_local(uri, data=None, a1="", web_session=""): | ||
| 16 | + for _ in range(10): | ||
| 17 | + try: | ||
| 18 | + with sync_playwright() as playwright: | ||
| 19 | + stealth_js_path = pathlib.Path(BASE_DIR / "utils/stealth.min.js") | ||
| 20 | + chromium = playwright.chromium | ||
| 21 | + | ||
| 22 | + # 如果一直失败可尝试设置成 False 让其打开浏览器,适当添加 sleep 可查看浏览器状态 | ||
| 23 | + browser = chromium.launch(headless=True) | ||
| 24 | + | ||
| 25 | + browser_context = browser.new_context() | ||
| 26 | + browser_context.add_init_script(path=stealth_js_path) | ||
| 27 | + context_page = browser_context.new_page() | ||
| 28 | + context_page.goto("https://www.xiaohongshu.com") | ||
| 29 | + browser_context.add_cookies([ | ||
| 30 | + {'name': 'a1', 'value': a1, 'domain': ".xiaohongshu.com", 'path': "/"}] | ||
| 31 | + ) | ||
| 32 | + context_page.reload() | ||
| 33 | + # 这个地方设置完浏览器 cookie 之后,如果这儿不 sleep 一下签名获取就失败了,如果经常失败请设置长一点试试 | ||
| 34 | + sleep(2) | ||
| 35 | + encrypt_params = context_page.evaluate("([url, data]) => window._webmsxyw(url, data)", [uri, data]) | ||
| 36 | + return { | ||
| 37 | + "x-s": encrypt_params["X-s"], | ||
| 38 | + "x-t": str(encrypt_params["X-t"]) | ||
| 39 | + } | ||
| 40 | + except Exception: | ||
| 41 | + # 这儿有时会出现 window._webmsxyw is not a function 或未知跳转错误,因此加一个失败重试趴 | ||
| 42 | + pass | ||
| 43 | + raise Exception("重试了这么多次还是无法签名成功,寄寄寄") | ||
| 44 | + | ||
| 45 | + | ||
| 46 | +def sign(uri, data=None, a1="", web_session=""): | ||
| 47 | + # 填写自己的 flask 签名服务端口地址 | ||
| 48 | + res = requests.post(f"{XHS_SERVER}/sign", | ||
| 49 | + json={"uri": uri, "data": data, "a1": a1, "web_session": web_session}) | ||
| 50 | + signs = res.json() | ||
| 51 | + return { | ||
| 52 | + "x-s": signs["x-s"], | ||
| 53 | + "x-t": signs["x-t"] | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + | ||
| 57 | +def beauty_print(data: dict): | ||
| 58 | + print(json.dumps(data, ensure_ascii=False, indent=2)) |
| ... | @@ -5,7 +5,7 @@ from time import sleep | ... | @@ -5,7 +5,7 @@ from time import sleep |
| 5 | 5 | ||
| 6 | from xhs import XhsClient | 6 | from xhs import XhsClient |
| 7 | 7 | ||
| 8 | -from uploader.xhs_uploader.main import sign | 8 | +from uploader.xhs_uploader.main_old import sign |
| 9 | 9 | ||
| 10 | # pip install qrcode | 10 | # pip install qrcode |
| 11 | if __name__ == '__main__': | 11 | if __name__ == '__main__': | ... | ... |
| ... | @@ -9,14 +9,16 @@ SOCIAL_MEDIA_TIKTOK = "tiktok" | ... | @@ -9,14 +9,16 @@ SOCIAL_MEDIA_TIKTOK = "tiktok" |
| 9 | SOCIAL_MEDIA_BILIBILI = "bilibili" | 9 | SOCIAL_MEDIA_BILIBILI = "bilibili" |
| 10 | SOCIAL_MEDIA_KUAISHOU = "kuaishou" | 10 | SOCIAL_MEDIA_KUAISHOU = "kuaishou" |
| 11 | SOCIAL_MEDIA_WANGYIHAO = 'wangyihao' | 11 | SOCIAL_MEDIA_WANGYIHAO = 'wangyihao' |
| 12 | -SOCIAL_MEDIA_XHS = 'xiaohongshu' | 12 | +SOCIAL_MEDIA_XHS = 'xiaohongshu' # 小红书最长标题是20 |
| 13 | SOCIAL_MEDIA_TOUTIAO = 'toutiao' | 13 | SOCIAL_MEDIA_TOUTIAO = 'toutiao' |
| 14 | SOCIAL_MEDIA_BAIJIAHAO = 'baijiahao' | 14 | SOCIAL_MEDIA_BAIJIAHAO = 'baijiahao' |
| 15 | +SOCIAL_MEDIA_SOHU = 'sohu' # 搜狐视频话题最多2个 标题最多100 标题不支持🩹 | ||
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | def get_supported_social_media() -> List[str]: | 18 | def get_supported_social_media() -> List[str]: |
| 18 | return [SOCIAL_MEDIA_DOUYIN, SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK, SOCIAL_MEDIA_KUAISHOU, | 19 | return [SOCIAL_MEDIA_DOUYIN, SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK, SOCIAL_MEDIA_KUAISHOU, |
| 19 | - SOCIAL_MEDIA_WANGYIHAO, SOCIAL_MEDIA_TOUTIAO, SOCIAL_MEDIA_BAIJIAHAO] | 20 | + SOCIAL_MEDIA_WANGYIHAO, SOCIAL_MEDIA_TOUTIAO, SOCIAL_MEDIA_BAIJIAHAO, SOCIAL_MEDIA_XHS, |
| 21 | + SOCIAL_MEDIA_SOHU] | ||
| 20 | 22 | ||
| 21 | 23 | ||
| 22 | def get_cli_action() -> List[str]: | 24 | def get_cli_action() -> List[str]: | ... | ... |
| ... | @@ -46,6 +46,7 @@ logger.add(stdout, colorize=True, format=log_formatter) | ... | @@ -46,6 +46,7 @@ logger.add(stdout, colorize=True, format=log_formatter) |
| 46 | douyin_logger = create_logger('douyin', 'logs/douyin.log') | 46 | douyin_logger = create_logger('douyin', 'logs/douyin.log') |
| 47 | tencent_logger = create_logger('tencent', 'logs/tencent.log') | 47 | tencent_logger = create_logger('tencent', 'logs/tencent.log') |
| 48 | xhs_logger = create_logger('xhs', 'logs/xhs.log') | 48 | xhs_logger = create_logger('xhs', 'logs/xhs.log') |
| 49 | +sohu_logger = create_logger('sohu', 'logs/sohu.log') | ||
| 49 | tiktok_logger = create_logger('tiktok', 'logs/tiktok.log') | 50 | tiktok_logger = create_logger('tiktok', 'logs/tiktok.log') |
| 50 | bilibili_logger = create_logger('bilibili', 'logs/bilibili.log') | 51 | bilibili_logger = create_logger('bilibili', 'logs/bilibili.log') |
| 51 | kuaishou_logger = create_logger('kuaishou', 'logs/kuaishou.log') | 52 | kuaishou_logger = create_logger('kuaishou', 'logs/kuaishou.log') | ... | ... |
-
Please register or login to post a comment