-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalApi.go
106 lines (90 loc) · 2.54 KB
/
localApi.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
package main
import (
"bytes"
"encoding/json"
//"fmt"
"log"
"os/exec"
"strings"
)
type LocalApi struct {
YDLPath string
auth *TwitchAuth
chat *TwitchChat
}
type ParamsUrlConv struct {
Url string `json:"url"`
}
type ParamsLocal struct {
Query string `json:"query"`
}
// Create a constructor so a new API object cannot be created without an auth key
func NewLocalApi(ydlPath string, auth *TwitchAuth, chat *TwitchChat) *LocalApi {
api := new(LocalApi)
api.YDLPath = ydlPath
api.auth = auth
api.chat = chat
return api
}
// Gets the actual stream URL using youtube-dl
func (api *LocalApi) getStreamUrl(apiParams []byte) bytes.Buffer {
//fmt.Println(string(apiParams))
params := new(ParamsUrlConv)
err := json.Unmarshal(apiParams, params)
if err != nil {
log.Printf("Incorrect parameters passed to local call getStreamUrl: %s", err)
}
// Capture youtube-dl output
var output []byte
args := []string{"-g", params.Url}
if output, err = exec.Command("youtube-dl", args...).Output(); err != nil {
log.Printf("There was a problem running youtube-dl: %s", err)
}
var result bytes.Buffer
result.WriteString(`"`)
result.WriteString(strings.TrimSpace(string(output)))
result.WriteString(`"`)
return result
}
// Gets the actual stream URL using youtube-dl
func (api *LocalApi) getStreamDesc(apiParams []byte) bytes.Buffer {
// fmt.Println(string(apiParams))
params := new(ParamsUrlConv)
err := json.Unmarshal(apiParams, params)
if err != nil {
log.Printf("Incorrect parameters passed to local call getStreamUrl: %s", err)
}
// Capture youtube-dl output
var output []byte
args := []string{"--get-description", params.Url}
if output, err = exec.Command("youtube-dl", args...).Output(); err != nil {
log.Printf("There was a problem running youtube-dl: %s", err)
}
var result bytes.Buffer
result.WriteString(`"`)
result.Write(output)
result.WriteString(`"`)
return result
}
// Gets the actual stream URL using youtube-dl
func (api *LocalApi) isAuthenticated(apiParams []byte) bytes.Buffer {
var result bytes.Buffer
if api.auth.Password == "" {
result.WriteString("false")
} else {
result.WriteString("true")
}
return result
}
// Changes the current chat channel
func (api *LocalApi) changeChat(apiParams []byte) bytes.Buffer {
params := new(ParamsLocal)
err := json.Unmarshal(apiParams, params)
if err != nil {
log.Printf("There was a problem parsing the requested chat channel")
}
api.chat.AddChannel(api.auth.Username, "#"+params.Query, api.auth.Password)
var result bytes.Buffer
result.WriteString("true")
return result
}