增加嗅探m3u8的功能

main
orestonce 2022-05-21 20:26:17 +08:00
parent 20d0333ad3
commit bdf6427508
3 changed files with 29 additions and 3 deletions

View File

@ -15,9 +15,11 @@ import (
"github.com/orestonce/gopool" "github.com/orestonce/gopool"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -76,6 +78,12 @@ func RunDownload(req RunDownload_Req) (resp RunDownload_Resp) {
if req.SkipTsCountFromHead < 0 { if req.SkipTsCountFromHead < 0 {
req.SkipTsCountFromHead = 0 req.SkipTsCountFromHead = 0
} }
var err error
req.M3u8Url, err = sniffM3u8(req.M3u8Url)
if err != nil {
resp.ErrMsg = "sniffM3u8: " + err.Error()
return resp
}
id, err := req.getVideoId() id, err := req.getVideoId()
if err != nil { if err != nil {
resp.ErrMsg = "getVideoId: " + err.Error() resp.ErrMsg = "getVideoId: " + err.Error()
@ -510,7 +518,23 @@ func getFileSha256(targetFile string) (v string) {
return hex.EncodeToString(tmp[:]) return hex.EncodeToString(tmp[:])
} }
func getStringSha256(s string) (v string) { func sniffM3u8(urlS string) (afterUrl string, err error) {
tmp := sha256.Sum256([]byte(s)) if strings.HasSuffix(strings.ToLower(urlS), ".m3u8") {
return hex.EncodeToString(tmp[:]) return urlS, nil
}
resp, err := http.DefaultClient.Get(urlS)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
groups := regexp.MustCompile(`http[s]://[a-zA-Z0-9/\\.%_-]+.m3u8`).FindSubmatch(content)
if len(groups) == 0 {
return "", errors.New("未发现m3u8资源")
}
return string(groups[0]), nil
} }

View File

@ -1,3 +1,5 @@
//+build windows
package m3u8d package m3u8d
import ( import (

Binary file not shown.