-
Notifications
You must be signed in to change notification settings - Fork 2
/
mytv.go
221 lines (180 loc) · 5.19 KB
/
mytv.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
215
216
217
218
219
220
221
package main
import (
"crypto/md5"
"crypto/tls"
"encoding/hex"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"syscall"
"time"
)
type Channel struct {
Name string
Url string
}
type Channels []Channel
func (a Channels) Len() int { return len(a) }
func (a Channels) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Channels) Less(i, j int) bool { return a[i].Name < a[j].Name }
func getUrl(url string) string {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
response, err := client.Get(os.Args[1])
if err != nil {
panic(err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
return string(contents)
}
func parseChannels(s string) Channels {
lines := strings.Split(s, "\n")
channels := make(Channels, 0, len(lines)/2)
name := ""
url := ""
for _, line := range lines {
if strings.Contains(line, "EXTINF") {
parts := strings.SplitN(line, ",", 2)
name = strings.TrimSpace(parts[1])
continue
}
if strings.Contains(line, "#EXTGRP") {
continue
}
if name != "" {
url = strings.TrimSpace(line)
channels = append(channels, Channel{name, url})
}
}
return channels
}
func getMD5Hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
func getLabelPath(channel Channel, extension string) string {
return fmt.Sprintf("labels/%s.%s", getMD5Hash(channel.Name), extension)
}
func killProcessGroup(proc *os.Process) {
pgid, err := syscall.Getpgid(proc.Pid)
if err == nil {
syscall.Kill(-pgid, 15)
}
proc.Kill()
proc.Wait()
}
func makeLabels(channels Channels) {
os.Mkdir("labels", 0755)
for _, channel := range channels {
filename := getLabelPath(channel, "gz")
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf(" ... making label for %s\n", channel.Name)
jpgFilename := getLabelPath(channel, "jpg")
exec.Command("convert", "-background", "black", "-fill", "white",
"-font", "Liberation-Sans-Bold", "-size", "768x",
"-pointsize", "160", "", "-gravity", "center",
fmt.Sprintf("caption:%s", channel.Name), jpgFilename).Output()
fbiProc := exec.Command("fbi", "-noverbose", "-T", "1", "-1", jpgFilename)
fbiProc.Start()
time.Sleep(500 * time.Millisecond)
exec.Command("/bin/sh", "-c", fmt.Sprintf("cat /dev/fb0 | gzip > %s", filename)).Output()
exec.Command("/bin/sh", "-c", "pkill fbi").Output()
os.Remove(jpgFilename)
} else {
fmt.Printf(" ... label for %s is present\n", channel.Name)
}
}
}
func switchChannel(channel Channel) *os.Process {
exec.Command("/bin/sh", "-c", "setterm -blank off -powerdown off > /dev/tty0").Output()
exec.Command("/bin/sh", "-c", "clear > /dev/tty0").Output()
exec.Command("/bin/sh", "-c", "setterm -cursor off > /dev/tty0").Output()
exec.Command("/bin/sh", "-c",
fmt.Sprintf("zcat %s > /dev/fb0",
getLabelPath(channel, "gz"))).Output()
player := exec.Command("omxplayer", "-o", "hdmi", channel.Url)
player.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
err := player.Start()
if err != nil {
panic(err)
}
return player.Process
}
func killChannel(proc *os.Process) {
killProcessGroup(proc)
exec.Command("/bin/sh", "-c", "clear > /dev/tty0")
exec.Command("/bin/sh", "-c", "setterm -cursor on > /dev/tty0")
}
var channels Channels
var currentChannelNo = -1
var currentPlayerProcess *os.Process = nil
func turnOff() {
if currentPlayerProcess != nil {
killChannel(currentPlayerProcess)
currentPlayerProcess = nil
}
currentChannelNo = -1
}
func changeChannel(channels Channels, no int) {
if no >= len(channels) || no < 0 {
return
}
turnOff()
currentChannelNo = no
currentPlayerProcess = switchChannel(channels[currentChannelNo])
}
func changeChannelHandler(w http.ResponseWriter, r *http.Request) {
no, err := strconv.Atoi(r.FormValue("no"))
fmt.Printf("Switching to %d\n", no)
if err != nil {
fmt.Fprintf(w, "ERROR")
} else {
changeChannel(channels, no)
http.Redirect(w, r, "/", 301)
}
}
func turnoffHandler(w http.ResponseWriter, r *http.Request) {
turnOff()
fmt.Printf("Turning off\n")
http.Redirect(w, r, "/", 301)
}
func channelDirectoryHandler(w http.ResponseWriter, r *http.Request) {
channelsJson := make([]string, 0, len(channels))
for idx, channel := range channels {
channelsJson = append(channelsJson, fmt.Sprintf("{ \"no\": %d, \"name\": \"%s\" }", idx, channel.Name))
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "{ \"current\": %d, \"channels\": [ %s ] }", currentChannelNo, strings.Join(channelsJson, ", "))
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s _m3u_url_", os.Args[0])
return
}
fmt.Println("Welcome to MyTV\n")
channels = parseChannels(getUrl(os.Args[1]))
sort.Sort(channels)
fmt.Println("Making labels")
makeLabels(channels)
fmt.Println("Starting web server")
fs := http.FileServer(http.Dir("static"))
http.Handle("/", fs)
http.HandleFunc("/channels", channelDirectoryHandler)
http.HandleFunc("/channel", changeChannelHandler)
http.HandleFunc("/off", turnoffHandler)
fmt.Println("Serving on :80")
log.Fatal(http.ListenAndServe(":80", nil))
}