implement html js parser
This commit is contained in:
parent
d7e10c88bb
commit
1c4885317a
3 changed files with 82 additions and 13 deletions
4
go.mod
4
go.mod
|
|
@ -1,3 +1,5 @@
|
|||
module mptv3
|
||||
|
||||
go 1.23.4
|
||||
go 1.23.4
|
||||
|
||||
require golang.org/x/net v0.43.0 // indirect
|
||||
|
|
|
|||
2
go.sum
Normal file
2
go.sum
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
||||
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||
89
main.go
89
main.go
|
|
@ -6,16 +6,19 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
type MPVRequest struct {
|
||||
Command []string `json:"command"`
|
||||
}
|
||||
type MPVResponse struct {
|
||||
Data string `json:"data"`
|
||||
type MPVResponsePlayback struct {
|
||||
Data float32 `json:"data"`
|
||||
}
|
||||
|
||||
type MPV struct {
|
||||
|
|
@ -68,21 +71,83 @@ func (mpv *MPV) ExecuteIPC(req *MPVRequest) ([]byte, error) {
|
|||
return resBytes[:n], nil
|
||||
}
|
||||
|
||||
func (mpv *MPV) InquirePlayback() (float32, error) {
|
||||
resBytes, err := mpv.ExecuteIPC(&MPVRequest{
|
||||
Command: []string{"get_property", "playback-time"},
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var res MPVResponsePlayback
|
||||
if err = json.Unmarshal(resBytes, &res); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return res.Data, nil
|
||||
}
|
||||
|
||||
const PARSE_JSON_OFFSET = 25
|
||||
|
||||
func ParseWebMedia(url string) (string, error) {
|
||||
res, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
scripts := make([]string, 0)
|
||||
|
||||
var processNode func(*html.Node)
|
||||
processNode = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode && n.Data == "script" {
|
||||
if n.FirstChild != nil && n.FirstChild.Type == html.TextNode {
|
||||
scripts = append(scripts, n.FirstChild.Data)
|
||||
}
|
||||
}
|
||||
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
processNode(c)
|
||||
}
|
||||
}
|
||||
|
||||
node, err := html.Parse(res.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
processNode(node)
|
||||
|
||||
var streamChannels string
|
||||
|
||||
for _, script := range scripts {
|
||||
if strings.Contains(script, "var streamChannels") {
|
||||
streamChannels = script
|
||||
}
|
||||
}
|
||||
if streamChannels == "" {
|
||||
return "", fmt.Errorf("failed to find streamChannels")
|
||||
}
|
||||
|
||||
return streamChannels[25:], err
|
||||
}
|
||||
|
||||
var testUrl string
|
||||
|
||||
func main() {
|
||||
flag.StringVar(&testUrl, "test-url", "", "test url")
|
||||
flag.Parse()
|
||||
|
||||
mpv := NewMPV(testUrl, "/tmp/mptv3.sock")
|
||||
mpv.Spawn()
|
||||
defer mpv.Stop()
|
||||
// mpv := NewMPV(testUrl, "/tmp/mptv3.sock")
|
||||
// mpv.Spawn()
|
||||
// defer mpv.Stop()
|
||||
|
||||
time.Sleep(time.Second * 2)
|
||||
fmt.Println("EXECUTING IPC!!!!")
|
||||
res, err := mpv.ExecuteIPC(&MPVRequest{
|
||||
Command: []string{"get_property", "playback-time"},
|
||||
})
|
||||
// for {
|
||||
// time.Sleep(time.Second * 2)
|
||||
// _, err := mpv.InquirePlayback()
|
||||
|
||||
fmt.Println(res, err)
|
||||
// fmt.Println(err)
|
||||
// }
|
||||
|
||||
json, _ := ParseWebMedia(testUrl)
|
||||
fmt.Println(json)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue