forked from zjyl1994/tvproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvb.go
49 lines (43 loc) · 953 Bytes
/
tvb.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
package main
import (
"errors"
"io/ioutil"
"regexp"
"github.com/gin-gonic/gin"
)
func parseTVB(liveName string) (string, error) {
resp, err := getHTTPClient().Get("http://news.tvb.com/live/" + liveName)
if err != nil {
return "", err
}
defer resp.Body.Close()
bBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
m3u8Regexp := regexp.MustCompile(`<source src="(.*?)" type="application/x-mpegURL">`)
match := m3u8Regexp.FindStringSubmatch(string(bBody))
if len(match) > 1 {
return match[1], nil
} else {
return "", nil
}
}
func tvbHandler(liveName string, c *gin.Context) {
m3u8, err := parseTVB(liveName)
if err != nil {
c.AbortWithError(500, err)
return
}
if m3u8 == "" {
c.AbortWithError(404, errors.New("video not found"))
} else {
c.Redirect(302, m3u8)
}
}
func iNewsHandler(c *gin.Context) {
tvbHandler("inews", c)
}
func financeHandler(c *gin.Context) {
tvbHandler("j5_ch85", c)
}