Feat: support b23 tv parser

pull/31/head
zijiren233 3 years ago
parent 22245104dd
commit bf6f5418bf

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"net/http"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -17,7 +18,18 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
noRedirectHttpClient = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
)
func NoRedirectHttpClient() *http.Client {
return noRedirectHttpClient
}
const ( const (
UA = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.69` UA = `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.69`

@ -3,16 +3,27 @@ package bilibili
import ( import (
"errors" "errors"
"regexp" "regexp"
"github.com/synctv-org/synctv/utils"
) )
var ( var (
BVRegex = regexp.MustCompile(`(?:https://www\.bilibili\.com/video/)?((?:bv|bV|Bv|BV)\w+)(?:/(\?.*)?)?$`) BVRegex = regexp.MustCompile(`^(?:https://www\.bilibili\.com/video/)?((?:bv|bV|Bv|BV)\w+)(?:[/\?].*)?$`)
ARegex = regexp.MustCompile(`(?:https://www\.bilibili\.com/video/)?(?:av|aV|Av|AV)(\d+)(?:/(\?.*)?)?$`) ARegex = regexp.MustCompile(`^(?:https://www\.bilibili\.com/video/)?(?:av|aV|Av|AV)(\d+)(?:[/\?].*)?$`)
SSRegex = regexp.MustCompile(`(?:https://www\.bilibili\.com/bangumi/play/)?(?:ss|sS|Ss|SS)(\d+)(?:\?.*)?$`) SSRegex = regexp.MustCompile(`^(?:https://www\.bilibili\.com/bangumi/play/)?(?:ss|sS|Ss|SS)(\d+)(?:\?.*)?$`)
EPRegex = regexp.MustCompile(`(?:https://www\.bilibili\.com/bangumi/play/)?(?:ep|eP|Ep|EP)(\d+)(?:\?.*)?$`) EPRegex = regexp.MustCompile(`^(?:https://www\.bilibili\.com/bangumi/play/)?(?:ep|eP|Ep|EP)(\d+)(?:\?.*)?$`)
B23Regex = regexp.MustCompile(`^(https://)?b23\.tv/(\w+)$`)
) )
func Match(url string) (t string, id string, err error) { func Match(url string) (t string, id string, err error) {
if B23Regex.MatchString(url) {
resp, err := utils.NoRedirectHttpClient().Get(url)
if err != nil {
return "", "", err
}
resp.Body.Close()
return Match(resp.Header.Get("Location"))
}
if m := BVRegex.FindStringSubmatch(url); m != nil { if m := BVRegex.FindStringSubmatch(url); m != nil {
return "bv", m[1], nil return "bv", m[1], nil
} }

Loading…
Cancel
Save