Jump to content

Twitch Watch


Damein
 Share

Recommended Posts

I know I've made a few of these and there are quite a few out there already but just wanted to throw up a new one I did.

Special thanks to Alexander Samuelsson AKA AdmiralAlkex for the JSON/Data Retrieve Sections as found in his version Forum Topic

 

I wanted something that I could choose between Livestreamer or Browser when watching and also the ability to launch chat too (If I'm using Livestreamer) because I also use Hex Chat for that as well. Cleaned up the tray menus to my liking and voila. Thanks again and enjoy!

 

GitHub

YouTube Demo

 

Source

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Imgs\WatchIcon.ico
#AutoIt3Wrapper_Compression=0
#AutoIt3Wrapper_Res_Fileversion=1.3.0.0
#AutoIt3Wrapper_Res_LegalCopyright=R.S.S.
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Add_Constants=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
; *** Start added by AutoIt3Wrapper ***
#include <StructureConstants.au3>
; *** End added by AutoIt3Wrapper ***

#cs ----------------------------------------------------------------------------

    AutoIt Version: 3.3.12.0 (Stable)
    Author:         Damein / Special Thanks to Alexander Samuelsson AKA AdmiralAlkex for the JSON/Data Retrieve Sections

    Script Function:
    Stuff

#ce ----------------------------------------------------------------------------

#include "Json.au3"
#include <Date.au3>
#include <Array.au3>
#include <InetConstants.au3>
#include <TrayConstants.au3>

Opt("TrayMenuMode", 3)
Opt("TrayOnEventMode", 1)
Opt("WinTitleMatchMode", 2)

$iPrintJSON = IniRead(@ScriptDir & "\Data/Settings.ini", "Section", "PrintJSON", "") ;PRINT ON JSON

Global $sUsername, $LiveStreamsMenu, $FirstRun = 0, $LiveStreamData
Global Enum $eTwitch
Global Enum $eDisplayName, $eUrl, $ePreview, $eGame, $eCreated, $eTrayId, $eStatus, $eTime, $eOnline, $eService, $eMax
Global $aStreams[0][$eMax]

Dim $LiveStreams[4000][4000]

$TrayOptionsMenu = TrayCreateMenu("Options")
$TrayOptionsSetUsername = TrayCreateItem("Set Username...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_SetUsername")
$TrayOptionsRefresh = TrayCreateItem("Refresh...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_Refresh")
$TrayOptionsLiveStreamer = TrayCreateItem("Use Livestreamer...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_LiveStreamer")
$TrayOptionsUseBrowser = TrayCreateItem("Use Browser...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_Browser")
$TrayOptionsQuality = TrayCreateItem("Set Quality...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_Quality")
$TrayExit = TrayCreateItem("Exit...", $TrayOptionsMenu)
TrayItemSetOnEvent(-1, "_Exit")

TrayCreateItem("")

$ChatOptionsMenu = TrayCreateMenu("Chat Options...")
$TrayOptionsChat = TrayCreateItem("Load Chat...", $ChatOptionsMenu)
TrayItemSetOnEvent(-1, "_LoadChat")
$TrayOptionsChatBrowser = TrayCreateItem("Use Browser...", $ChatOptionsMenu)
TrayItemSetOnEvent(-1, "_UseBrowser")
$TrayOptionsChatHex = TrayCreateItem("Use Hex...", $ChatOptionsMenu)
TrayItemSetOnEvent(-1, "_UseHex")


$ReadFormat = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Format", "NA")
If $ReadFormat = "Livestreamer" Then
    TrayItemSetState($TrayOptionsUseBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsLiveStreamer, $TRAY_CHECKED)
ElseIf $ReadFormat = "Browser" Then
    TrayItemSetState($TrayOptionsLiveStreamer, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsUseBrowser, $TRAY_CHECKED)
EndIf

$ReadChat = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Chat", "NA")
If $ReadChat = "1" Then
    TrayItemSetState($TrayOptionsChat, $TRAY_CHECKED)
ElseIf $ReadFormat = "0" Then
    TrayItemSetState($TrayOptionsChat, $TRAY_UNCHECKED)
EndIf

$ReadChatType = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "ChatType", "NA")
If $ReadChatType = "Browser" Then
    TrayItemSetState($TrayOptionsChatBrowser, $TRAY_CHECKED)
    TrayItemSetState($TrayOptionsChatHex, $TRAY_UNCHECKED)
ElseIf $ReadChatType = "Hex" Then
    TrayItemSetState($TrayOptionsChatBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsChatHex, $TRAY_CHECKED)
EndIf


$sUsername = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Username", "NA")
If $sUsername = "NA" Or "" Then
    MsgBox(48, "Error", "Make sure to set a username from the tray menu before trying to search streams!")
Else
    TrayTip("Updating...", "Getting live streams...", 2000)
    _TwitchGet($sUsername)
EndIf

Func _TwitchGet($sUsername)
    Dim $LiveStreams[4000][4000]
    Global $aStreams[0][$eMax]
    $iLimit = 100
    $iOffset = 0
    $sQuotedUsername = URLEncode($sUsername)
    $sBaseUrl = "https://api.twitch.tv/kraken/users/" & $sQuotedUsername & "/follows/channels"

    While True
        $sUrl = $sBaseUrl & OPTIONS_OFFSET_LIMIT_TWITCH($iOffset, $iLimit)
        $avTemp = FetchItems($sUrl, "follows")
        If UBound($avTemp) = 0 Then ExitLoop

        Local $sOptions
        For $iX = 0 To UBound($avTemp) - 1
            $oChannel = Json_ObjGet($avTemp[$iX], "channel")
            $sName = Json_ObjGet($oChannel, "name")
            $sOptions &= $sName & ','
        Next

        $sOptions = StringTrimRight($sOptions, 1)
        $sUrl = 'https://api.twitch.tv/kraken/streams?channel=' & $sOptions & '&limit=' & $iLimit
        $oChannel = FetchItems($sUrl, "streams")

        For $iX = 0 To UBound($oChannel) - 1
            $oChannel2 = Json_ObjGet($oChannel[$iX], "channel")
            $sUrl = Json_ObjGet($oChannel2, "url")
            If $sUrl = "" Then $sUrl = "http://www.twitch.tv/" & Json_ObjGet($oChannel2, "name")

            $sDisplayName = Json_ObjGet($oChannel2, "display_name")

            $sStatus = Json_ObjGet($oChannel2, "status")

            $oPreview = Json_ObjGet($oChannel[$iX], "preview")
            $sMedium = Json_ObjGet($oPreview, "medium")

            $sGame = Json_ObjGet($oChannel[$iX], "game")

            $sCreated = Json_ObjGet($oChannel[$iX], "created_at")

            $asSplit = StringSplit($sCreated, "T")
            $asDate = StringSplit($asSplit[1], "-")
            $asTime = StringSplit(StringTrimRight($asSplit[2], 1), ":")

            $tSystemTime = DllStructCreate($tagSYSTEMTIME)
            $tSystemTime.Year = $asDate[1]
            $tSystemTime.Month = $asDate[2]
            $tSystemTime.Day = $asDate[3]
            $tSystemTime.Hour = $asTime[1]
            $tSystemTime.Minute = $asTime[2]
            $tSystemTime.Second = $asTime[3]

            $tFileTime = _Date_Time_SystemTimeToFileTime($tSystemTime)
            $tLocalTime = _Date_Time_FileTimeToLocalFileTime($tFileTime)
            $sTime = _Date_Time_FileTimeToStr($tLocalTime, 1)
            $iHours = _DateDiff("h", $sTime, _NowCalc())
            $iMinutes = _DateDiff("n", $sTime, _NowCalc())
            $iMinutes -= $iHours * 60

            $sTime = StringFormat("%02i:%02i", $iHours, $iMinutes)




            _StreamSet($sDisplayName, $sUrl, $sMedium, $sGame, $sCreated, $sTime, $sStatus, $eTwitch)
        Next

        If UBound($oChannel) <= 100 Then ExitLoop

        $iOffset += $iLimit
    WEnd
    _WriteStreams()
    Return "Potato on a Stick"

EndFunc   ;==>_TwitchGet

Func FetchItems($sUrl, $sKey, $sExtendedKey = Null)
    Local $sRetExtended

    $oJSON = getJson($sUrl)

    If IsObj($oJSON) = False Then Return ""

    If IsString($sExtendedKey) Then
        $sRetExtended = Json_ObjGet($oJSON, $sExtendedKey)
    EndIf

    $aFollows = Json_ObjGet($oJSON, $sKey)
    If UBound($aFollows) > 0 Then
        Return SetExtended($sRetExtended, $aFollows)
    Else
        Return SetExtended($sRetExtended, "")
    EndIf
EndFunc   ;==>FetchItems

Func _StreamSet($sDisplayName, $sUrl, $sThumbnail, $sGame, $sCreated, $sTime, $sStatus, $iService)
    ConsoleWrite("Found streamer: " & $sDisplayName & @CRLF)

    For $iIndex = 0 To UBound($aStreams) - 1
        If $aStreams[$iIndex][$eUrl] = $sUrl Then ExitLoop
    Next
    If $iIndex = UBound($aStreams) Then
        ReDim $aStreams[$iIndex + 1][$eMax]
    EndIf

    $aStreams[$iIndex][$eDisplayName] = $sDisplayName
    $aStreams[$iIndex][$eUrl] = $sUrl
    $aStreams[$iIndex][$ePreview] = $sThumbnail
    $aStreams[$iIndex][$eGame] = $sGame
    $aStreams[$iIndex][$eCreated] = $sCreated
    $aStreams[$iIndex][$eTime] = $sTime
    $aStreams[$iIndex][$eStatus] = $sStatus
    $aStreams[$iIndex][$eOnline] = True
    $aStreams[$iIndex][$eService] = $iService
EndFunc   ;==>_StreamSet

Func OPTIONS_OFFSET_LIMIT_TWITCH($iOffset, $iLimit)
    Return '?offset=' & $iOffset & '&limit=' & $iLimit
EndFunc   ;==>OPTIONS_OFFSET_LIMIT_TWITCH

Func getJson($sUrl)
    $dJsonString = InetRead($sUrl, $INET_FORCERELOAD)

    If $iPrintJSON Then ConsoleWrite(BinaryToString($dJsonString) & @CRLF)

    $oJSON = Json_Decode(BinaryToString($dJsonString))
    Return $oJSON
EndFunc   ;==>getJson

Func URLEncode($urlText)
    $url = ""
    For $i = 1 To StringLen($urlText)
        $acode = Asc(StringMid($urlText, $i, 1))
        Select
            Case ($acode >= 48 And $acode <= 57) Or _
                    ($acode >= 65 And $acode <= 90) Or _
                    ($acode >= 97 And $acode <= 122)
                $url = $url & StringMid($urlText, $i, 1)
            Case $acode = 32
                $url = $url & "+"
            Case Else
                $url = $url & "%" & Hex($acode, 2)
        EndSelect
    Next
    Return $url
EndFunc   ;==>URLEncode

Func _WriteStreams()

    FileDelete(@ScriptDir & "/Data/Data.ini")
    FileCopy(@ScriptDir & "/Data/Default Data.ini", @ScriptDir & "/Data/Data.ini")

    For $i = 0 To UBound($aStreams) - 1
        IniWrite(@ScriptDir & "/Data/Data.ini", "Live", $aStreams[$i][0], $aStreams[$i][1] & "|" & $aStreams[$i][3])
    Next
    _SetTray()
EndFunc   ;==>_WriteStreams

Func _Refresh()
    TrayTip("Updating...", "Getting live streams...", 2000)
    _TwitchGet($sUsername)
EndFunc   ;==>_Refresh

Func _SetTray()
    TrayItemDelete($LiveStreamsMenu)
    If $FirstRun = 1 Then
        For $i = 1 To UBound($LiveStreamData) - 1
            TrayItemDelete($LiveStreams[$i][$eTrayId])
        Next
    EndIf
    $LiveStreamsMenu = TrayCreateMenu("Live Streams")
    $LiveStreamData = IniReadSection(@ScriptDir & "/Data/Data.ini", "Live")
    For $i = 1 To UBound($LiveStreamData) - 1
        $SplitData = StringSplit($LiveStreamData[$i][1], "|")
        $LiveStreams[$i][$eTrayId] = TrayCreateItem($LiveStreamData[$i][0] & " - " & $SplitData[2], $LiveStreamsMenu)
        TrayItemSetOnEvent(-1, "_RunStream")
    Next
    TrayTip("Updating...", "Update complete!", 2000)
    $FirstRun = 1
EndFunc   ;==>_SetTray

Func _RunStream()
    $Tray = @TRAY_ID
    $Tray = TrayItemGetText($Tray)
    $Split = StringSplit($Tray, "-")
    $Tray = StringStripWS($Split[1], 8)

    $CheckVideoSource = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Format", "NA")
    If $CheckVideoSource = "NA" Then
        MsgBox(48, "Error", "No format has been set, please do so in the options menu!")
    ElseIf $CheckVideoSource = "Livestreamer" Then
        $CheckVideoQuality = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Quality", "NA")
        Run("livestreamer" & " twitch.tv/" & $Tray & " " & $CheckVideoQuality)
        Sleep(1000)
    ElseIf $CheckVideoSource = "Browser" Then
        ShellExecute("https://www.twitch.tv/" & $Tray)
        Sleep(1000)
    EndIf

    $ReadChat = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Chat", "NA")
    $ReadChatType = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "ChatType", "NA")
    If $ReadChat = "1" Then
        If $ReadChatType = "Browser" Then
            ShellExecute("https://www.twitch.tv/" & $Tray & "/chat?popout=")
        ElseIf $ReadChatType = "Hex" Then
            WinActivate("HexChat:")
            Sleep(100)
            Send("/join {#}" & $Tray)
            Sleep(100)
            Send("{ENTER}")
        EndIf
    EndIf
EndFunc   ;==>_RunStream


Func _SetUsername()
    $InputUsername = InputBox("Username...", "Please type out your username only. No Twitch.tv is needed!")
    If @error = 1 Then
        MsgBox(48, "Cancelled!", "You have cancelled this operation!")
    Else
        IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Username", $InputUsername)
        $sUsername = $InputUsername
        MsgBox(0, "Username...", "Username: " & $InputUsername & " is set!")
    EndIf
EndFunc   ;==>_SetUsername

Func _LiveStreamer()
    TrayItemSetState($TrayOptionsLiveStreamer, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsUseBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsLiveStreamer, $TRAY_CHECKED)
    MsgBox(0, "Video Source", "Now using Livestreamer by default!")
    IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Format", "Livestreamer")
EndFunc   ;==>_LiveStreamer

Func _Browser()
    TrayItemSetState($TrayOptionsLiveStreamer, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsUseBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsUseBrowser, $TRAY_CHECKED)
    MsgBox(0, "Video Source", "Now using default Browser by default!")
    IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Format", "Browser")
EndFunc   ;==>_Browser

Func _Quality()
    $InputQuality = InputBox("Quality Selection", "Please input a quality for Livestreamer" & @CRLF & "Mobile" & @CRLF & "Low" & @CRLF & "Medium" & @CRLF & "High" & @CRLF & "Source")
    If @error = 1 Then
        MsgBox(48, "Cancelled!", "You have cancelled this operation!")
    Else
        IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Quality", $InputQuality)
        MsgBox(0, "Quality Selection", "Quality: " & $InputQuality & " has been set for Livestreamer use!")
    EndIf
EndFunc   ;==>_Quality

Func _UseBrowser()
    TrayItemSetState($TrayOptionsChatBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsChatHex, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsChatBrowser, $TRAY_CHECKED)
    IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "ChatType", "Browser")
EndFunc   ;==>_UseBrowser

Func _UseHex()
    TrayItemSetState($TrayOptionsChatBrowser, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsChatHex, $TRAY_UNCHECKED)
    TrayItemSetState($TrayOptionsChatHex, $TRAY_CHECKED)
    IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "ChatType", "Hex")
EndFunc   ;==>_UseHex


Func _LoadChat()
    $ReadChat = IniRead(@ScriptDir & "/Data/Settings.ini", "Section", "Chat", "NA")
    If $ReadChat = "1" Then
        TrayItemSetState($TrayOptionsChat, $TRAY_UNCHECKED)
        IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Chat", "0")
    ElseIf $ReadChat = 0 Then
        TrayItemSetState($TrayOptionsChat, $TRAY_CHECKED)
        IniWrite(@ScriptDir & "/Data/Settings.ini", "Section", "Chat", "1")
    EndIf
EndFunc   ;==>_LoadChat


Func _Exit()
    Exit
EndFunc   ;==>_Exit

While 1
    Sleep(10)
WEnd

 

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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