Jump to content

Recommended Posts

Posted (edited)

Hello,

I am currently in the progress of writing a Twitch UDF to interact with the API (KRAKEN).

This is my first UDF so please point out any errors you find :)

Special thanks to FaridAgl for his functions >here

Before using this UDF, make sure to paste your client-ID into the UDF from here

Current version: 0.2 - 24.02.2015

UDF source:

; ===================================================================
; 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 

Example:

#include "Twitch.au3"
#include <Array.au3>

$sGet = _KRAKENINFO("syndicate")
_ArrayDisplay($sGet)

Function overview:

_KRAKENINFO

Changelog

 

v0.2: - Added Client-ID to HTTP-Header as per required from twitch.tv

          - Added total views to streaming info

          - Title/Game/... now available when stream is offline

 

I'd appreciate any feedback :)

Twitch.au3

Edited by draien
  • 1 year later...
Posted (edited)

For folks that attempt to use this UDF.

Change:

$oHTTP.SetRequestHeader("Client-ID: " & $CLIENT_ID)

To: 

$oHTTP.SetRequestHeader("Client-ID", $CLIENT_ID)

(FYI I am using Windows 10, not sure if the original version works on lower versions of windows.)

 

Edited by Roue

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...