
OdyseeGrabURL:
Iniread, OdyseeURLSLUG, %PostStatusesFilepath%, Livestream,OdyseeURLSLUG, %A_Space%

if(OdyseeURLSLUG = ""){
	Message = OdyseeURLSLug is empty in the Livestream.ini file.
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}


Status := NavigateFromBaseURLTo("https://odysee.com/$/livestream")
if(Status)
Return

; click on Local Setup button to switch windows
Xpath = //span[contains(text(),'Local Setup')]
Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=2,SleepLength:=1000)


Xpath = //label[normalize-space()='Stream server']
try, Status := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text

if(!Status){
	Message = Failed to switch to "Stream Server" page
	SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
	Return
}



/*
Message = Waiting for Livestream Created Confirmation Popup
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

Xpath = //h2[normalize-space()='Livestream Created']

Loop, 60 {

	try, Status := ElementInnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
	if(Status = "Livestream Created")
	break
	else, {
		sleep, 1000
		Continue
	}

}

*/

Message = Grabbing Livestream URL from Livestream Settings Page
SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")
/*
		; Grab and Format the livestream URL
		Xpath = //li[@role='link']
		Loop, 60 {
			URLSLUG := GetHTMLValueFromXpathOuterHTML(XPATH, "href")
			if(URLSLug = "Failed"){
				Message = Failed to Grab URL SLug from confirmation popup
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				Return
			}
			; Msgbox % "URLSLug: " URLSLug
			if(InStr(URLSLug, OdyseeLivestreamSlug))
				break

			if(A_Index = 60){
				Message = Odysee did not generate permanant livestream URL after 1 minute of waiting. Congestion Issues?
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				Return
			}

			sleep, 1000

		}
		*/
		/*; Message = 
		Xpath = //span[contains(text(),'View Livestream Settings')]
		Status := Selenium_LoopToClickXpath(Xpath:=Xpath,NumOfLoops:=5,SleepLength:=1000)
		if(Status){
			Message = Failed to click on View Livestream Settigns Button
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}

		; Double check that we're on the right page.
		Xpath = //label[normalize-space()='Stream server']
		try InnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text
		if(InnerText != "Stream server"){
			Message = Failed to Navigate to View Livestream Settings Page
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}


		*/


		Message = Waiting for Livestream to get confirmed and URL to be generated`nWill Give up after 5 minutes
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")


		Xpath = //div[@class='section']//li[1]
		Loop, 100 {

			if(A_Index = 100){
				Message = Failed to grab URL after 5 minutes of waiting.
				SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				Return
			}

			Try ElementOuterHTML := driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag

			if(!InStr(ElementOuterHTML, OdyseeURLSLUG)){
				Sleep, 5000
				Continue
				; Message = Failed to Grab Livestream URL. Please copy and paste it manually.
				; SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
				; return

			}
			else, {
				break
			}

		}
		; Clipboard := OdyseeURLSLUG
		; Msgbox % "OdyseeURLSLUG: " OdyseeURLSLUG
		; Clipboard := ElementOuterHTML
		; Msgbox % "ElementOuterHTML: " ElementOuterHTML





		Xpath = //div[@class='section']//li[1]
		URLSLUG := GetHTMLValueFromXpathOuterHTML(XPATH, "href")
		if(URLSLUG = "Failed"){
			Message = Failed to Grab Livestream URL from outerHTML of livestream element.
			SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging")
			Return
		}

		OdyseeLivestreamURL := "https://odysee.com" . URLSLug
		; Msgbox % "OdyseeLivestreamURL: " OdyseeLivestreamURL

		; Message = URL Slug Grabbed from Confirmation Popup: %URLSLug%
		SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging")

		; OdyseeLivestreamURL := "https://odysee.com" . URLSLug


		IniWrite, %OdyseeLivestreamURL%, %PostStatusesFilepath%, Livestream,OdyseeLivestreamURL
