BitChuteUpload:
;------------------------------------------------
CurrentSite := "Bitchute"

Message = Starting Upload
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


Status := NavigateFromBaseURLTo("https://www.bitchute.com/")
if(Status)
Return

Message = Checking Login Status
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = (//div[normalize-space()='Sign in'])[1]
try LoginStatus := driver.findelementbyxpath(Xpath).Attribute("innerText")
if(LoginStatus = "Sign In"){
	Message = Logged out. Trying to Log Back In
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


}


/*
try TabUrl := driver.url
if(InStr(TabUrl, "/accounts/login/")){
	if(AutoLogin){
		
		
		Xpath = (//button[normalize-space()='Submit'])[1]
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		if(Status){
			Message = Failed to click Login Button. Please Log Back In
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}
	}
	else, {
		Message = Login Expired. Please Log Back In
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

}
*/


CheckForAlerts()

if(BitchuteUploadUploadURL = "")
{
	; click on video upload icon so dropdown menu appears
	Xpath = (//i[normalize-space()='video_call'])[1]
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	; grab upload url from dropdown menu
	Xpath = (//a[@id='id_upload_video'])[1]
	UploadURL := GetHTMLValueFromXpathOuterHTML(XPATH, "href")
	
	if(!InStr(UploadURL, "https")){
		Message = Failed to Grab Upload Page URL from Home Page
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

 	UploadURL := StrReplace(UploadURL, "api.bitchute", "old.bitchute")
}

try, driver.Get(UploadURL)    ;Open selected URL
try, driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding

Message = Inputting Title
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

; Try to input video title, check if input and if not, try again
loop, 5 {
	status := js_SendAndCheckWithQuerySelector(Selector:="#title",ValueToCheck:="value",SleepLength:=1000,JSStringText:=VideoTitle)
	; Msgbox % "status: " status

	if(!Status)
	break
}


if(Status){
	Message = Failed to input Video Title with Javascript. Trying again with Manual Typing
	SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")

	Xpath = //input[@id='title']
	Status := Selenium_TypeTextIntoElement(VideoTitle, Xpath)
	if(Status){
		Message = Failed to Input Video Title after trying two different ways. Please try Upload again and double check that the website hasn't been updated in a major way.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar, DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	try, TextInputted := driver.findelementbyxpath(Xpath).Attribute("value") ;XPath: ID=site-title & span tag
	if(InStr(VideoTitle, TextInputted) OR VideoTitle = TextInputted){
		; Title got input successfully
	}
	else, {
		Message = Title that got input into Bitchute: %TextInputted%
		SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging")

		Message = Failed to Input Video Title after trying two different ways. Please try Upload again and double check that the website hasn't been updated in a major way.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar, DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
}



Message = Inputting Description
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

; trim description if it's too long
BitchuteDescription := VideoDescription
if(StrLen(BitchuteDescription) > 2995){
	BitchuteDescription := SubStr(BitchuteDescription, 1, 2995)
	JSBitchuteDescription := FormatTextToJSText(BitchuteDescription) 
}
else, 
JSBitchuteDescription := FormatTextToJSText(BitchuteDescription) 

Loop, 10 { ; Attempt to input video description a couple of times
	Message = Inputting Description. Attempt #%A_index%
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


	if(A_index = 10){
		try currentTabURL := GetCurrentTabURlBase()
		; currentTabURL := SubStr(currentTabURL, 1, 40)
		Message = Failed to input Video Description after %A_index% attempts`nCurrent Tab URL: %currentTabURL%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}


	status := js_SendAndCheckWithQuerySelector("#description",ValueToCheck:="value",SleepLength:=1000,JSStringText:=JSBitchuteDescription)
	if(status){
		Message = %status%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	}
	else, 
	Break
	
	sleep, 1000
}


Message = Inputting Tags
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

; Get first 3 tags from Video Tags array
Loop, 3 {
	BitchuteTags .= ArrayOfVideoTags[A_Index] . " "
}

Message = Inputting Tags: %BitchuteTags%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")


; input Search Terms (Tags)
Xpath = //input[@placeholder='Search Terms']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=BitchuteTags)
if(Status){
	Message = Failed to input search terms (tags)
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}

js = return document.querySelector("#hashtags").value; 
try CurrentHashTagValue := driver.executeScript(JS)            ;Execute Javascript
if(CurrentHashTagValue = ""){
	Message = Tags that got input into page: %CurrentHashTagValue%
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
}


; Upload Thumbnail
Message = Attaching Thumbnail
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

if(VideoThumbFilepath != ""){
	; Upload Cover Image Button
	Message = Uploading Thumbnail
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")


	Xpath = //input[@name='thumbnailInput']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=VideoThumbFilepath)
	if(Status){
		Message = Failed to Attach Thumbnail
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
	; sleep, 5000
}

; Upload Video button
Message = Uploading Video File
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //input[@name='videoInput']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=VideoFilepath)
if(Status){
	Message = Failed to Upload Video
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}


Message = Waiting for Video to Finish Uploading`nChecking Progress Every 5 seconds
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
Xpath = //div[@role='progressbar'] ; Xpath to progress %
Loop, %Number_of_loops_to_Check_Upload_status% {
	sleep, %Time_Between_Loops_Upload_Status%

	if(A_index = 5){ ; while waiting for bitchute video to finish uploading, start up lbry
		if(OdyseeVideo OR OdyseeAudio){
			SaveOrPostProgress(Message:="Starting up LBRY while waiting for Bitchute Video to finish uploading",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
			CheckLBRYProcess()
		}
	}
	; Selenium
	; try BitChuteUploadProgress := driver.findelementbyxpath(Xpath).Attribute("innerText") ;GREAT FOR GRABBING INNER CONTENTS/Values

	try BitChuteUploadProgress := driver.executeScript("return document.querySelector('.filepond--file-status').innerHTML;") ;navigate using javascript 
	; Msgbox % "BitChuteUploadProgress: " BitChuteUploadProgress

	if(BitChuteUploadProgress = ""){
		Message = Unable to Grab Upload Progress, Upload Possibly Failed
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	if(InStr(BitChuteUploadProgress, "Upload complete")){
		Message = Waiting 10 Seconds before clicking "Proceed" button
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
		sleep, 10000
		Break ; Break out of the loop when 100% upload status

	}

	; split_text = ">
	; Pull out upload percentage
	BitChuteUploadProgress := StrSplit(BitChuteUploadProgress, ">")
	BitChuteUploadProgress := BitChuteUploadProgress[2]
	BitChuteUploadProgress := StrSplit(BitChuteUploadProgress, "</span")
	BitChuteUploadProgress := BitChuteUploadProgress[1]
	; DevModeMsgBox(BitChuteUploadProgress)

	Status := Check_For_Stuck_Video_Upload(A_index, BitChuteUploadProgress)
	if(Status = "Failed")
	Return
}

try UploadPageURL := driver.url ; save the active URL to a variable

; Finish Uploading Button
Xpath = //button[normalize-space()='Proceed']  ; Finish button
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=10,SleepLength:=5000)
if(Status){
	Message = Failed to Click "Finish" button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}

; Error Popup Location if something goes wrong.
Xpath = /html/body/div[2]/div
try BitChuteError := driver.findelementbyxpath(Xpath).Attribute("innerText") ; Grabb innertext


TooltipThis("Waiting for video page to finish loading")
try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding

; Loop for 2 minutes and keep checking if the active page has moved on to the video page

Message = Waiting 30 Seconds Before Refreshing Page to Find Newest Video
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

Loop, 6 { ; 6 loops X 30 seconds
	sleep, 5000 
	try CurrentWindow := driver.URL
	if(CurrentWindow != UploadPageURL AND A_index = 6) ; could also check for: https://www.bitchute.com/channel in str
	Break

	if(A_index = 6){
		Message = Upload Likely Failed: Videos Page did not Appear after 2 minutes
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
}

try driver.executeScript("history.go(0)") ;refresh page 


try FirstResultVideoTitle := driver.findElementsByClass("channel-videos-title").item[1].Attribute("innerText") ; Grabb innertext


try FirstResultIDAndTag := driver.findElementsByClass("channel-videos-title").item[1].Attribute("outerHTML") ;XPath: ID=site-title & span tag
; Msgbox % "FirstResultIDAndTag: " FirstResultIDAndTag
VideoHref := StrSplit(FirstResultIDAndTag, "<a href=")[2]
VideoHref := StrSplit(VideoHref, " class=")[1]
SingleQuote = "
VideoHref := StrReplace(VideoHref, SingleQuote, "")

if(VideoHref = ""){
	Message = Failed to Grab Video URL. Please Copy and Paste it into Result Window
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

BitChuteURL := "https://www.bitchute.com/" . VideoHref
BitChuteURL := StrReplace(BitchuteURL, "//video", "/video")

; navigate to video page
try driver.Get(BitChuteURL)	;Open selected URL
driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding


Xpath = //a[@data-toggle='tab'][normalize-space()='Settings']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=15,SleepLength:=5000)
if(Status){
	Message = Failed to Navigate to Video Settings Page to Uncheck "Allow Comments"
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}


; Unclick the "Discussion Allowed on This Video" checkbox
js = document.querySelector("form[id='save-settings'] div div label span").click()
try driver.executeScript(js)

try Status := driver.findElementsByID("id_is_discussable").item[1].isSelected()
Message = Checked Status = %Status%`n-1 is checked. 0 is unchecked
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


; Click the save button
Xpath = //button[normalize-space()='Save']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

Message = Upload Complete:`n%BitChuteURL%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
IniWrite, %BitChuteURL%, %VideoLinksIniFile%, URLs, BitChuteURL

SaveDriverURL()
AddToTotalVideosUploadedCount()


Return
