Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #SingleInstance, Force ; Allow only one instance of the script to run
- #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
- #Persistent ; Keep the script 'alive' in the tray
- productName := "twiz AntiIdle"
- clientIdentifier := A_ComputerName
- iniFile := A_ScriptDir "\PlexAntiIdle.ini"
- Gosub, ReadINI
- If !FileExist(iniFile) {
- Gosub, WriteINI
- }
- If PlexToken {
- IsValid := VerifyToken(PlexToken)
- }
- If !IsValid {
- Gosub, GeneratePin
- }
- ;Multiply the value by 60000 to use minutes (1,000ms = 1sec, 60,0000ms = 60 sec)
- UpdateTimer := 60000 * UpdateTimer
- MaxIdleTime := 60000 * MaxIdleTime
- PlexURL := "http://" PlexIP ":" PlexPort "/status/sessions?X-Plex-Token=" PlexToken
- ; Create a repeating Timer to check if Idle
- SetTimer, CheckIdle, %UpdateTimer%
- Return
- ReadINI:
- iniRead, UpdateTimer, %iniFile%, Plex, UpdateTimer, 5
- iniRead, MaxIdleTime, %iniFile%, Plex, MaxIdleTime, 10
- iniRead, PlexIP, %iniFile%, Plex, PlexIP, 127.0.0.1
- iniRead, PlexPort, %iniFile%, Plex, PlexPort, 32400
- iniRead, PlexToken, %iniFile%, Plex, PlexToken, %A_Space%
- Return
- WriteINI:
- iniWrite, %UpdateTimer%, %iniFile%, Plex, UpdateTimer
- iniWrite, %MaxIdleTime%, %iniFile%, Plex, MaxIdleTime
- iniWrite, %PlexIP%, %iniFile%, Plex, PlexIP
- iniWrite, %PlexPort%, %iniFile%, Plex, PlexPort
- Return
- CheckIdle:
- ; Check if host PC has been idle more than X minutes
- If (A_TimeIdle > MaxIdleTime) {
- UserCount := ""
- ; Try to send an HTTP request to the Plex Server
- Try {
- hObject := ComObjCreate("WinHttp.WinHttpRequest.5.1")
- hObject.Open("GET", PlexURL)
- hObject.Send()
- Response := hObject.ResponseText
- ; Catch any errors, and exit if there is one
- } Catch e {
- MsgBox, 16, %A_ScriptName% - Error, % e.Message ; Display the HTTP error message
- ExitApp
- }
- ; Get the number of connected users if the response contains the word "MediaContainer"
- If InStr(Response, "MediaContainer") {
- UserCount := RegExReplace(Response, "is).*<MediaContainer size=""(\d+)"">.*", "$1")
- If (UserCount > 0) {
- ; If the Plex server has connected users, we reset the computers idle time
- ; https://msdn.microsoft.com/en-us/library/aa373208(VS.85).aspx
- DllCall("SetThreadExecutionState", "Int", 0x00000001)
- }
- ; If the response contains the word "Unauthorized", display this message and exit
- } Else If InStr(Response, "Unauthorized") {
- ;RegExReplace(Response, "is).*<body>.*?<h1>(.*?)</h1>.*?</body>.*" "$1")
- Gosub, CheckPin
- ; MsgBox,, Unauthorized, %A_Now%: Unauthorized.`n`nIt appears your Plex Token is invalid.`nPlease check that your Plex Token is correct and run the app again.
- ;}
- } Else { ; If the response doesn't contain either of the above, then us know
- Gosub, WriteLog
- MsgBox,, Unknown Response, %A_Now%: Unknown response from server.
- }
- }
- Return
- ; https://forums.plex.tv/t/authenticating-with-plex/609370
- VerifyToken(PlexToken) {
- Global productName, clientIdentifier
- cmdStr := "curl -X GET https://plex.tv/api/v2/user "
- . " -H ""accept: application/json"""
- . " -d ""X-Plex-Product=" productName """"
- . " -d ""X-Plex-Client-Identifier=" clientIdentifier """"
- . " -d ""X-Plex-Token=" PlexToken """"
- Response := RunCMD(cmdStr)
- If InStr(Response, """errors"":") {
- HTTPStatus := RegExReplace(Response, ".*""status"":(.*?)[}\]].*", "$1")
- If (HTTPStatus = 401) {
- ;Gosub, WriteLog
- MsgBox, %HTTPStatus%: Access Token is invalid.`n%PlexToken%
- Return 0
- } Else {
- Gosub, WriteLog
- MsgBox, Unknown status code: %HTTPStatus%
- Return
- }
- }
- Return 1
- }
- GeneratePin:
- ThisStep := A_ThisLabel
- cmdStr := "curl -X POST https://plex.tv/api/v2/pins "
- . " -H ""accept: application/json"""
- . " -d ""strong=true"""
- . " -d ""X-Plex-Product=" productName """"
- . " -d ""X-Plex-Client-Identifier=" clientIdentifier """"
- Response := RunCMD(cmdStr)
- RegExMatch(Response, "id"":(.*?),""code"":""(.*?)""", PinValue)
- If !PinValue1 {
- Gosub, WriteLog
- MsgBox, Error generating PIN.
- Return
- }
- Gosub, ConstructAuthUrl
- Return
- ConstructAuthUrl:
- ThisStep := A_ThisLabel
- AuthURL := "https://app.plex.tv/auth#?"
- . "clientID=" clientIdentifier
- . "&code=" PinValue2
- . "&context%5Bdevice%5D%5Bproduct%5D=" productName
- Run, %AuthURL%
- Sleep, 250
- MsgBox, Please keep this window open until you log into Plex, then click OK to continue.
- Gosub, CheckPin
- Return
- CheckPin:
- ThisStep := A_ThisLabel
- cmdStr := "curl -X GET https://plex.tv/api/v2/pins/" PinValue1
- . " -H ""accept: application/json"""
- . " -d ""code=" PinValue2 """"
- . " -d ""X-Plex-Client-Identifier=" clientIdentifier """"
- Response := RunCMD(cmdStr)
- PlexToken := RegExReplace(Response, ".*?""authToken"":""(.*?)"".*", "$1")
- If VerifyToken(PlexToken) {
- ;iniWrite, %PlexID%, %iniFile%, Plex, PinValue1
- ;iniWrite, %PlexCode%, %iniFile%, Plex, PinValue2
- iniWrite, %PlexToken%, %iniFile%, Plex, PlexToken
- } Else {
- Gosub, WriteLog
- Msgbox, Invalid Access Token generated.
- ExitApp
- }
- Return
- RunCMD(Command) {
- DHW := A_DetectHiddenWindows
- DetectHiddenWindows, On
- Run, %ComSpec% /k,, Hide, PID
- WinWait, ahk_pid %PID%,, 10000
- DetectHiddenWindows, %DHW%
- DllCall("AttachConsole", "UInt", PID)
- WshShell := ComObjCreate("WScript.Shell")
- exec := WshShell.Exec(Command)
- output := exec.StdOut.ReadAll()
- DllCall("FreeConsole")
- Process, Close, %pid%
- Return %output%
- }
- WriteLog:
- FileAppend, % "[" A_Now "]`t" ThisStep "`t" PinValue1 "`t" PinValue2 "`n" AuthURL "`n" Response "`n`n", %A_ScriptDir%\ErrorLog.txt
- Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement