forked from iawia002/lux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractors.go
60 lines (53 loc) 路 1.27 KB
/
extractors.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
package extractors
import (
"net/url"
"strings"
"sync"
"github.com/pkg/errors"
"github.com/wanyuqin/lux/utils"
)
var lock sync.RWMutex
var extractorMap = make(map[string]Extractor)
// Register registers an Extractor.
func Register(domain string, e Extractor) {
lock.Lock()
extractorMap[domain] = e
lock.Unlock()
}
// Extract is the main function to extract the data.
func Extract(u string, option Options) ([]*Data, error) {
u = strings.TrimSpace(u)
var domain string
bilibiliShortLink := utils.MatchOneOf(u, `^(av|BV|ep)\w+`)
if len(bilibiliShortLink) > 1 {
bilibiliURL := map[string]string{
"av": "https://www.bilibili.com/video/",
"BV": "https://www.bilibili.com/video/",
"ep": "https://www.bilibili.com/bangumi/play/",
}
domain = "bilibili"
u = bilibiliURL[bilibiliShortLink[1]] + u
} else {
u, err := url.ParseRequestURI(u)
if err != nil {
return nil, errors.WithStack(err)
}
if u.Host == "haokan.baidu.com" {
domain = "haokan"
} else {
domain = utils.Domain(u.Host)
}
}
extractor := extractorMap[domain]
if extractor == nil {
extractor = extractorMap[""]
}
videos, err := extractor.Extract(u, option)
if err != nil {
return nil, errors.WithStack(err)
}
for _, v := range videos {
v.FillUpStreamsData()
}
return videos, nil
}