
RumbleSchedule:
;------------------------------------------------

CurrentSite := "Rumble"
SaveOrPostProgress(Message:="Scheduling Post in Rumble Studio",PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Status := NavigateFromBaseURLTo("https://studio.rumble.com/api/signin")
if(Status)
Return

CheckForAlerts()

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


try CurrentURL := driver.URL
if(InStr(CurrentURL, "/sso/auth/consent")){
	Message = Rumble Studio Logged out. Trying to log back in.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

	; login button in middle of page
	Xpath = //button[@class='consent-page--button header-user']
	Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

	sleep, 2000
	try CurrentURL := driver.URL
	if(InStr(CurrentURL, "/sso/auth/consent")){
		Message = Failed to login to Rumble Studio
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
}



; Schedule New Stream
; ------------------------------------------------
Message = Scheduling new stream and inputting information
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile")

; Schedule a stream button
Xpath = //div[normalize-space()='Schedule a stream']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click "Schedule a Stream" button. Livestream Already exists?
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

Xpath = (//h2[normalize-space()='Select a template (optional)'])
try ElementInnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
if(!ElementInnerText){
	Message = Templates Subwindow did not show up
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
	Return
}




loop, 3 {
	Xpath = (//div[@class='flex items-center justify-between gap-x-8 p-4 bg-indigo rounded-xl'])[%A_Index%]
	try, ElementText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

	; use the template with "Freedomain Livestream in the name"
	if(InStr(ElementText, "Freedomain Livestream")){
		Xpath = (//span[contains(text(),'Select')])[1]
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
		if(Status){
			Message = Failed to Select Livestream Template
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}
	}

}

; input thumbnail
if(Livestreamthumbnail){

	Xpath = //input[@id='room-thumbnail']
	Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=Livestreamthumbnail)
	if(Status){
		Message = Faied to upload custom thubmanil
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}
}



; input Livestream Title
Xpath = //input[@placeholder='Give your stream a name']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamTitle, ClearElement:=1)

; input Livestream Description
Xpath = //textarea[@placeholder='Tell the audience more about your stream']
Status := Selenium_LoopToSendValueToXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000,StringTextContent:=LivestreamDescription, ClearElement:=1)



; Input Date & Time
; ------------------------------------------------
; Create variables with time and date of timestream
FormatTime, RumbleLivestreamDate , %LivestreamDate%, dddd, MMMM
FormatTime, LivestreamOrdinalDay , %LivestreamDate%, d
FormatTime, RumbleLivestreamYear , %LivestreamDate%, yyyy

LivestreamOrdinalDay := GetDateOrdinalSuffix(LivestreamOrdinalDay)
; Msgbox % "LivestreamOrdinalDay: " LivestreamOrdinalDay

RumbleLivestreamDate := RumbleLivestreamDate . " " . LivestreamOrdinalDay . ", " . RumbleLivestreamYear
; Msgbox % "RumbleLivestreamDate: " RumbleLivestreamDate
; Msgbox % "RumbleLivestreamDate: " RumbleLivestreamDate


FormatTime, RumbleLivestreamTime , %LivestreamTime%, h:mm tt
; Msgbox % "RumbleLivestreamTime: " RumbleLivestreamTime
; FormatTime, RumbleLivestreamYear , %LivestreamDate%, yyyy

; click on box to open time picker dialogue
Xpath = //input[@placeholder='Pick a time']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)

; generate xpath based on date and click element
Xpath = //div[@aria-label='Choose %RumbleLivestreamDate%']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to select date from popup menu using generated xpath.`nXpath = %Xpath%
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

; generate xpath based on time and click element
Xpath = //li[normalize-space()='%RumbleLivestreamTime%']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to select time from popup menu using generated xpath.`nXpath = %Xpath%
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}



; Select Channel for Upload
; ------------------------------------------------
; click channel dropdown
Xpath = //span[@class='text-light truncate text-mini']
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click on Channel dropdown menu
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

; Grab inner text of all items in the channel select drop down menu
xpath = (//div[@class='shadow-md rounded-lg overflow-x-hidden bg-indigo'])[1]
try ElementInnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

; pull out name of first channel in dropdown
FirstChannelName := StrSplit(ElementInnerText, "`n")[1]

Xpath = (//span[normalize-space()='%FirstChannelName%'])[1]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to select the channel -%FirstChannelName%- from Channel dropdown menu
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	return
}



; Select each Secondary Platform checkbox
; ------------------------------------------------
IniRead, RumbleSecondaryPlatformsList, %SettingsIniFilepath%, %ScriptSettingsSection%, RumbleSecondaryPlatformsList, %A_Space%


if(RumbleSecondaryPlatformsList = ""){
	; if variable is empty, get all items in the list and write them to .ini file
	Loop, 10 {

		IndexPlusOne := A_index + 1
		Xpath = (//div[@class='flex shrink-0 items-center gap-x-4'])[%A_index%]
		XpathCheckbox = (//div[@class='relative shrink-0'])[%IndexPlusOne%]

		try SecondaryPlatformCheckboxName := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

		if(SecondaryPlatformCheckboxName = "Add new destination" OR SecondaryPlatformCheckboxName = ""){
			Break
		}

		RumbleSecondaryPlatformsList .= SecondaryPlatformCheckboxName . "--"


	}
	IniWrite, %RumbleSecondaryPlatformsList%, %SettingsIniFilepath%, %ScriptSettingsSection%, RumbleSecondaryPlatformsList
	MsgBox 0x40, Rumble Secondary Platforms List Generated, Rumble Secondary Platforms List was genearted and all the available options were added to it.`n`nPlease edit the settings.ini file and remove any platforms you do not need selected from the follwoing KEY:`n`n[%ScriptSettingsSection%]`nRumbleSecondaryPlatformsList=
}


Loop, 10 {

	IndexPlusOne := A_index + 1
	Xpath = (//div[@class='flex shrink-0 items-center gap-x-4'])[%A_index%]
	XpathCheckbox = (//div[@class='relative shrink-0'])[%A_index%]


	try SecondaryPlatformCheckboxName := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

	if(SecondaryPlatformCheckboxName = "Add new destination" OR SecondaryPlatformCheckboxName = ""){
		Break
	}

	if(!InStr(RumbleSecondaryPlatformsList,SecondaryPlatformCheckboxName))
	Continue

	Status := Selenium_LoopToClickXpath(Xpath:=XpathCheckbox,NumOfLoops:=2,SleepLength:=1000)
	if(Status){
		Message = Failed to check off the checkbox for: %SecondaryPlatformCheckboxName%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	}

	sleep, 250

}


if(ConfirmBeforeSubmit){
	OnMessage(0x44, "OnMsgBoxUserConfirmation")
	MsgBox 0x21, User Confirmation, Please check that all data was input correctly and fix any mistakes and then click PROCEED to finalize the Upload.`n`nClick STOP to cancel the rest of this Upload and move on to the next website.
	OnMessage(0x44, "")

	IfMsgBox OK, {

		} Else IfMsgBox Cancel, {
			Message = User Selected STOP button when asked for confirmation. Cancelling Rest of automation for this site.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}
	}
/*
*/

; click the "next" button to finalize the upload
Xpath = (//span[normalize-space()='Next'])[1]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)
if(Status){
	Message = Failed to click "Next" to finalize the upload
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


Message = Waiting 15 seconds before Navigating to Rumble and Grabbing Livestream URL
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
sleep, 15000


try, driver.get("https://rumble.com/account/live-streaming")
catch e {
	Message = Failed to Navigate to Rumble.com
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

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


Xpath = (//span[@class='video-info-status recorder'])[1]

Loop, 5 {
	if(A_index = 5){
		Message = Failed to grab livestream URL. `nAfter 5 checks the top livestream is still not in -Waiting for Stream- status.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
		Return
	}

	try StreamStatus := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
	if(StreamStatus != "Waiting for Stream"){
		Message = First Livestream on the list is not in -Waiting for Stream- status.`nWaiting 30 seconds and checking again.
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
		sleep, 30000
		try, driver.executeScript("history.go(0)") ;refresh page 
		try, driver.executeScript("return document.readyState").equals("complete") ; wait until page loads completely before proceeding
	}
	else, 
	break

}


Xpath = (//h2[@class='video-title'])[1]

RumbleURLSlug := GetHTMLValueFromXpathOuterHTML(XPATH, "href")
if(RumbleURLSlug = "" or RumbleURLSlug = "Failed"){
	Message = Failed to grab Rumble URL Slug from Livestreams Page
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}

RumbleLivestreamURL := "https://rumble.com" . RumbleURLSlug

IniWrite, %RumbleLivestreamURL%, %PostStatusesFilepath%, Livestream,RumbleLivestreamURL


return












