DailyMotionUpload:
;------------------------------------------------

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

IniRead, DailyMotionParnerUploadPage, %SettingsIniFilepath%, General, DailyMotionPostPageURL, %A_Space%
if(DailyMotionParnerUploadPage = ""){
	Message = Please add your DailyMotion Upload URL to settings.ini file under:`n`n[General]`nDailyMotionPostPageURL=https://www.dailymotion.com/partner/[YOURIDNUMBER]/media/video/upload
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

Status := NavigateFromBaseURLTo(DailyMotionParnerUploadPage,"Partner HQ - Dailymotion")
if(Status)
Return

Message = Waiting for Page to Finish Fully Loading
SaveOrPostProgress(Message:=Message,PostType:="Tooltip")
try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
sleep, 1500

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

LoggedOutStatus := CheckURLForSubstring("/sign-in")
if(LoggedOutStatus){

	if(AutoLogin){
		Message = Trying to Log Back in Automatically
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

		; have to click into username and password field for page to register that there's input
		; clicking with JS doesn't make it register, but with xpath selenium it does
		Xpath = //input[@placeholder='Email address']
		try driver.FindElementByXPath(Xpath).click()

		Xpath = //input[@placeholder='Enter password']
		try driver.FindElementByXPath(Xpath).click()

		js = document.querySelector("button[type='submit']").click(); 
		try driver.executeScript(js)

		try driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
		sleep, 1000

		; Do a double check to make sure that login worked
		Message = Checking Login Status
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

		LoggedOutStatus := CheckURLForSubstring("/sign-in")
		if(LoggedOutStatus){
			Message = Failed to log back in. Please Log Back In Manually
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			SaveDriverURLOFErrorPage()
			Return
		}

	} 
	else, { ; notify user and return
		Message = Login Expired. Please Log Back in
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	Message = Waiting 5 seconds for page to fully load
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
	sleep, 5000
}


status := CheckForAlerts()
if(Status){
	Message = Waiting for page to fully load
	try, driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
	sleep, 2000
}


Message = Uploading Video
SaveOrPostProgress(Message:=Message,PostType:="Tooltip")

Xpath = //input[@type='file']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=1,SleepLength:=2000,StringTextContent:=VideoFilepath)
if(Status){

	Message = Failed to Upload Video. Please Check Login Status
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return

}
sleep, 2000


Message = Uploading Thumbnail
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //input[@type='file']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=VideoThumbFilepath)
if(Status){
	Message = Failed to Upload Thumbnail: Check Login Status
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}


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

loop, 5 { ; sometimes the pre-inserted title doesn't get cleaned out when inputting title

if(A_index = 5){
	Message = Failed to input title after 5 attempts.
	; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	; Return
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}

Xpath = (//input[@placeholder='Enter text'])[1]
try, driver.FindElementByXPath(Xpath).click()

js = document.querySelector("input[placeholder='Enter text']").value = "%JSVideoTitle%";
try driver.executeScript(js)

Xpath = (//input[@placeholder='Enter text'])[1]
try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.SPACE)
try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.BackSpace)

; get text in title box and see if it matches video title
js = return document.querySelector("input[placeholder='Enter text']").value;
try CurrentTitle := driver.executeScript(js)
; Msgbox % "CurrentTitle: " CurrentTitle

if(CurrentTitle != VideoTitle){
	Message = Failed to input title on attempt %A_index%
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	sleep, 1000
}
else,
Break

}




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


if(strLen(VideoDescription) > 2900){
	DailyMotionDescription := SubStr(VideoDescription, 1, 2900) . "..."
}
else, 
DailyMotionDescription := VideoDescription


DailyMotionJSDescription := FormatTextToJSText(DailyMotionDescription)

Message := "DailyMotionJSDescription String Length: " . StrLen(DailyMotionJSDescription)
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


Message = Inputting Video Description
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")
Loop, 5 { ; make a couple attempts to input description
	if(A_index = 5){
		Message = Failed to input Description after %A_index% attempts.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}


	; click into element
	Xpath = //textarea[@placeholder='Enter a description']
	try driver.FindElementByXPath(Xpath).click()

	; send a space and then backspace so element registers the input
	Xpath = //textarea[@placeholder='Enter a description']
	try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.SPACE)
	try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.BackSpace)


	; inut description with javascript
	js = document.querySelector("textarea[placeholder='Enter a description']").value = "%JSVideoDescription%";
	try driver.executeScript(js)

	; send a space and then backspace so element registers the input
	Xpath = //textarea[@placeholder='Enter a description']
	try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.SPACE)
	try driver.FindElementByXPath(Xpath).SendKeys(driver.Keys.BackSpace)

	sleep, 1000

	js = return document.querySelector("textarea[placeholder='Enter a description']").textContent; 
	try, InputDescription := driver.executeScript(js)

	message = InputDescription: %InputDescription%
	DevModeMsgBox(InputDescription)

	; if input description is less than 15 chars, try again
	if(StrLen(InputDescription) < 15)
	Continue
	else, 
	Break

}



; Make a couple attempts to Click on Drop down menu and select the "Education" Category
Message = Selecting Education Category
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Loop, 3 {
	if(A_index = 3){
		Message = Failed to Select Education Category after %A_index% attempts
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}

	Xpath = //div[@name='channel']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	js = document.querySelector("div[title='Education'] div[class='ant-select-item-option-content']").click()
	try status := driver.executeScript(js)

	js = return document.querySelector("div[name='channel']").textContent; 
	try, status := driver.executeScript(js)

	if(instr(status, "Education"))
	break

}


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

; Append comma to end of tags
; DailyMotion page automtically splits the tags into individual tags when they're seperated by commas

; Iterate from one end of the array to another:
Loop % VideoTagsArray.Length(){
	; ArrayItem := ARRAY[A_Index]
	; MsgBox % 
	DailyMotionVideoTags .= VideoTagsArray[A_Index] . ","
}


; DailyMotionVideoTags := VideoTags . ","

Message = Video Tags: %DailyMotionVideoTags%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = (//input[@id='tags'])[1]
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=DailyMotionVideoTags)

; double check the input
js = return document.querySelector("div[name='tags']").textContent; 
try, inputTags := driver.executeScript(js)
Message = Tags that got input: %inputTags% (Will be doubled b/c of js)
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


Message = Clicking Next Button to Move on to next input screen
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //button[@type='button']//span[contains(text(),'Next')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)


Message = Selecting "Not For Kids" checkbox
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

try driver.findElementsByName("is_created_for_kids").item[2].click()
catch e {
	sleep, 2000
	try driver.findElementsByName("is_created_for_kids").item[2].click()
	catch e {
		Message = Failed to Click on the "Not Made For Kids" checkbox. Please switch to tab and finalize upload.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		SaveDriverURLOFErrorPage()
		Return
	}
}


; Disable Monetization Checkbox
Message = Disabling Monetization Checkbox
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //*[@id="advertising_instream_blocked"]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click on Monetization slider with xpath, trying with full xpath
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	Xpath = /html/body/div/div[2]/div/div[3]/div/div/div[2]/div[2]/div/div/div/div[4]/div/button
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to click on Monetization slider with full xpath, sleeping for 5 seconds so user can manually uncheck it.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}
}


Message = Clicking Next Button to Move on to third video settings screen
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //button[@type='button']//span[contains(text(),'Next')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)


Message = Clicking Save Button to Finalize Upload
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //span[normalize-space()='Save']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Upload was ALMOST successfuly. Failed to Click Final Save button
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
}


; Grab the Share URL and pull out the Video Link from it and save it to the Video links ini file, so double uploads are not tried if fialure occurs
Message = Waiting 30 seconds before grabbing Video URL
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

sleep, 30000


Xpath = //ul[@data-test-id='videos-media-list']//li//div//div//div//div//h3//a
try, DailyMotionInternalURL := driver.findelementbyxpath(Xpath).Attribute("href")
if(DailyMotionInternalURL = ""){
	Message = Failed to Grab Video URL. Upload Most likely Failed.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	SaveDriverURLOFErrorPage()
	Return
}


; Pull out URL slug from internal video URL and create dailymotion share URL from it
DailyMotionURL := StrSplit(DailyMotionInternalURL, "/video/details/")[2] 
DailyMotionURL := "https://www.dailymotion.com/video/" . DailyMotionURL


Message = Upload Complete: %DailyMotionURL%
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
IniWrite, %DailyMotionURL%, %VideoLinksIniFile%, URLs, DailyMotionURL
SaveDriverURL()
AddToTotalVideosUploadedCount()

Return