-
Notifications
You must be signed in to change notification settings - Fork 3
/
inews.go
62 lines (52 loc) · 1.09 KB
/
inews.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
package main
import (
"io/ioutil"
"net/http"
"regexp"
log "github.com/tominescu/double-golang/simplelog"
)
const RETRY_COUNT = 3
func GetURL(id string) string {
url := "http://news.tvb.com/live/" + id + "?is_hd"
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("X-Forwarded-For", "185.225.12.27")
resp, err := client.Do(req)
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ""
}
reg := regexp.MustCompile(`http:[^\"]*\.m3u8`)
dst := reg.Find(body)
return string(dst)
}
func inewsHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Request URL:%s", r.URL)
err := r.ParseForm()
if err != nil {
http.Error(w, http.StatusText(503), 503)
return
}
id := r.Form.Get("id")
if id == "" {
http.Error(w, http.StatusText(503), 503)
return
}
var url string
for i := 0; i < RETRY_COUNT; i++ {
url = GetURL(id)
if url != "" {
break
}
}
if len(url) == 0 {
http.Error(w, "Cant find m3u8", 503)
return
}
w.Header().Set("Location", url)
http.Error(w, url, 302)
}