-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
214 lines (183 loc) · 5.27 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bufio"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/dolmen-go/kittyimg"
"github.com/nicklaw5/helix/v2"
)
func errhandle(err error) {
if err != nil {
log.Fatal(err)
}
}
func checkTwitchCLI() {
fmt.Println("Checking for Twitch-cli")
_, err := exec.LookPath("twitch")
if err != nil {
fmt.Printf("didn't find 'twitch' or 'twitch-cli' executable\n")
}
}
func getCredentials() (cid, secret, access, refresh string) {
scanner := bufio.NewScanner(os.Stdin)
home := os.Getenv("HOME")
twitchConfig := (home + "/.config/twitch-cli/.twitch-cli.env")
if _, err := os.Stat(twitchConfig); err == nil {
fmt.Println("Twitch credentials found")
} else {
fmt.Println("Twitch credentials not found, please Enter ClientID")
for scanner.Scan() {
cid = scanner.Text()
break
}
fmt.Println("Please Enter ClientSecret")
for scanner.Scan() {
secret = scanner.Text()
break
}
// consider cleaning this up by reducing line
cmd := exec.Command("twitch", "configure", "-i", cid, "-s", secret)
err := cmd.Start()
err = cmd.Wait()
errhandle(err)
cmd = exec.Command("twitch", "token", "-u", "-s", "user:read:follows")
err = cmd.Start()
err = cmd.Wait()
errhandle(err)
}
f, err := os.Open(twitchConfig)
errhandle(err)
defer f.Close()
scanner = bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "ACCESSTOKEN") {
access = strings.Replace(line, "ACCESSTOKEN=", "", 1)
} else if strings.Contains(line, "REFRESHTOKEN") {
refresh = strings.Replace(line, "REFRESHTOKEN=", "", 1)
} else if strings.Contains(line, "CLIENTID") {
cid = strings.Replace(line, "CLIENTID=", "", 1)
} else {
secret = strings.Replace(line, "CLIENTSECRET=", "", 1)
}
}
return cid, secret, access, refresh
}
func createClient() *helix.Client {
clientID, clientSecret, accessToken, _ := getCredentials()
client, err := helix.NewClient(&helix.Options{
ClientID: clientID,
ClientSecret: clientSecret,
RedirectURI: "http://localhost:3000",
UserAccessToken: accessToken,
})
errhandle(err)
fmt.Println("Validating Access Token")
isValid, resp, err := client.ValidateToken(accessToken)
if err != nil {
fmt.Println(resp)
} else if isValid {
fmt.Println("Access Token is Valid! UwU")
} else {
fmt.Println(resp)
}
return client
}
type Stream struct {
Title string
UserLogin string
UserName string
GameName string
GameID string
StartedAt time.Time
IsMature bool
ID string
ThumbnailURL string
TagIDs []string
Type string
UserID string
ViewerCount int
}
func newStream(stream helix.Stream) *Stream {
var s Stream
s.Title = stream.Title
s.UserLogin = stream.UserLogin
s.UserName = stream.UserName
s.GameName = stream.GameName
s.GameID = stream.GameID
s.StartedAt = stream.StartedAt
s.IsMature = stream.IsMature
s.ID = stream.ID
s.ThumbnailURL = strings.Replace(stream.ThumbnailURL, "{width}x{height}", "260x200", 1)
s.TagIDs = stream.TagIDs
s.Type = stream.Type
s.UserID = stream.UserID
s.ViewerCount = stream.ViewerCount
return &s
}
func playStream(stream *Stream, quality string) {
fmt.Println("Starting " + stream.UserName + "'s stream")
cmd := exec.Command("streamlink", "-v", "--twitch-disable-hosting", "--title", "{author} - {title}", "https://www.twitch.tv/"+stream.UserName, quality)
err := cmd.Run()
errhandle(err)
}
func grabThumbnail(st *Stream) (filePath string) {
filePath = st.UserName + "-thumb.png"
cmd := exec.Command("curl", "-o", filePath, st.ThumbnailURL)
cmd.Start()
cmd.Wait()
return filePath
}
func displayThumbnail(st *Stream) {
imagePath := grabThumbnail(st)
file, err := os.Open(imagePath)
if err != nil {
panic(err)
}
defer file.Close()
thumbNail, _, err := image.Decode(file)
kittyimg.Fprint(os.Stdout, thumbNail)
}
func main() {
checkTwitchCLI()
client := createClient()
apiResponse, err := client.GetFollowedStream(&helix.FollowedStreamsParams{
// UserID: "spiritmancy",
UserID: "80449608",
})
errhandle(err)
// possible to inline streams var?
streams := apiResponse.Data.Streams
for index := range apiResponse.Data.Streams {
st := newStream(streams[index])
fmt.Println("printing struct")
fmt.Printf("%+v\n", st)
displayThumbnail(st)
}
// playStream(*streams[0], "best")
}
// // --can-handle-url URL
// // --config FILENAME
// // -j, --json
// // Output JSON representations instead of the normal text output.
// // -a ARGUMENTS, --player-args ARGUMENTS
// // This option allows you to customize the default arguments which are put
// // together with the value of --player to create a command to execute.
// // --player-continuous-http
// // Make the player read the stream through HTTP, but unlike --player-http
// // it will continuously try to open the stream if the player requests it.
// // This makes it possible to handle stream disconnects if your player is
// // capable of reconnecting to a HTTP stream. This is usually done by
// // setting your player to a "repeat mode".
// // --default-stream STREAM -> aka best
// TODOS:
// * obtain userid
// * increase player options (choose player, set retry settings, read streamlink configs), flow options for 'self configuration with out twitch-cli'