from unicodedata import name
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import pandas as pd
import gspread
import urllib3
import requests
from bs4 import BeautifulSoup
import re
import schedule
import os.path
options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome Beta\\User Data\\")
options.add_argument("--start-maximized") # use --start-maximized instead
options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"
print("Videos ready to upload...")
channelurl = "https://studio.youtube.com/channel/UCWDa0d_pcE80B9dzwonCPnw"
sa = gspread.service_account(filename="C:/Users/user/Documents/sheetsapi.json")
sh = sa.open("only74kotc")
sheet = sh.worksheet("Sheet4 above 70")
http = urllib3.PoolManager()
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
}
start_row = 592
end_row = start_row + 83
def sudo_uploader():
for i in range(start_row, end_row + 1):
value = sheet.cell(i, 11).value
if value == "yes":
url = sheet.cell(i, 1).value
try:
source = requests.get (url, allow_redirects=False, headers=headers)
soup = BeautifulSoup(source.text,'html.parser')
rawtitle = soup.find('h1', class_="ProductTitle__product-title___3QMYH").text
except:
print("waiting one min")
time.sleep(60)
source = requests.get (url, allow_redirects=False, headers=headers)
soup = BeautifulSoup(source.text,'html.parser')
rawtitle = soup.find('h1', class_="ProductTitle__product-title___3QMYH").text
myvalue = i
print("jiten1", myvalue)
def remove_bracket_contents(rawtitle):
return re.sub(r"\([^\)]+\s*(each|Each)\)", "", rawtitle)
mytabname = remove_bracket_contents(rawtitle).strip()
print(mytabname, "uploading")
simp_path = 'E:/otc/'+mytabname+'.mp4'
if os.path.isfile(simp_path):
bot = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=options)
wait = WebDriverWait(bot, 90)
bot.get(channelurl)
# bot.minimize_window()
time.sleep(3)
upload_button = bot.find_element(By.XPATH, '//*[@id="upload-icon"]')
upload_button.click()
time.sleep(3)
file_input = bot.find_element(By.XPATH, '//*[@id="content"]/input')
abs_path = os.path.abspath(simp_path)
file_input.send_keys(abs_path)
else:
sheet.update_cell(i, 13, "File not found")
continue # Skip to the next iteration of the loop
#Title
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="next-button"]')))
print("jiten2", myvalue)
try:
inputElement = bot.find_element(By.XPATH, '//*[@id="textbox"]')
inputElement.click() # click on the input field to ensure it's selected
inputElement.send_keys(Keys.CONTROL + "a") # press the Ctrl + A keys to select all text
inputElement.send_keys(Keys.DELETE) # press the Delete key to delete the selected text
inputElement.send_keys(""+mytabname+" Uses in Hindi | Side Effects | Dose") # enter your desired text
time.sleep(2)
inputElement = bot.find_elements(By.XPATH, '//*[@id="textbox"]')[1].send_keys(""+mytabname+" Uses in Hindi | Benefits | Side Effects | Dose\n\n\
------------------------------------\nRelated Searches\n\
"+mytabname+" Uses in Hindi\n\
"+mytabname+" Use in Hindi\n\
"+mytabname+" Kis kaam aati hai\n\
"+mytabname+" Review\n\
"+mytabname+" Use\n\
"+mytabname+" Side Effects\n\
"+mytabname+" ke Fayde\n\
"+mytabname+" ke fayde in hindi\n\
"+mytabname+" how to take\n\
"+mytabname+" how to use\n\
––––––––––––––––––––––––––––––\n\
Disclaimer:\n\
We are not promoting any medicine in our videos, our videos are for educating people about all medicines. \
Also, we are not suggesting anyone to take any medicine without the suggestion of your doctor. \
So this video is for information purposes only, always ask your doctor before eating any medicine.\n\
––––––––––––––––––––––––––––––\n\
SOME IMAGES ARE USED AS ILLUSTRATIONS FOR EDUCATIONAL PURPOSE UNDER FAIR USE - All images and videos used are in Public Domain\n\
––––––––––––––––––––––––––––––\n\
Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for 'fair use' for purposes \
such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted \
by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips \
the balance in favor of fair use.")
thumb_input = bot.find_element(By.XPATH, '//*[@id="file-loader"]')
simp2_path = 'E:/otcthumbnail/'+mytabname+'.jpg'
abs2_path = os.path.abspath(simp2_path)
time.sleep(3)
thumb_input.send_keys(abs2_path)
time.sleep(2)
bot.implicitly_wait(7)
print("passing the loop")
seemore_button = bot.find_element(By.XPATH, '/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[1]/ytcp-ve/ytcp-video-metadata-editor/div/div/ytcp-button/div')
bot.implicitly_wait(7)
print("see more mil gya")
bot.execute_script('arguments[0].click();', seemore_button)
time.sleep(2)
print("see more click ho gya")
bot.implicitly_wait(5)
try:
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Use in Hindi,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Kis kaam aati hai,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Review,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Use,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Side Effects,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" Dose,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" kis liye hoti hai,")
bot.implicitly_wait(1)
bot.find_element(By.XPATH, '//*[@id="text-input"]').send_keys(""+mytabname+" ke fayde,")
bot.implicitly_wait(1)
except:
print("tags not clickable")
time.sleep(1)
try:
element = WebDriverWait(bot, 340).until(EC.text_to_be_present_in_element((By.XPATH, '/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/div[1]/div/div/ytcp-animatable/ytcp-badge/div'), "Saved as private"))
time.sleep(5)
element = WebDriverWait(bot, 340).until(EC.text_to_be_present_in_element((By.XPATH, '/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/div[1]/div/div/ytcp-animatable/ytcp-badge/div'), "Saved as private"))
print("saved private complete")
except:
print("check error de gya")
time.sleep(2)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="close-button"]')))
next_button3 = bot.find_element(By.XPATH, '//*[@id="close-button"]')
for i in range(1):
next_button3.click()#close button
time.sleep(2)
print("first close done..............................")
max_attempts = 10
for attempt_number in range(1, max_attempts+1):
bot.get(f"{channelurl}/videos/upload?filter=%5B%7B%22name%22%3A%22VISIBILITY%22%2C%22value%22%3A%5B%22DRAFT%22%5D%7D%5D&sort=%7B%22columnType%22%3A%22date%22%2C%22sortOrder%22%3A%22DESCENDING%22%7D")
try:
element = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytcp-app/ytcp-entity-page/div/div/main/div/ytcp-animatable[4]/ytcp-content-section/ytcp-video-section/ytcp-video-section-content/div/ytcp-video-row/div/div[9]/ytcp-video-list-cell-actions/div/ytcp-button[3]/div')))
print("Edit draft is clickable")
break
except:
print(f"Element is not clickable - attempt {attempt_number} of {max_attempts}")
time.sleep(7)
try:
next_button3 = bot.find_element(By.XPATH, '/html/body/ytcp-app/ytcp-entity-page/div/div/main/div/ytcp-animatable[4]/ytcp-content-section/ytcp-video-section/ytcp-video-section-content/div/ytcp-video-row/div/div[9]/ytcp-video-list-cell-actions/div/ytcp-button[3]/div')
for i in range(1):
next_button3.click()#MEdit draft
time.sleep(2)
print("video information open")
except:
next_button3 = bot.find_element(By.XPATH, '/html/body/ytcp-app/ytcp-entity-page/div/div/main/div/ytcp-animatable[4]/ytcp-content-section/ytcp-video-section/ytcp-video-section-content/div/ytcp-video-row/div/div[9]/ytcp-video-list-cell-actions/div/ytcp-button[3]/div')
for i in range(1):
next_button3.click()#MEdit draft
time.sleep(2)
print("video information open")
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="step-badge-4"]')))
next_button = bot.find_element(By.XPATH, '//*[@id="step-badge-4"]')
for i in range(1):
next_button.click()#publish page
time.sleep(2)
bot.implicitly_wait(20)
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[1]/ytcp-uploads-review/div[2]/div[1]/ytcp-video-visibility-select/div[2]/tp-yt-paper-radio-group/tp-yt-paper-radio-button[3]')))
next_button7 = bot.find_element(By.XPATH, '/html/body/ytcp-uploads-dialog/tp-yt-paper-dialog/div/ytcp-animatable[1]/ytcp-uploads-review/div[2]/div[1]/ytcp-video-visibility-select/div[2]/tp-yt-paper-radio-group/tp-yt-paper-radio-button[3]')
for i in range(1):
next_button7.click()#Select Public
time.sleep(1)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="done-button"]')))
done_button = bot.find_element(By.XPATH, '//*[@id="done-button"]')
done_button.click()#Publish
time.sleep(3)
published = WebDriverWait(bot, 30).until(EC.text_to_be_present_in_element((By.XPATH, '//*[@id="dialog-title"]'), "Video"))
print("processing done")
time.sleep(2)
sheet.update_cell(myvalue, 13, "uploaded")
try:
lastclose = bot.find_element(By.XPATH, '/html/body/ytcp-video-share-dialog/ytcp-dialog/tp-yt-paper-dialog/div[3]/ytcp-button/div')
lastclose.click()#Publish
time.sleep(1)
print("Last close done")
except:
print("Last close Not done")
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytcp-app/ytcp-entity-page/div/div/main/div/ytcp-animatable[4]/ytcp-content-section/ytcp-video-section/ytcp-video-section-content/div/ytcp-video-row/div/div[4]/div/div')))
done_button = bot.find_element(By.XPATH, '/html/body/ytcp-app/ytcp-entity-page/div/div/main/div/ytcp-animatable[4]/ytcp-content-section/ytcp-video-section/ytcp-video-section-content/div/ytcp-video-row/div/div[4]/div/div')
done_button.click()#Monetization drop down
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="offRadio"]')))
done_button = bot.find_element(By.XPATH, '//*[@id="offRadio"]')
done_button.click()#Monetization on select
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytcp-video-monetization-edit-dialog/tp-yt-paper-dialog/div/div/ytcp-button[2]/div')))
done_button = bot.find_element(By.XPATH, '/html/body/ytcp-video-monetization-edit-dialog/tp-yt-paper-dialog/div/div/ytcp-button[2]/div')
done_button.click()#Monetization on Save
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytpp-self-certification-questionnaire-dialog/ytcp-dialog/tp-yt-paper-dialog/div[2]/ytpp-self-certification-questionnaire/div[3]/div/ytcp-checkbox-lit/div[2]')))
done_button = bot.find_element(By.XPATH, '/html/body/ytpp-self-certification-questionnaire-dialog/ytcp-dialog/tp-yt-paper-dialog/div[2]/ytpp-self-certification-questionnaire/div[3]/div/ytcp-checkbox-lit/div[2]')
done_button.click()#Ad suitablity on
wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/ytpp-self-certification-questionnaire-dialog/ytcp-dialog/tp-yt-paper-dialog/div[3]/div/ytcp-button[2]/div')))
done_button = bot.find_element(By.XPATH, '/html/body/ytpp-self-certification-questionnaire-dialog/ytcp-dialog/tp-yt-paper-dialog/div[3]/div/ytcp-button[2]/div')
done_button.click()#Ad suitablity Submit
time.sleep(4)
except:
print("Monetization on fails")
bot.quit()
except:
sheet.update_cell(myvalue, 13, "function error")
elif value == "Noooooo":
sheet.update_cell(i, 13, "Not available")
schedule.every().day.at("14:04").do(sudo_uploader)
while True:
# Checks whether a scheduled task
# is pending to run or not
schedule.run_pending()
time.sleep(1)