diff --git a/README.MD b/README.MD index 0ed1965c..35d561a8 100644 --- a/README.MD +++ b/README.MD @@ -182,7 +182,7 @@ generate_schedule_time_next_day 默认从第二天开始(此举为避免选择 ![Alt text](media/20231009111131.png) - 导出 ![Alt text](media/20231009111214.png) -3. 黏贴至 accounts.ini文件中 +3. 黏贴至 uploader/xhs_uploader/accounts.ini文件中 #### 解释与注意事项: diff --git a/cli_main.py b/cli_main.py index e8da41e9..67925930 100644 --- a/cli_main.py +++ b/cli_main.py @@ -5,9 +5,9 @@ from pathlib import Path from conf import BASE_DIR -from douyin_uploader.main import douyin_setup, DouYinVideo -from tencent_uploader.main import weixin_setup, TencentVideo -from tk_uploader.main_chrome import tiktok_setup, TiktokVideo +from uploader.douyin_uploader.main import douyin_setup, DouYinVideo +from uploader.tencent_uploader.main import weixin_setup, TencentVideo +from uploader.tk_uploader.main_chrome import tiktok_setup, TiktokVideo from utils.base_social_media import get_supported_social_media, get_cli_action, SOCIAL_MEDIA_DOUYIN, \ SOCIAL_MEDIA_TENCENT, SOCIAL_MEDIA_TIKTOK from utils.constant import TencentZoneTypes diff --git a/douyin_uploader/__init__.py b/douyin_uploader/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/get_bilibili_cookie.py b/examples/get_bilibili_cookie.py index 8acfc370..fb241f0c 100644 --- a/examples/get_bilibili_cookie.py +++ b/examples/get_bilibili_cookie.py @@ -1,2 +1,2 @@ -# cd bilibili_uploader +# cd uploader/bilibili_uploader # biliup.exe -u account.json login diff --git a/examples/get_douyin_cookie.py b/examples/get_douyin_cookie.py index 861fbc4b..aa181d99 100644 --- a/examples/get_douyin_cookie.py +++ b/examples/get_douyin_cookie.py @@ -2,8 +2,8 @@ from pathlib import Path from conf import BASE_DIR -from douyin_uploader.main import douyin_setup +from uploader.douyin_uploader.main import douyin_setup if __name__ == '__main__': - account_file = Path(BASE_DIR / "douyin_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "douyin_uploader" / "account.json") cookie_setup = asyncio.run(douyin_setup(str(account_file), handle=True)) diff --git a/examples/get_kuaishou_cookie.py b/examples/get_kuaishou_cookie.py new file mode 100644 index 00000000..17740ed2 --- /dev/null +++ b/examples/get_kuaishou_cookie.py @@ -0,0 +1,9 @@ +import asyncio +from pathlib import Path + +from conf import BASE_DIR +from uploader.ks_uploader.main import ks_setup + +if __name__ == '__main__': + account_file = Path(BASE_DIR / "cookies" / "ks_uploader" / "account.json") + cookie_setup = asyncio.run(ks_setup(str(account_file), handle=True)) diff --git a/examples/get_tencent_cookie.py b/examples/get_tencent_cookie.py index 20f33e59..178dddbd 100644 --- a/examples/get_tencent_cookie.py +++ b/examples/get_tencent_cookie.py @@ -2,8 +2,8 @@ from pathlib import Path from conf import BASE_DIR -from tencent_uploader.main import weixin_setup +from uploader.tencent_uploader.main import weixin_setup if __name__ == '__main__': - account_file = Path(BASE_DIR / "tencent_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "tencent_uploader" / "account.json") cookie_setup = asyncio.run(weixin_setup(str(account_file), handle=True)) diff --git a/examples/get_tk_cookie.py b/examples/get_tk_cookie.py index 3ce008d7..2b2ce315 100644 --- a/examples/get_tk_cookie.py +++ b/examples/get_tk_cookie.py @@ -2,8 +2,8 @@ from pathlib import Path from conf import BASE_DIR -from tk_uploader.main_chrome import tiktok_setup +from uploader.tk_uploader.main_chrome import tiktok_setup if __name__ == '__main__': - account_file = Path(BASE_DIR / "tk_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "tk_uploader" / "account.json") cookie_setup = asyncio.run(tiktok_setup(str(account_file), handle=True)) diff --git a/examples/upload_video_to_bilibili.py b/examples/upload_video_to_bilibili.py index bfcc7a47..4beea23d 100644 --- a/examples/upload_video_to_bilibili.py +++ b/examples/upload_video_to_bilibili.py @@ -1,7 +1,7 @@ import time from pathlib import Path -from bilibili_uploader.main import read_cookie_json_file, extract_keys_from_json, random_emoji, BilibiliUploader +from uploader.bilibili_uploader.main import read_cookie_json_file, extract_keys_from_json, random_emoji, BilibiliUploader from conf import BASE_DIR from utils.constant import VideoZoneTypes from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags @@ -9,7 +9,7 @@ if __name__ == '__main__': filepath = Path(BASE_DIR) / "videos" # how to get cookie, see the file of get_bilibili_cookie.py. - account_file = Path(BASE_DIR / "bilibili_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "bilibili_uploader" / "account.json") if not account_file.exists(): print(f"{account_file.name} 配置文件不存在") exit() diff --git a/examples/upload_video_to_douyin.py b/examples/upload_video_to_douyin.py index ac9e1233..08023a53 100644 --- a/examples/upload_video_to_douyin.py +++ b/examples/upload_video_to_douyin.py @@ -2,13 +2,13 @@ from pathlib import Path from conf import BASE_DIR -from douyin_uploader.main import douyin_setup, DouYinVideo +from uploader.douyin_uploader.main import douyin_setup, DouYinVideo from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags if __name__ == '__main__': filepath = Path(BASE_DIR) / "videos" - account_file = Path(BASE_DIR / "douyin_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "douyin_uploader" / "account.json") # 获取视频目录 folder_path = Path(filepath) # 获取文件夹中的所有文件 diff --git a/examples/upload_video_to_kuaishou.py b/examples/upload_video_to_kuaishou.py new file mode 100644 index 00000000..8211bca1 --- /dev/null +++ b/examples/upload_video_to_kuaishou.py @@ -0,0 +1,26 @@ +import asyncio +from pathlib import Path + +from conf import BASE_DIR +from uploader.ks_uploader.main import ks_setup, KSVideo +from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags + + +if __name__ == '__main__': + filepath = Path(BASE_DIR) / "videos" + account_file = Path(BASE_DIR / "cookies" / "ks_uploader" / "account.json") + # 获取视频目录 + folder_path = Path(filepath) + # 获取文件夹中的所有文件 + files = list(folder_path.glob("*.mp4")) + file_num = len(files) + publish_datetimes = generate_schedule_time_next_day(file_num, 1, daily_times=[16]) + cookie_setup = asyncio.run(ks_setup(account_file, handle=False)) + for index, file in enumerate(files): + title, tags = get_title_and_hashtags(str(file)) + # 打印视频文件名、标题和 hashtag + print(f"视频文件名:{file}") + print(f"标题:{title}") + print(f"Hashtag:{tags}") + app = KSVideo(title, file, tags, publish_datetimes[index], account_file) + asyncio.run(app.main(), debug=False) diff --git a/examples/upload_video_to_tencent.py b/examples/upload_video_to_tencent.py index b80b15c7..2e47a1ef 100644 --- a/examples/upload_video_to_tencent.py +++ b/examples/upload_video_to_tencent.py @@ -2,14 +2,14 @@ from pathlib import Path from conf import BASE_DIR -from tencent_uploader.main import weixin_setup, TencentVideo +from uploader.tencent_uploader.main import weixin_setup, TencentVideo from utils.constant import TencentZoneTypes from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags if __name__ == '__main__': filepath = Path(BASE_DIR) / "videos" - account_file = Path(BASE_DIR / "tencent_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "tencent_uploader" / "account.json") # 获取视频目录 folder_path = Path(filepath) # 获取文件夹中的所有文件 diff --git a/examples/upload_video_to_tiktok.py b/examples/upload_video_to_tiktok.py index 740c9279..efa94cc0 100644 --- a/examples/upload_video_to_tiktok.py +++ b/examples/upload_video_to_tiktok.py @@ -3,13 +3,13 @@ from conf import BASE_DIR # from tk_uploader.main import tiktok_setup, TiktokVideo -from tk_uploader.main_chrome import tiktok_setup, TiktokVideo +from uploader.tk_uploader.main_chrome import tiktok_setup, TiktokVideo from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags if __name__ == '__main__': filepath = Path(BASE_DIR) / "videos" - account_file = Path(BASE_DIR / "tk_uploader" / "account.json") + account_file = Path(BASE_DIR / "cookies" / "tk_uploader" / "account.json") folder_path = Path(filepath) # get video files from folder files = list(folder_path.glob("*.mp4")) diff --git a/examples/upload_video_to_xhs.py b/examples/upload_video_to_xhs.py index b0803f0e..8acabbca 100644 --- a/examples/upload_video_to_xhs.py +++ b/examples/upload_video_to_xhs.py @@ -6,10 +6,10 @@ from conf import BASE_DIR from utils.files_times import generate_schedule_time_next_day, get_title_and_hashtags -from xhs_uploader.main import sign_local, beauty_print +from uploader.xhs_uploader.main import sign_local, beauty_print config = configparser.RawConfigParser() -config.read(Path(BASE_DIR / "xhs_uploader" / "accounts.ini")) +config.read(Path(BASE_DIR / "uploader" / "xhs_uploader" / "accounts.ini")) if __name__ == '__main__': diff --git a/tencent_uploader/__init__.py b/tencent_uploader/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/uploader/__init__.py b/uploader/__init__.py new file mode 100644 index 00000000..c8cfe0b7 --- /dev/null +++ b/uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies").mkdir(exist_ok=True) \ No newline at end of file diff --git a/uploader/bilibili_uploader/__init__.py b/uploader/bilibili_uploader/__init__.py new file mode 100644 index 00000000..ce5d45e5 --- /dev/null +++ b/uploader/bilibili_uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies" / "bilibili_uploader").mkdir(exist_ok=True) \ No newline at end of file diff --git a/bilibili_uploader/biliup.exe b/uploader/bilibili_uploader/biliup.exe similarity index 100% rename from bilibili_uploader/biliup.exe rename to uploader/bilibili_uploader/biliup.exe diff --git a/bilibili_uploader/main.py b/uploader/bilibili_uploader/main.py similarity index 100% rename from bilibili_uploader/main.py rename to uploader/bilibili_uploader/main.py diff --git a/uploader/douyin_uploader/__init__.py b/uploader/douyin_uploader/__init__.py new file mode 100644 index 00000000..4a213b89 --- /dev/null +++ b/uploader/douyin_uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies" / "douyin_uploader").mkdir(exist_ok=True) \ No newline at end of file diff --git a/douyin_uploader/main.py b/uploader/douyin_uploader/main.py similarity index 100% rename from douyin_uploader/main.py rename to uploader/douyin_uploader/main.py diff --git a/uploader/ks_uploader/__init__.py b/uploader/ks_uploader/__init__.py new file mode 100644 index 00000000..c6176570 --- /dev/null +++ b/uploader/ks_uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies" / "ks_uploader").mkdir(exist_ok=True) \ No newline at end of file diff --git a/uploader/ks_uploader/main.py b/uploader/ks_uploader/main.py new file mode 100644 index 00000000..5f098100 --- /dev/null +++ b/uploader/ks_uploader/main.py @@ -0,0 +1,189 @@ +# -*- coding: utf-8 -*- +from datetime import datetime + +from playwright.async_api import Playwright, async_playwright +import os +import asyncio + +from utils.base_social_media import set_init_script +from utils.files_times import get_absolute_path +from utils.log import kuaishou_logger + + +async def cookie_auth(account_file): + async with async_playwright() as playwright: + browser = await playwright.chromium.launch(headless=True) + context = await browser.new_context(storage_state=account_file) + context = await set_init_script(context) + # 创建一个新的页面 + page = await context.new_page() + # 访问指定的 URL + await page.goto("https://cp.kuaishou.com/article/publish/video") + try: + await page.wait_for_selector("div.names div.container div.name:text('机构服务')", timeout=5000) # 等待5秒 + + kuaishou_logger.info("[+] 等待5秒 cookie 失效") + return False + except: + kuaishou_logger.success("[+] cookie 有效") + return True + + +async def ks_setup(account_file, handle=False): + account_file = get_absolute_path(account_file, "ks_uploader") + if not os.path.exists(account_file) or not await cookie_auth(account_file): + if not handle: + return False + kuaishou_logger.info('[+] cookie文件不存在或已失效,即将自动打开浏览器,请扫码登录,登陆后会自动生成cookie文件') + await get_ks_cookie(account_file) + return True + + +async def get_ks_cookie(account_file): + 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://cp.kuaishou.com") + await page.pause() + # 点击调试器的继续,保存cookie + await context.storage_state(path=account_file) + + +class KSVideo(object): + def __init__(self, title, file_path, tags, publish_date: datetime, account_file): + self.title = title # 视频标题 + self.file_path = file_path + self.tags = tags + self.publish_date = publish_date + self.account_file = account_file + self.date_format = '%Y-%m-%d %H:%M' + + async def handle_upload_error(self, page): + kuaishou_logger.error("视频出错了,重新上传中") + await page.locator('div.progress-div [class^="upload-btn-input"]').set_input_files(self.file_path) + + async def upload(self, playwright: Playwright) -> None: + # 使用 Chromium 浏览器启动一个浏览器实例 + browser = await playwright.chromium.launch(headless=False) + # 创建一个浏览器上下文,使用指定的 cookie 文件 + context = await browser.new_context(storage_state=f"{self.account_file}") + context = await set_init_script(context) + + # 创建一个新的页面 + page = await context.new_page() + # 访问指定的 URL + await page.goto("https://cp.kuaishou.com/article/publish/video") + kuaishou_logger.info('正在上传-------{}.mp4'.format(self.title)) + # 等待页面跳转到指定的 URL,没进入,则自动等待到超时 + kuaishou_logger.info('正在打开主页...') + await page.wait_for_url("https://cp.kuaishou.com/article/publish/video") + # 点击 "上传视频" 按钮 + await page.locator("div.vVExjn9O3UQ- input").set_input_files(self.file_path) + + await asyncio.sleep(2) + + if not await page.get_by_text("封面编辑").count(): + raise Exception("似乎没有跳转到到编辑页面") + + await asyncio.sleep(1) + + # 等待按钮可交互 + new_feature_button = page.locator('button[type="button"] span:text("我知道了")') + if await new_feature_button.count() > 0: + await new_feature_button.click() + + kuaishou_logger.info("正在填充标题和话题...") + await page.get_by_text('填写描述').locator("xpath=following-sibling::div").click() + kuaishou_logger.info("clear existing title") + await page.keyboard.press("Backspace") + await page.keyboard.press("Control+KeyA") + await page.keyboard.press("Delete") + kuaishou_logger.info("filling new title") + await page.keyboard.type(self.title) + await page.keyboard.press("Enter") + + # 快手只能添加3个话题 + for index, tag in enumerate(self.tags[:3], start=1): + kuaishou_logger.info("正在添加第%s个话题" % index) + await page.locator('span:text("#话题")').click() + await page.type('div.clGhv3UpdEo-', tag, delay=100) + await asyncio.sleep(2) + await page.locator('div.FZcv90s7kFs- > div').nth(0).click() + + while True: + try: + number = await page.locator('div > span:text("上传成功")').count() + if number > 0: + kuaishou_logger.success("视频上传完毕") + break + else: + kuaishou_logger.info("正在上传视频中...") + await asyncio.sleep(2) + except: + kuaishou_logger.info("正在上传视频中...") + await asyncio.sleep(2) + + # 定时任务 + if self.publish_date != 0: + await self.set_schedule_time(page, self.publish_date) + + # 判断视频是否发布成功 + while True: + # 判断视频是否发布成功 + try: + publish_button = page.get_by_role('button', name="发布", exact=True) + if await publish_button.count(): + await publish_button.click() + + await asyncio.sleep(1) + confirm_button = page.locator("button > span:text('确认发布')") + if await confirm_button.count(): + await page.locator("button > span:text('确认发布')").click() + + await page.wait_for_url("https://cp.kuaishou.com/article/manage/video?status=2&from=publish", + timeout=1500) + kuaishou_logger.success("视频发布成功") + break + except: + kuaishou_logger.info("视频正在发布中...") + await page.screenshot(full_page=True) + await asyncio.sleep(0.5) + + await context.storage_state(path=self.account_file) # 保存cookie + kuaishou_logger.info('cookie更新完毕!') + await asyncio.sleep(2) # 这里延迟是为了方便眼睛直观的观看 + # 关闭浏览器上下文和浏览器实例 + await context.close() + await browser.close() + + async def main(self): + async with async_playwright() as playwright: + await self.upload(playwright) + + async def set_schedule_time(self, page, publish_date): + kuaishou_logger.info("click schedule") + publish_date_hour = publish_date.strftime("%Y-%m-%d %H:%M:%S") + await page.locator("label:text('发布时间')").locator('xpath=following-sibling::div').locator( + '.ant-radio-input').nth(1).click() + await asyncio.sleep(1) + + await page.locator('div.ant-picker-input input[placeholder="选择日期时间"]').click() + await asyncio.sleep(1) + + await page.keyboard.press("Control+KeyA") + await page.keyboard.type(str(publish_date_hour)) + await page.keyboard.press("Enter") + await asyncio.sleep(1) + + diff --git a/uploader/tencent_uploader/__init__.py b/uploader/tencent_uploader/__init__.py new file mode 100644 index 00000000..9860b4a4 --- /dev/null +++ b/uploader/tencent_uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies" / "tencent_uploader").mkdir(exist_ok=True) \ No newline at end of file diff --git a/tencent_uploader/main.py b/uploader/tencent_uploader/main.py similarity index 100% rename from tencent_uploader/main.py rename to uploader/tencent_uploader/main.py diff --git a/uploader/tk_uploader/__init__.py b/uploader/tk_uploader/__init__.py new file mode 100644 index 00000000..262e9042 --- /dev/null +++ b/uploader/tk_uploader/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from conf import BASE_DIR + +Path(BASE_DIR / "cookies" / "tk_uploader").mkdir(exist_ok=True) \ No newline at end of file diff --git a/tk_uploader/main.py b/uploader/tk_uploader/main.py similarity index 99% rename from tk_uploader/main.py rename to uploader/tk_uploader/main.py index aa389550..6a9500d5 100644 --- a/tk_uploader/main.py +++ b/uploader/tk_uploader/main.py @@ -5,7 +5,7 @@ from playwright.async_api import Playwright, async_playwright import os import asyncio -from tk_uploader.tk_config import Tk_Locator +from uploader.tk_uploader.tk_config import Tk_Locator from utils.base_social_media import set_init_script from utils.files_times import get_absolute_path from utils.log import tiktok_logger diff --git a/tk_uploader/main_chrome.py b/uploader/tk_uploader/main_chrome.py similarity index 99% rename from tk_uploader/main_chrome.py rename to uploader/tk_uploader/main_chrome.py index aaf0bc38..4ba3fedd 100644 --- a/tk_uploader/main_chrome.py +++ b/uploader/tk_uploader/main_chrome.py @@ -7,7 +7,7 @@ import asyncio from conf import LOCAL_CHROME_PATH -from tk_uploader.tk_config import Tk_Locator +from uploader.tk_uploader.tk_config import Tk_Locator from utils.base_social_media import set_init_script from utils.files_times import get_absolute_path from utils.log import tiktok_logger diff --git a/tk_uploader/tk_config.py b/uploader/tk_uploader/tk_config.py similarity index 100% rename from tk_uploader/tk_config.py rename to uploader/tk_uploader/tk_config.py diff --git a/bilibili_uploader/__init__.py b/uploader/xhs_uploader/__init__.py similarity index 100% rename from bilibili_uploader/__init__.py rename to uploader/xhs_uploader/__init__.py diff --git a/xhs_uploader/accounts.ini b/uploader/xhs_uploader/accounts.ini similarity index 100% rename from xhs_uploader/accounts.ini rename to uploader/xhs_uploader/accounts.ini diff --git a/xhs_uploader/main.py b/uploader/xhs_uploader/main.py similarity index 100% rename from xhs_uploader/main.py rename to uploader/xhs_uploader/main.py diff --git a/xhs_uploader/xhs_login_qrcode.py b/uploader/xhs_uploader/xhs_login_qrcode.py similarity index 95% rename from xhs_uploader/xhs_login_qrcode.py rename to uploader/xhs_uploader/xhs_login_qrcode.py index 796602fd..b6a7d064 100644 --- a/xhs_uploader/xhs_login_qrcode.py +++ b/uploader/xhs_uploader/xhs_login_qrcode.py @@ -5,7 +5,7 @@ from xhs import XhsClient -from xhs_uploader.main import sign_local, sign +from uploader.xhs_uploader.main import sign # pip install qrcode if __name__ == '__main__': diff --git a/utils/log.py b/utils/log.py index fcd6876e..fa63a8ca 100644 --- a/utils/log.py +++ b/utils/log.py @@ -48,3 +48,4 @@ def filter_record(record): xhs_logger = create_logger('xhs', 'logs/xhs.log') tiktok_logger = create_logger('tiktok', 'logs/tiktok.log') bilibili_logger = create_logger('bilibili', 'logs/bilibili.log') +kuaishou_logger = create_logger('kuaishou', 'logs/kuaishou.log') diff --git a/xhs_uploader/__init__.py b/xhs_uploader/__init__.py deleted file mode 100644 index e69de29b..00000000