-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (113 loc) · 3.29 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
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"runtime"
"sync"
"time"
"github.com/rakyll/statik/fs"
"github.com/rs/cors"
"github.com/cnt0/golang-http-utils/utils"
"github.com/cnt0/twitch-streamsniper/api"
"github.com/cnt0/twitch-streamsniper/player-connection/mpris"
"github.com/cnt0/twitch-streamsniper/player-connection/mpv"
_ "github.com/cnt0/twitch-streamsniper/site/statik"
)
const handlerTimeout = 5 * time.Second
var (
m sync.Mutex
streams *api.FollowedStreams
client = flag.String("client", "", "client field for twitch authentication")
auth = flag.String("auth", "", "auth field for twitch authentication")
ytdl = flag.String("ytdl", "youtube-dl", "path to youtube-dl executable")
socket = flag.String("socket", "/tmp/mpvsocket", "path to unix socket for communication with mpv")
isMPV = flag.Bool("mpv", false, "connect to mpv socket instead of mpris dbus")
mpvConnection *mpv.Connection
mprisConnection *mpris.Connection
)
func playVideo(addr string) error {
if *isMPV {
return mpvConnection.PlayVideo(addr)
}
return mprisConnection.PlayVideo(addr)
}
// HandleUpdateAll ...
func HandleUpdateAll(w http.ResponseWriter, r *http.Request) {
m.Lock()
var err error
if streams, err = api.ParseFollowedStreams(*client, *auth); err != nil {
log.Println(err)
}
m.Unlock()
if err := json.NewEncoder(w).Encode(streams); err != nil {
log.Println(err)
}
}
// HandleUpdateFormats ...
func HandleUpdateFormats(w http.ResponseWriter, r *http.Request) {
channel := r.URL.Query().Get("s")
log.Println("update stream for " + channel)
if channel == "" {
m.Lock()
if err := json.NewEncoder(w).Encode(streams); err != nil {
log.Println(err)
}
m.Unlock()
}
stream, err := streams.UpdateStream(channel, *client, *ytdl)
if err != nil {
log.Println(err)
} else if stream == nil {
log.Printf("%s: no such stream!", channel)
} else {
if err := json.NewEncoder(w).Encode(stream); err != nil {
log.Println(err)
}
}
}
// HandlePlayVideo ...
func HandlePlayVideo(w http.ResponseWriter, r *http.Request) {
log.Printf("request: %v\n", r.Method)
if len(*socket) > 0 && r.Method == "POST" {
var s struct {
URL string `json:"url"`
}
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
log.Println(err)
}
log.Printf("playing video %v", s.URL)
if err := playVideo(s.URL); err != nil {
log.Println(err)
}
}
}
func init() {
flag.Parse()
}
func main() {
runtime.GOMAXPROCS(1)
log.Printf("client: %v", *client)
log.Printf("auth: %v", *auth)
m.Lock()
var err error
if streams, err = api.ParseFollowedStreams(*client, *auth); err != nil {
log.Fatal(err)
}
m.Unlock()
statikFS, _ := fs.New()
mux := http.NewServeMux()
mux.Handle("/", http.FileServer(statikFS))
mux.Handle("/formats", http.TimeoutHandler(http.HandlerFunc(HandleUpdateFormats), handlerTimeout, "timeout in formats handler"))
mux.Handle("/update", http.TimeoutHandler(http.HandlerFunc(HandleUpdateAll), handlerTimeout, "timeout in update handler"))
mux.Handle("/play", http.TimeoutHandler(http.HandlerFunc(HandlePlayVideo), handlerTimeout, "timeout in play handler"))
c := cors.New(cors.Options{
AllowedOrigins: []string{
"http://localhost:8080",
"http://localhost:4200",
},
AllowCredentials: true,
})
utils.ListenAndServeSA(":8080", c.Handler(mux))
}