From 5671242faff9800cf19672c738f6adf58b9af06b Mon Sep 17 00:00:00 2001 From: Yuriy Date: Thu, 25 Apr 2024 19:57:26 -0400 Subject: [PATCH 1/3] modularization and addition of Locals Specific Functions --- Locals-Functions.ahk | 50 ++++++++++++++++++++++++++++++++++++++++++ Selenium-Functions.ahk | 32 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 Locals-Functions.ahk diff --git a/Locals-Functions.ahk b/Locals-Functions.ahk new file mode 100644 index 0000000..4a73062 --- /dev/null +++ b/Locals-Functions.ahk @@ -0,0 +1,50 @@ + + +; Iterate over Locals posts on the home page until found post with title +; ------------------------------------------------ +GrabLocalsPostURLUsingTitle(PostTitle){ + + Loop, 10 { + Message = Checking Post %A_index% on page for Post Titled`n%PostTitle% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + ToolTip, %A_Index% + + ; first two items can be skipped. + ; new Post box and Create other forms of posts box + if(A_index < 3) + Continue + + ; Xpath for each post box on the main page + Xpath = //body/div/div/div/div/div/div[1]/div[%A_Index%] + + + try ElementInnerText := driver.findelementbyxpath(Xpath).Attribute("innerText") ;XPATH Inner Text + Try ElementOuterHTML := driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag + + + if(InStr(ElementInnerText, PostTitle)){ + Message = Found Post Title in Element Number: %A_index% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + + ; Pull Out the Post URL from the OuterHTML using RegEx + regexMatch := RegExMatch(ElementOuterHTML, "data-post-url=""([^""]+)""", match) + if (regexMatch) + { + PostURL := match1 + Message = URL Pulled out from OuterHTML: %PostURL% + Return PostURL + } + else { + Message = Failed to Pull out URL from OuterHTML + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + Return "Failed" + } + + } + } + + Message = Failed to find New Post in the first 10 posts + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + Return "Failed" + +} diff --git a/Selenium-Functions.ahk b/Selenium-Functions.ahk index b62ffb7..617a2c4 100644 --- a/Selenium-Functions.ahk +++ b/Selenium-Functions.ahk @@ -820,6 +820,38 @@ SaveDriverURLOFErrorPage(){ ; save the url of the result page. That way if a tab } +GetHTMLValueFromXpathOuterHTML(XPATH, HTMLElementName){ + +Try ElementOuterHTML := driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag +; try Clipboard := driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag +; Try MsgBox,,Element OuterHTML: `n, % driver.findelementbyxpath(Xpath).Attribute("outerHTML") ;XPATH-ID & Tag + +; Match string between quotes following the HTMLELEMENTNAME +RegexStatement = %HTMLElementName%="([^"]+)" + +; regexMatch := RegExMatch(ElementOuterHTML, "value=""([^""]+)""", match) +regexMatch := RegExMatch(ElementOuterHTML, RegexStatement, match) +if (regexMatch) +{ + ElementValue := match1 + ; Msgbox % "PostURL: " PostURL + ; Message = URL Pulled out from OuterHTML: %PostURL% + Return ElementValue +} +else { + Message = Failed to Pull out value from OuterHTML + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + Return "Failed" +} + +} + + + + + + + ;---\Selenium--- ;------------------------------------------------ From b8ffb10db187c70219c594ddfe7cdebc6885d552 Mon Sep 17 00:00:00 2001 From: Yuriy Date: Fri, 26 Apr 2024 19:41:40 -0400 Subject: [PATCH 2/3] improved chrome update functionality --- Chrome-Functions.ahk | 424 ++++++++++++++++++++++++------------------- 1 file changed, 237 insertions(+), 187 deletions(-) diff --git a/Chrome-Functions.ahk b/Chrome-Functions.ahk index 9eea04c..f3d417f 100644 --- a/Chrome-Functions.ahk +++ b/Chrome-Functions.ahk @@ -21,6 +21,9 @@ global InstalledChromeVersion ; Chrome Related Functions ;------------------------------------------------ DownloadLatestChromium(){ + Message = Checking Variables for Downloading Newest Chrome + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + if(ChromeFilepath = "") ChromeFilepath := GetInstalledChromeFilepath() @@ -90,9 +93,9 @@ DownloadLatestChromium(){ ; close any open instances of Chromium - SetTitleMatchMode, 2 ; substring - + ; SetTitleMatchMode, 2 ; substring +/* if(WinExist(" - Chromium")){ Message = Trying to close any open Chromium Windows SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") @@ -111,292 +114,339 @@ DownloadLatestChromium(){ MsgBox 0x10,, %Message% return } + */ + ; make sure all instances of chrome are closed + Loop, { + ChromeOpen := WinExist("ahk_exe chrome.exe") + if(ChromeOpen) { + Message = Chrome is Open. Asking user to close instances before continuing. + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - ; msgbox + MsgBox 0x41, Chrome is Open, Please close all instances of Chrome and then click OK to continue with replacing Chrome with the newest version. - FileGetTime, ChromeDriverExeOldCreationTime, %ChromeDriverProgramFilesExeFilepath%, C + IfMsgBox OK, { + } Else IfMsgBox Cancel, { + return + } + } + else, + break + } - Message = Downloading Chromedriver v%ChromeStableLatestVersion% to:`n%ChromeDriverDownloadZipFilepath% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - UrlDownloadToFile, %chromedriverDLURL64%, %ChromeDriverDownloadZipFilepath% - Message = Downloading Chromium v%ChromeStableLatestVersion% to:`n%ChromeDownloadZipFilepath% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - UrlDownloadToFile, %chromeDLURL64%, %ChromeDownloadZipFilepath% + ; make sure all instances of chromedriver.exe are closed + Message = Checking and killing any instances of the chromedriver.exe process + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - ; delete the pre-existing chrome-win64 directory - Message = Deleting Old Chromium Directory - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + Loop, 5 { - FileRemoveDir, %ChromeExtractedDirectory%, 1 - if(FileExist(ChromeExtractedDirectory)){ - Message = Failed to delete the old Chromium. `nPlease manually delete the following directory and then run the Chrome Update again.`n`n %ChromeExtractedDirectory% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar") + Process, Exist, chromedriver.exe + ChromedriverExists := ErrorLevel - MsgBox 0x10,, %Message% - - ToolTip - return - } + if(ChromedriverExists){ + Message = Chromedriver Exists on check number %A_Index% + SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging") - - ; Double check that .zip files exist - ChromeDriverZipExists := FileExist(ChromeDriverDownloadZipFilepath) - if(!ChromeDriverZipExists){ - Message = Download of chromedriver failed for some reason. chromedriver.zip not found. - SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - MsgBox 0x10,, %Message% - Return - } + Process, Close, chromedriver.exe + sleep, 5000 ; wait 5 seconds before checking again + } + else, + break - ChromeZipExists := FileExist(ChromeDownloadZipFilepath) - if(!ChromeZipExists){ - Message = Download of chrome failed for some reason. chrome.zip not found. - SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - MsgBox 0x10,, %Message% - Return - } + Message = Chrome Update Failed. Unable to kill background chromedriver.exe process + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + return + } + FileGetTime, ChromeDriverExeOldCreationTime, %ChromeDriverProgramFilesExeFilepath%, C - Message = Extracting Chromedriver v%ChromeStableLatestVersion% Zip file - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - Unzip(ChromeDriverDownloadZipFilepath, ChromeDriverDownloadFilepath) - Message = Extracting Chromium v%ChromeStableLatestVersion% Zip file - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - Unzip(ChromeDownloadZipFilepath, ChromeExtractedDirectory) + Message = Downloading Chromedriver v%ChromeStableLatestVersion% `nFrom: %chromedriverDLURL64%`nTo: %ChromeDriverDownloadZipFilepath% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + UrlDownloadToFile, %chromedriverDLURL64%, %ChromeDriverDownloadZipFilepath% - + Message = Downloading Chromium v%ChromeStableLatestVersion% `nfrom: %chromeDLURL64% to:`n%ChromeDownloadZipFilepath%`n(This might take 1-2 minutes) + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + UrlDownloadToFile, %chromeDLURL64%, %ChromeDownloadZipFilepath% - - ; Check if the Copy-ChromeDriver-To-Program-Files.exe file exists - ; @todo, remove later. this is temporary to fix chromedriver-mover being broken and needing to be replaced - FileDelete, %ChromeDriverMoverEXEFilepath% + ; delete the pre-existing chrome-win64 directory + Message = Deleting Old Chromium Directory + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - Message = Checking Chromedriver-Mover.exe exists - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - if(!FileExist(ChromeDriverMoverEXEFilepath)){ - Message = Chromedriver-Mover not found. Downloading from Gitea - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - UrlDownloadToFile, %ChromeDriverMoverURL%, %ChromeDriverMoverEXEFilepath% - if(ErrorLevel){ - Message = Failed to download the Copy-ChromeDriver-To-Program-Files.exe executable. `n`n%ManualMoveChromedriverMessage% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + FileRemoveDir, %ChromeExtractedDirectory%, 1 + if(FileExist(ChromeExtractedDirectory)){ + Message = Failed to delete the old Chromium. `nPlease manually delete the following directory and then run the Chrome Update again.`n`n %ChromeExtractedDirectory% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar") + + MsgBox 0x10,, %Message% ToolTip - Return + return } - } - if(!FileExist(ChromeDriverMoverEXEFilepath)){ - Message = ChromeDriver-Mover not found after downloading. `nPossibly triggered and deleted by Antivirus?`n`n%ManualMoveChromedriverMessage% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging,msgbox") + ; Double check that .zip files exist + ChromeDriverZipExists := FileExist(ChromeDriverDownloadZipFilepath) + if(!ChromeDriverZipExists){ + Message = Download of chromedriver failed for some reason. chromedriver.zip not found. + SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + MsgBox 0x10,, %Message% + Return + } - ToolTip - return - } + ChromeZipExists := FileExist(ChromeDownloadZipFilepath) + if(!ChromeZipExists){ + Message = Download of chrome failed for some reason. chrome.zip not found. + SaveOrPostProgress(Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + MsgBox 0x10,, %Message% + Return + } - Message = Starting up Chromedriver-Mover - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - ; Msgbox % "ChromeDriverMoverEXEFilepath: " ChromeDriverMoverEXEFilepath - ; Msgbox % "ChromeDriverDownloadFilepath: " ChromeDriverDownloadFilepath - try, Run "%ChromeDriverMoverEXEFilepath%" "%ChromeDriverDownloadFilepath%" - if(ErrorLevel = "ERROR"){ - Message = Failed to run Chromedriver-Mover.exe program to automatically copy the chromedriver to Program Files.`n%ManualMoveChromedriverMessage% - MsgBox 0x40,, %Message% - } + Message = Extracting Chromedriver v%ChromeStableLatestVersion% Zip file + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + Unzip(ChromeDriverDownloadZipFilepath, ChromeDriverDownloadFilepath) + Message = Extracting Chromium v%ChromeStableLatestVersion% Zip file + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + Unzip(ChromeDownloadZipFilepath, ChromeExtractedDirectory) - Message = Waiting 30 seconds for new Chromedriver to get moved to C:\Program Files\SeleniumBasic\ - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - Loop, 30 { - sleep, 1000 - FileGetTime, ChromeDriverExeCurrentCreationTime, %ChromeDriverProgramFilesExeFilepath%, C - ; Msgbox % "ChromeDriverExeOldCreationTime: " ChromeDriverExeOldCreationTime - ; Msgbox % "ChromeDriverExeCurrentCreationTime: " ChromeDriverExeCurrentCreationTime + ; Check if the Copy-ChromeDriver-To-Program-Files.exe file exists - if(ErrorLevel){ + ; @todo, remove later. this is temporary to fix chromedriver-mover being broken and needing to be replaced + FileDelete, %ChromeDriverMoverEXEFilepath% + + Message = Checking Chromedriver-Mover.exe exists + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - Message = chromedriver.exe not found in %ChromeDriverProgramFilesExeFilepath%. `nWaiting for new version to get copied over. + if(!FileExist(ChromeDriverMoverEXEFilepath)){ + Message = Chromedriver-Mover not found. Downloading from Gitea SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - ; sleep, 1000 - Continue + UrlDownloadToFile, %ChromeDriverMoverURL%, %ChromeDriverMoverEXEFilepath% + if(ErrorLevel){ + Message = Failed to download the Copy-ChromeDriver-To-Program-Files.exe executable. `n`n%ManualMoveChromedriverMessage% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + + ToolTip + Return + } } - if(ChromeDriverExeCurrentCreationTime != ChromeDriverExeOldCreationTime and ChromeDriverExeCurrentCreationTime != ""){ - break + + if(!FileExist(ChromeDriverMoverEXEFilepath)){ + Message = ChromeDriver-Mover not found after downloading. `nPossibly triggered and deleted by Antivirus?`n`n%ManualMoveChromedriverMessage% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging,msgbox") + + ToolTip + return } - ; sleep, 1000 - Continue - } + Message = Starting up Chromedriver-Mover + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + + + ; Msgbox % "ChromeDriverMoverEXEFilepath: " ChromeDriverMoverEXEFilepath + ; Msgbox % "ChromeDriverDownloadFilepath: " ChromeDriverDownloadFilepath + + try, Run "%ChromeDriverMoverEXEFilepath%" "%ChromeDriverDownloadFilepath%" + if(ErrorLevel = "ERROR"){ + Message = Failed to run Chromedriver-Mover.exe program to automatically copy the chromedriver to Program Files.`n%ManualMoveChromedriverMessage% + MsgBox 0x40,, %Message% + } + + + Message = Waiting 30 seconds for new Chromedriver to get moved to C:\Program Files\SeleniumBasic\ + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + + Loop, 30 { + sleep, 1000 - if(!FileExist(ChromeDriverProgramFilesExeFilepath)){ - Message = Failed to Automatically move chromedriver.exe`n%ManualMoveChromedriverMessage% + FileGetTime, ChromeDriverExeCurrentCreationTime, %ChromeDriverProgramFilesExeFilepath%, C + + ; Msgbox % "ChromeDriverExeOldCreationTime: " ChromeDriverExeOldCreationTime + ; Msgbox % "ChromeDriverExeCurrentCreationTime: " ChromeDriverExeCurrentCreationTime + + if(ErrorLevel){ + + Message = chromedriver.exe not found in %ChromeDriverProgramFilesExeFilepath%. `nWaiting for new version to get copied over. + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + ; sleep, 1000 + Continue + + } + + if(ChromeDriverExeCurrentCreationTime != ChromeDriverExeOldCreationTime and ChromeDriverExeCurrentCreationTime != ""){ + break + } + + ; sleep, 1000 + Continue + } + + if(!FileExist(ChromeDriverProgramFilesExeFilepath)){ + Message = Failed to Automatically move chromedriver.exe`n%ManualMoveChromedriverMessage% + MsgBox 0x40,, %Message% + ToolTip + return + } + + Message = Chromium and Chromedriver.exe were updated successfully. + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") MsgBox 0x40,, %Message% + ToolTip return } - Message = Chromium and Chromedriver.exe were updated successfully. - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - MsgBox 0x40,, %Message% - ToolTip - return -} + CheckForChromeUpdates(ChromeFilepath := ""){ + if(ChromeFilepath = "") + ChromeFilepath := GetInstalledChromeFilepath() + CurrentInstalledChromeVersion := GetInstalledChromeVersion() + CurrentChromeStableVersion := GetLatestChromeStableVersion() -CheckForChromeUpdates(ChromeFilepath := ""){ - if(ChromeFilepath = "") - ChromeFilepath := GetInstalledChromeFilepath() + if(CurrentInstalledChromeVersion < CurrentChromeStableVersion){ + ChromeUpdateAvailable := 1 + return ChromeUpdateAvailable + } + else, Return 0 - CurrentInstalledChromeVersion := GetInstalledChromeVersion() - CurrentChromeStableVersion := GetLatestChromeStableVersion() - if(CurrentInstalledChromeVersion < CurrentChromeStableVersion){ - ChromeUpdateAvailable := 1 - return ChromeUpdateAvailable } - else, Return 0 -} + GetInstalledChromeFilepath(){ + ; ChromeFilepath is global variable + if(ChromeFilepath = ""){ -GetInstalledChromeFilepath(){ - ; ChromeFilepath is global variable + ChromePortableFilepath = %A_ScriptDir%\Lib\chrome-win64\chrome.exe + ChromePortableFilepath2 = %A_ScriptDir%\Lib\chrome-win64\chrome-win64\chrome.exe ; filepath of chrome.exe that was downloaded automatically using this uploader - if(ChromeFilepath = ""){ + if(FileExist(ChromePortableFilepath) or FileExist(ChromePortableFilepath2)){ + if(FileExist(ChromePortableFilepath2)) + ChromeFilepath := ChromePortableFilepath2 - ChromePortableFilepath = %A_ScriptDir%\Lib\chrome-win64\chrome.exe - ChromePortableFilepath2 = %A_ScriptDir%\Lib\chrome-win64\chrome-win64\chrome.exe ; filepath of chrome.exe that was downloaded automatically using this uploader + if(FileExist(ChromePortableFilepath)) + ChromeFilepath := ChromePortableFilepath + + Message = Using Chromium Portable for Upload + SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging") + } + else { + MsgBox 0x10, Chromium Portable not Found, Chromium Portable not found. `nPlease make sure it's located at one of the following paths and then try again.`n`n%ChromePortableFilepath% `nOR`n%ChromePortableFilepath2% - if(FileExist(ChromePortableFilepath) or FileExist(ChromePortableFilepath2)){ - if(FileExist(ChromePortableFilepath2)) - ChromeFilepath := ChromePortableFilepath2 - if(FileExist(ChromePortableFilepath)) - ChromeFilepath := ChromePortableFilepath + ; Leave this in for now, but i don't think we will be going to using the system Chrome in the future. + ; if(FileExist("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")) + ; ChromeFilepath = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe - Message = Using Chromium Portable for Upload - SaveOrPostProgress(Message:=Message,PostType:="ErrorLoggingTextFile,DiscordErrorLogging") + ; if(FileExist("C:\Program Files\Google\Chrome\Application\chrome.exe")) + ; ChromeFilepath = C:\Program Files\Google\Chrome\Application\chrome.exe + } } - else { - MsgBox 0x10, Chromium Portable not Found, Chromium Portable not found. `nPlease make sure it's located at one of the following paths and then try again.`n`n%ChromePortableFilepath% `nOR`n%ChromePortableFilepath2% + ToolTip + } - ; Leave this in for now, but i don't think we will be going to using the system Chrome in the future. - ; if(FileExist("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe")) - ; ChromeFilepath = C:\Program Files (x86)\Google\Chrome\Application\chrome.exe + GetInstalledChromeVersion(){ - ; if(FileExist("C:\Program Files\Google\Chrome\Application\chrome.exe")) - ; ChromeFilepath = C:\Program Files\Google\Chrome\Application\chrome.exe + if(ChromeFilepath = ""){ + GetInstalledChromeFilepath() } - } - ToolTip -} + GetInstalledChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion + InstalledChromeVersion := RunCMD(GetInstalledChromeVersionCommand) -GetInstalledChromeVersion(){ + if(InStr(InstalledChromeVersion, "`r")) + InstalledChromeVersion := StrReplace(InstalledChromeVersion, "`r") ; replace any newline characters that powershell returns - if(ChromeFilepath = ""){ - GetInstalledChromeFilepath() + return InstalledChromeVersion } - GetInstalledChromeVersionCommand = powershell (Get-Item '%ChromeFilepath%').VersionInfo.ProductVersion - InstalledChromeVersion := RunCMD(GetInstalledChromeVersionCommand) - if(InStr(InstalledChromeVersion, "`r")) - InstalledChromeVersion := StrReplace(InstalledChromeVersion, "`r") ; replace any newline characters that powershell returns - - return InstalledChromeVersion -} + GetLatestChromeStableVersion(){ + ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints + json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json") -GetLatestChromeStableVersion(){ - ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints - json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json") - + ; converts json data variable into object + parsed := JSON.Load(json_str) - ; converts json data variable into object - parsed := JSON.Load(json_str) + try ChromeStableLatestVersion := parsed.channels.stable.version - try ChromeStableLatestVersion := parsed.channels.stable.version + if(ChromeStableLatestVersion = ""){ + return "Failed to parse json. " + } - if(ChromeStableLatestVersion = ""){ - return "Failed to parse json. " + return ChromeStableLatestVersion } - return ChromeStableLatestVersion -} + GetDownloadURLOfChromeAndDriver(InstalledChromeVersion){ + ; Will return the download URL of Chrome for Testing and Chrome Driver, seperated by a || -GetDownloadURLOfChromeAndDriver(InstalledChromeVersion){ - ; Will return the download URL of Chrome for Testing and Chrome Driver, seperated by a || + ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints + json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json") - ; https://github.com/GoogleChromeLabs/chrome-for-testing#json-api-endpoints - json_str := urldownloadtovar("https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json") - - ; requires #include of json.ahk in parent script - parsed := JSON.Load(json_str) + ; requires #include of json.ahk in parent script + parsed := JSON.Load(json_str) - ChromelabsJsonEntriesCount := parsed.versions.count() + ChromelabsJsonEntriesCount := parsed.versions.count() - if(ChromelabsJsonEntriesCount = ""){ - return "Failed to parse chromedriver json. " - } + if(ChromelabsJsonEntriesCount = ""){ + return "Failed to parse chromedriver json. " + } - loop % ChromelabsJsonEntriesCount { + loop % ChromelabsJsonEntriesCount { - if(A_index = ChromelabsJsonEntriesCount){ - Message = "Failed to find %InstalledChromeVersion% in the ChromeLabs Json" - return Message - } + if(A_index = ChromelabsJsonEntriesCount){ + Message = "Failed to find %InstalledChromeVersion% in the ChromeLabs Json" + return Message + } - if(InstalledChromeVersion = parsed.versions[A_Index].version){ + if(InstalledChromeVersion = parsed.versions[A_Index].version){ - VersionIndex := A_Index + VersionIndex := A_Index - ; loop through the platforms to get download URL for Chrome - loop % parsed.versions[VersionIndex].downloads.chrome.count() { - - if(parsed.versions[VersionIndex].downloads.chrome[A_Index].platform = "win64") - chromeDLURL64 := parsed.versions[VersionIndex].downloads.chrome[A_Index].url - } + ; loop through the platforms to get download URL for Chrome + loop % parsed.versions[VersionIndex].downloads.chrome.count() { + if(parsed.versions[VersionIndex].downloads.chrome[A_Index].platform = "win64") + chromeDLURL64 := parsed.versions[VersionIndex].downloads.chrome[A_Index].url + } - ; loop through the platforms to get download URL for Chromedriver - loop % parsed.versions[VersionIndex].downloads.chromedriver.count() { - - if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win64") - chromedriverDLURL64 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url - } - break + ; loop through the platforms to get download URL for Chromedriver + loop % parsed.versions[VersionIndex].downloads.chromedriver.count() { + + if(parsed.versions[VersionIndex].downloads.chromedriver[A_Index].platform = "win64") + chromedriverDLURL64 := parsed.versions[VersionIndex].downloads.chromedriver[A_Index].url + } + + break + } } - } - URLS = %chromeDLURL64%||%chromedriverDLURL64% - return URLS -} \ No newline at end of file + URLS = %chromeDLURL64%||%chromedriverDLURL64% + return URLS + } \ No newline at end of file From 2e0c81f34fa96f659153e5daba1e8be5ba28fd0c Mon Sep 17 00:00:00 2001 From: Yuriy Date: Wed, 1 May 2024 14:22:34 -0400 Subject: [PATCH 3/3] merged misc and general functions --- General-Functions.ahk | 209 +++++++++++++++++++++++++++++++++++- Miscellaneous-Functions.ahk | 158 --------------------------- 2 files changed, 208 insertions(+), 159 deletions(-) diff --git a/General-Functions.ahk b/General-Functions.ahk index 5f2ab8f..8f330bc 100644 --- a/General-Functions.ahk +++ b/General-Functions.ahk @@ -105,7 +105,7 @@ Func_LogErrorsToTextFile(Text){ ; Do not log if we do not have an errorlog filepath if(ErrorLoggingFilePath = "") - return + return ; ErrorLoggingFile := Filepath FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss @@ -425,3 +425,210 @@ HasSubstringVal(haystack, needle) +; -------------------------------Functions------------------------------- + +OnMsgBoxConfirmChromiumOverwrite() { + DetectHiddenWindows, On + Process, Exist + If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { + ControlSetText Button1, Cancel + ControlSetText Button2, Yes + } +} + +; https://www.autohotkey.com/docs/v2/Functions.htm#Variadic +Join(sep, params*) { + For index, param in params + str .= param . sep + return SubStr(str, 1, -StrLen(sep)) +} + + + +LogErrorToTextFile(Error){ +/* if(LogErrorsToTextFile != 1) + Return + */ + ErrorLoggingFile := VideoFolderDir . "\" . "ErrorLogging.txt" + FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss + text = + ( + + + ---------------%TodayDate%--------------- + %CurrentSite%: %Error% + + + ) + if(LogErrorsToMsgbox) + Msgbox % "Text: " Text + + FileAppend, %Text%, %ErrorLoggingFile% + } ; End of Function + +/* +*/ + +AddToTotalVideosUploadedCount(){ + ; IniRead, TotalVideosUploaded, %SettingsIniFilepath%, General, TotalVideosUploaded, %A_Space% + TotalVideosUploaded += 1 + ; IniWrite, %TotalVideosUploaded%, %SettingsIniFilepath%, General, TotalVideosUploaded +} + + + +OnMsgBoxPodcastFinish() { + DetectHiddenWindows, On + Process, Exist + If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { + WinMove,, 0 + } +} + +OnMsgBoxSocialMediaPoster() { + DetectHiddenWindows, On + Process, Exist + If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { + ControlSetText Button1, Yes + ControlSetText Button2, Not Now + } +} + + + +Check_For_Stuck_Video_Upload(Index_Number, Upload_Status){ + if(A_index = 1){ ; Create a blank array + ProgressStatusArray := [] + Return + } + + Message = Upload Status: %Upload_Status% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip") + + ; if we reached the last loop number: + if(A_index = %Number_of_loops_to_Check_Upload_status%){ + Message = Upload Most Likely Failed: Video Hasn't Finished Uploading after 1 hour. + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + SaveDriverURLOFErrorPage() + Return "Failed" + } + + ; If progress is still the same after a ten minute interval then error out + if(HasVal(Array_Index_Num_of_Upload_StatusChecks, A_index)){ ; if current index is in Array of index numbers to check status during + + ; Send a notification message of upload status + Message = Upload Status: %Upload_Status% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") + + ; if current upload_status is in the array of values that are updated every 10 mins + if(HasVal(ProgressStatusArray, Upload_Status)){ + Message = Upload Failed (E#4508)`nUpload Stuck at same point for 10 minutes. Stuck Status: %ProgressStatus% + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") + SaveDriverURLOFErrorPage() + Return "Failed" + } + ProgressStatusArray.Push(Upload_Status) ; append current status to array + } + } ; end of func + +/* +*/ +; Find the longest word in a string of words +FindLongestWordInString(m, calloutNumber, pos, haystack, pattern){ + Global wordLength, longestWord + + len := StrLen(m) + + If ( len > wordLength ) + { + wordLength := len + longestWord := m + + ; MsgBox, %m% + } +} + +/* +*/ + +; Split a long string into multiple pieces based on the string lengtht you want and return an array with all strings +SplitStringWithSentences(text, maxChunkSize) { + sentences := StrSplit(text, "\. ") ; split at period with space after it + + currentChunk := "" + chunks := [] + + Loop, % sentences.Length() + { + sentence := sentences[A_Index] + CurrentChunkAndSentence := currentChunk . sentence + ; Msgbox % "CurrentChunkAndSentence: " CurrentChunkAndSentence + ; msgbox % StrLen(CurrentChunkAndSentence) + + if (StrLen(CurrentChunkAndSentence) <= maxChunkSize) + { + currentChunk .= sentence . "\. " + } + else + { + chunks.Push(currentChunk) + currentChunk := sentence . "\. " + } + } + + if (currentChunk != "") + chunks.Push(currentChunk) + + return chunks +} + + + +CreateErrorLoggingFiles(Directory := ""){ + FormatTime, TodayDate, YYYYMMDDHH24MISS, yyyyMMdd_hhmmss + + ErrorLoggingDirectory := Directory + + if(ErrorLoggingDirectory = ""){ + ErrorLoggingDirectory := A_ScriptDir . "\Lib\ErrorLogging" + ; If directory for error logging doesn't exist, create it + if(!FileExist(ErrorLoggingDirectory)) + FileCreateDir, %ErrorLoggingDirectory% + + ErrorLoggingDirectory := ErrorLoggingDirectory . "\" . TodayDate . "_" . ScriptAbbreviatedName + } + ; Msgbox % "ErrorLoggingDirectory: " ErrorLoggingDirectory + FileCreateDir, %ErrorLoggingDirectory% + + ; Create variables with filepaths that content will be saved to. + global ErrorLoggingFilePath := ErrorLoggingDirectory . "\" . "ErrorLogging.txt" + global StatusFileFilePath := ErrorLoggingDirectory . "\" . "PostStatus.ini" + + global PostTitleFilePath := ErrorLoggingDirectory . "\" . "PostTitle.txt" + global PostBodyFilePath := ErrorLoggingDirectory . "\" . "PostBody.txt" + global PostTagsFilePath := ErrorLoggingDirectory . "\" . "PostTags.txt" + + PostStatusesFilepath := ErrorLoggingDirectory . "\" . "Status.ini" + ; Msgbox % "ErrorLoggingDirectory: " ErrorLoggingDirectory + + + Return +} + + + + +; if passed argument is .exe file, then script has just been updated and we need to delete the old .exe file +CheckIfUpdateInstalled(PassedInArgument :=""){ + + if(InStr(PassedInArgument,".exe")){ + + FileDelete, %PassedInArgument% + if(ErrorLevel){ ; most likely because the old version hasn't finished exiting yet + Message = Failed to Delete Old .exe file after Updating + SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar") + } + + +} +} \ No newline at end of file diff --git a/Miscellaneous-Functions.ahk b/Miscellaneous-Functions.ahk index 628dda7..e69de29 100644 --- a/Miscellaneous-Functions.ahk +++ b/Miscellaneous-Functions.ahk @@ -1,158 +0,0 @@ - -; -------------------------------Functions------------------------------- - -OnMsgBoxConfirmChromiumOverwrite() { - DetectHiddenWindows, On - Process, Exist - If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { - ControlSetText Button1, Cancel - ControlSetText Button2, Yes - } -} - -; https://www.autohotkey.com/docs/v2/Functions.htm#Variadic -Join(sep, params*) { - For index, param in params - str .= param . sep - return SubStr(str, 1, -StrLen(sep)) -} - - - -LogErrorToTextFile(Error){ -/* if(LogErrorsToTextFile != 1) - Return - */ - ErrorLoggingFile := VideoFolderDir . "\" . "ErrorLogging.txt" - FormatTime, TodayDate , YYYYMMDDHH24MISS, yyyyMMdd_hhmmss - text = - ( - - - ---------------%TodayDate%--------------- - %CurrentSite%: %Error% - - - ) - if(LogErrorsToMsgbox) - Msgbox % "Text: " Text - - FileAppend, %Text%, %ErrorLoggingFile% - } ; End of Function - -/* -*/ - -AddToTotalVideosUploadedCount(){ - ; IniRead, TotalVideosUploaded, %SettingsIniFilepath%, General, TotalVideosUploaded, %A_Space% - TotalVideosUploaded += 1 - ; IniWrite, %TotalVideosUploaded%, %SettingsIniFilepath%, General, TotalVideosUploaded -} - - - -OnMsgBoxPodcastFinish() { - DetectHiddenWindows, On - Process, Exist - If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { - WinMove,, 0 - } -} - -OnMsgBoxSocialMediaPoster() { - DetectHiddenWindows, On - Process, Exist - If (WinExist("ahk_class #32770 ahk_pid " . ErrorLevel)) { - ControlSetText Button1, Yes - ControlSetText Button2, Not Now - } -} - - - -Check_For_Stuck_Video_Upload(Index_Number, Upload_Status){ - if(A_index = 1){ ; Create a blank array - ProgressStatusArray := [] - Return - } - - Message = Upload Status: %Upload_Status% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip") - - ; if we reached the last loop number: - if(A_index = %Number_of_loops_to_Check_Upload_status%){ - Message = Upload Most Likely Failed: Video Hasn't Finished Uploading after 1 hour. - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - SaveDriverURLOFErrorPage() - Return "Failed" - } - - ; If progress is still the same after a ten minute interval then error out - if(HasVal(Array_Index_Num_of_Upload_StatusChecks, A_index)){ ; if current index is in Array of index numbers to check status during - - ; Send a notification message of upload status - Message = Upload Status: %Upload_Status% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,DiscordErrorLogging") - - ; if current upload_status is in the array of values that are updated every 10 mins - if(HasVal(ProgressStatusArray, Upload_Status)){ - Message = Upload Failed (E#4508)`nUpload Stuck at same point for 10 minutes. Stuck Status: %ProgressStatus% - SaveOrPostProgress(Message:=Message,PostType:="Tooltip,ErrorLoggingTextFile,ErrorSummaryVar,DiscordErrorLogging") - SaveDriverURLOFErrorPage() - Return "Failed" - } - ProgressStatusArray.Push(Upload_Status) ; append current status to array - } - } ; end of func - -/* -*/ -; Find the longest word in a string of words -FindLongestWordInString(m, calloutNumber, pos, haystack, pattern){ - Global wordLength, longestWord - - len := StrLen(m) - - If ( len > wordLength ) - { - wordLength := len - longestWord := m - - ; MsgBox, %m% - } -} - -/* -*/ - -; Split a long string into multiple pieces based on the string lengtht you want and return an array with all strings -SplitStringWithSentences(text, maxChunkSize) { - sentences := StrSplit(text, "\. ") ; split at period with space after it - - currentChunk := "" - chunks := [] - - Loop, % sentences.Length() - { - sentence := sentences[A_Index] - CurrentChunkAndSentence := currentChunk . sentence - ; Msgbox % "CurrentChunkAndSentence: " CurrentChunkAndSentence - ; msgbox % StrLen(CurrentChunkAndSentence) - - if (StrLen(CurrentChunkAndSentence) <= maxChunkSize) - { - currentChunk .= sentence . "\. " - } - else - { - chunks.Push(currentChunk) - currentChunk := sentence . "\. " - } - } - - if (currentChunk != "") - chunks.Push(currentChunk) - - return chunks -} -