import requests import json import pyppeteer import asyncio from tqdm import tqdm from os import listdir from os.path import isfile, join, abspath username = 'логин долфин' password = 'пасс долфин' host = '127.0.0.1' videos_dir = r'папка с видосами' titles_path = r'путь до названий' comments_path = r'путь до комментов' def get_token(): login_url = "https://anty-api.com/auth/login" payload = f'username={username}&password={password}' headers = { 'Content-Type': 'application/x-www-form-urlencoded' } response = requests.post(login_url, headers=headers, data=payload) token = json.loads(response.text)['token'] print(f'Your token: {token}') return token def get_browser_profiles(token): profiles_url = "https://anty-api.com/browser_profiles" payload = 'username=motojo6085%40ploneix.com&password=7313ivan' headers = { 'Authorization': f'Bearer {token}', 'Content-Type': 'application/x-www-form-urlencoded' } response = requests.get(profiles_url, headers=headers, data=payload) browser_profile = json.loads(response.text)['data'][0]['id'] print(f'Your browser profile: {browser_profile}') return browser_profile def start_browser(browser_id): try: start_url = f'http://localhost:3001/v1.0/browser_profiles/{browser_id}/start?automation=1' response = requests.get(start_url) port = json.loads(response.text)['automation']['port'] ws_endpoint = json.loads(response.text)['automation']['wsEndpoint'] return port, ws_endpoint except Exception as ex: print(f'\033[31mProfile ID {browser_id} already running, close it') def stop_browser(browser_id): stop_url = f'http://localhost:3001/v1.0/browser_profiles/{browser_id}/stop' response = requests.get(stop_url) print(f'Finish Profile {browser_id}') async def deploy(port, ws_endpoint, data): global page browser = await pyppeteer.connect(browserWSEndpoint=f'ws://{host}:{str(port) + ws_endpoint}', defaultViewport=None) page = await browser.newPage() for d in tqdm(data): response = await page.goto('https://studio.youtube.com/', timeout=30000) if response.status not in [200, 302]: print(f'\nAttempt to reconnect...') for i in range(5): print(f'\nFailed to navigate to page https://studio.youtube.com/ (Attempt {i + 1})') response = await page.goto('https://studio.youtube.com/', timeout=30000) if response.status == 200 or response.status == 302: break await asyncio.sleep(5) print("step1") await page.waitForXPath('//ytcp-button[@id="create-icon"]//tp-yt-iron-icon[1]', timeout=30000) create_btn = await page.xpath('//ytcp-button[@id="create-icon"]//tp-yt-iron-icon[1]') await create_btn[0].click() print("step2") await page.waitForXPath('//yt-formatted-string[@class="item-text style-scope ytcp-text-menu"]', timeout=30000) add_video_btn = await page.xpath('//yt-formatted-string[@class="item-text style-scope ytcp-text-menu"]') await add_video_btn[0].click() print("step3") await page.waitForXPath('//input[@type="file"]', timeout=30000) input_form = await page.xpath('//input[@type="file"]') await input_form[0].uploadFile(d[0]) print("step4") await page.waitForSelector('#textbox', timeout=30000) await asyncio.sleep(6) await page.type(selector='#textbox', text=d[1]) print("step5") await page.waitForXPath('//tp-yt-paper-radio-button[@name="VIDEO_MADE_FOR_KIDS_NOT_MFK"]', timeout=30000) for_not_kids_btn = await page.xpath('//tp-yt-paper-radio-button[@name="VIDEO_MADE_FOR_KIDS_NOT_MFK"]') await for_not_kids_btn[0].click() print("step6") await page.waitForXPath('//ytcp-button[@id="toggle-button"]', timeout=30000) await asyncio.sleep(5) to_comments_btn = await page.xpath('//ytcp-button[@id="toggle-button"]') await to_comments_btn[0].click() print("step7") await page.waitForXPath('//tp-yt-paper-radio-button[@id="allow-all-radio-button"]/div[1]/div[1]', timeout=30000) comments_radio_btn = await page.xpath('//tp-yt-paper-radio-button[@id="allow-all-radio-button"]/div[1]/div[1]') await comments_radio_btn[0].click() print("step8") await page.waitForXPath('//ytcp-video-player-controls[@id="video-player"]', timeout=180000) await page.waitForXPath('//ytcp-button[@id="next-button"]', timeout=30000) next_btn = await page.xpath('//ytcp-button[@id="next-button"]') await next_btn[0].click() await next_btn[0].click() await next_btn[0].click() print("step9") await page.waitForXPath('//tp-yt-paper-radio-group[@id="privacy-radios"]/tp-yt-paper-radio-button[3]', timeout=30000) await asyncio.sleep(5) public_radio_btn = await page.xpath( '//tp-yt-paper-radio-group[@id="privacy-radios"]/tp-yt-paper-radio-button[3]') await public_radio_btn[0].click() print("step10") await page.waitForXPath('//ytcp-button[@id="done-button"]', timeout=30000) done_btn = await page.xpath('//ytcp-button[@id="done-button"]') await done_btn[0].click() print("step11") await page.waitForXPath('//a[@id="share-url"]', timeout=30000) shrot_ref = await page.evaluate('document.querySelector("#share-url").innerText') response = await page.goto(shrot_ref, timeout=30000) if response.status not in [200, 302]: print(f'\nAttempt to reconnect...') for i in range(5): print(f'\nVideo is upload but failed to navigate to page {shrot_ref} (Attempt {i + 1})') response = await page.goto(shrot_ref, timeout=30000) if response.status == 200 or response.status == 302: break await asyncio.sleep(5) await page.waitForXPath('//div[@id="comments-button"]', timeout=30000) comments_btn = await page.xpath('//div[@id="comments-button"]') is_disabled = await (await comments_btn[0].getProperty('disabled')).jsonValue() if is_disabled: print(f'Failed to add comment for video {shrot_ref}') continue else: await comments_btn[0].click() try: await page.waitForXPath('//yt-formatted-string[@id="simplebox-placeholder"]', timeout=30000) comment_btn = await page.xpath('//yt-formatted-string[@id="simplebox-placeholder"]') await comment_btn[0].click() except Exception as ex: continue await page.waitForXPath('//yt-formatted-string[@id="contenteditable-textarea"]//div[1]', timeout=30000) await page.type(selector='#contenteditable-root', text=d[2]) await page.waitForXPath('//ytd-button-renderer[@id="submit-button"]', timeout=30000) confirm_btn = await page.xpath('//ytd-button-renderer[@id="submit-button"]') await confirm_btn[0].click() await asyncio.sleep(10) await page.waitForSelector('div#action-menu ytd-menu-renderer', timeout=30000) await page.evaluate( 'document.querySelector("div#action-menu ytd-menu-renderer").setAttribute("menu-active", true)') comment_menu = await page.xpath('//div[@id="action-menu"]/ytd-menu-renderer') await comment_menu[0].click() await page.waitForXPath('//tp-yt-paper-listbox[@id="items"]/ytd-menu-navigation-item-renderer', timeout=30000) fix_option_btn = await page.xpath('//tp-yt-paper-listbox[@id="items"]/ytd-menu-navigation-item-renderer') await fix_option_btn[0].click() await page.waitForXPath('//yt-button-renderer[@id="confirm-button"]', timeout=30000) confirm_btn = await page.xpath('//yt-button-renderer[@id="confirm-button"]') await confirm_btn[0].click() await asyncio.sleep(5) if __name__ == '__main__': token = get_token() browser_id = get_browser_profiles(token) videos = [abspath(join(videos_dir, f)) for f in listdir(videos_dir) if isfile(join(videos_dir, f))] titles = [t for t in open(titles_path, encoding='UTF-8')] comments = [c for c in open(comments_path, encoding='UTF-8')] data = [] for video, title, comment in zip(videos, titles, comments): data.append((video, title, comment)) print('Enter the number of videos to upload: ') count = int(input()) port, ws_endpoint = start_browser(browser_id) asyncio.get_event_loop().run_until_complete(deploy(port, ws_endpoint, data[:count])) stop_browser(browser_id)