; =================================================================== ; Twitch.au3 - UDF ; v0.2 ; ; By: Matthäus "Draien" Häußler ; Last Updated: 24.02.2015 ; Tested with AutoIt Version 3.3.12.0 ; ; Discription: Paste your twitch client ID into the global const ; ; Special Thanks: ; "FaridAgl" - For the HTTPGet / HTTPPost (soon to come) function ; ; Main functions: ; _KRAKENINFO ; =================================================================== #include-once Global Const $HTTP_STATUS_OK = 200 Global Const $KRAKEN_URL = "https://api.twitch.tv/kraken/" Global Const $CLIENT_ID = "xxxxxx" ;Your Client ID ; #FUNCTION# ==================================================================================================================== ; Name ..........: _KRAKENINFO ; Description ...: Function to grab information from the KRAKEN/Twitch API ; Syntax ........: _KRAKENINFO($sChannel) ; Parameters ....: $sChannel - Name of the Stream/Channel to check as string ; Return values .: Successful - Returns an array with information about the stream/channel ; $aINFO[0] = Status (1, if online ; 0, if offline) ; $aINFO[1] = Displayname (String) ; $aINFO[2] = Game (String) ; $aINFO[3] = Title (String) ; $aINFO[4] = Followercount (Number) ; $aINFO[5] = Total Views (Number) ; $aINFO[6] = Live-Viewercount (Number, if online ; empty, if offline) ; Failure - Returns 0 and sets @error: ; |1 = Invalid Channel, sets @extended: (1, if empty; 2, if whitespace is found) ; |2 = Problem with _HTTPGet ; Author ........: Draien ; ================================================================================================================================ Func _KRAKENINFO($sChannel) Local $aINFO[7] Local $iEND = ',' Select ;Parameter Checking, Trust No One Case $sChannel = "" Return SetError(1, 1, 0) Case StringInStr($sChannel," ") Return SetError(1, 2, 0) EndSelect $sGet = _HTTPGet($KRAKEN_URL & "streams/" & $sChannel) If (@error) Then Return SetError(2, 0, 0) If StringInStr($sGet,'"stream":null') Then $sGet = _HTTPGet($KRAKEN_URL & "channels/" & $sChannel) $aINFO[0] = 0 Else $aINFO[0] = 1 $aINFO[6] = '"viewers":' EndIf $aINFO[1] = '"display_name":"' $aINFO[2] = '"game":"' $aINFO[3] = '"status":"' $aINFO[4] = '"followers":' $aINFO[5] = '"views":' For $x = 1 To UBound($aINFO) -1 If $aINFO[$x] <> "" Then ;Avoid searching for live-viewers when offline $iINFOs = StringInStr($sGet,$aINFO[$x])+StringLen($aINFO[$x]) ;Total start position for a value $iINFOe = StringInStr($sGet,$iEND,0,1,$iINFOs) ;Total end position for a value $aINFO[$x] = StringMid($sGet,$iINFOs,$iINFOe-$iINFOs) EndIf Next ;Last Stripping of unecessary things $aINFO[1] = StringTrimRight($aINFO[1],1) $aINFO[2] = StringTrimRight($aINFO[2],1) $aINFO[3] = StringTrimRight($aINFO[3],1) Return $aINFO EndFunc ;==>_KRAKENINFO ; #FUNCTION# ==================================================================================================================== ; Name ..........: _HTTPGet ; Description ...: Get function to interact with Kraken / Twitch-API ; Syntax ........: _HTTPGet($sData) ; Parameters ....: $sData - Data to get as string. ; Return values .: Success - Returns ResponseText ; Failure - Returns 0 and sets @error ; Author ........: FaridAgl ; Modified ......: - ; Remarks .......: ; Related .......: _HTTPPost ; Link ..........: ; Example .......: No ; ================================================================================================================================ Func _HTTPGet($sURL, $sData = "") Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1") $oHTTP.Open("GET", $sURL & "?" & $sData, False) If (@error) Then Return SetError(1, 0, 0) $oHTTP.SetRequestHeader("Client-ID: " & $CLIENT_ID) $oHTTP.Send() If (@error) Then Return SetError(2, 0, 0) If ($oHTTP.Status <> $HTTP_STATUS_OK) Then Return SetError(3, 0, 0) Return SetError(0, 0, $oHTTP.ResponseText) EndFunc