-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
123 lines (99 loc) · 2.39 KB
/
parser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/samber/lo"
"go.uber.org/zap"
)
var linksRe = []string{
`((?P<src>www|music)\.)?(?P<origin>youtube)\.com/(shorts/|watch\?v=)(?P<id>[\w_-]+)`,
`(www\.)?(?P<origin>instagram)\.com/reels?/(?P<id>[\w_-]+)`,
}
var prepLinksRe = lo.Map(linksRe, func(l string, _ int) string {
return "(" + l + ")"
})
var linkRe = regexp.MustCompile("https?://(" + strings.Join(prepLinksRe, "|") + ")")
const (
VideoType = iota
MusicType = iota
)
type Source struct {
Origin string
Id string
Type int
}
func GetUniqSources(str string) []Source {
links := linkRe.FindAllString(str, -1)
sources := make([]Source, 0, len(links))
groups := linkRe.SubexpNames()
uniq := map[string]bool{}
for _, link := range links {
L.Info("link", zap.String("link", link))
matches := linkRe.FindStringSubmatch(link)
source := &Source{
Type: VideoType,
}
for i, match := range matches {
if match == "" {
continue
}
switch groups[i] {
case "id":
source.Id = match
case "origin":
source.Origin = match
case "src":
if match == "music" {
source.Type = MusicType
}
default:
}
}
L.Debug("result source", zap.Any("source", source))
if source.Origin == "" || source.Id == "" {
continue
}
key := source.Origin + "_" + source.Id
if uniq[key] {
continue
}
uniq[key] = true
sources = append(sources, *source)
}
return sources
}
type urlBuilder func(string) string
var urlBuilders = map[string]urlBuilder{
"youtube": func(id string) string {
return "https://www.youtube.com/watch?v=" + id
},
"instagram": func(id string) string {
return "https://www.instagram.com/reel/" + id
},
}
func BuildURL(origin, id string) (string, error) {
builder := urlBuilders[origin]
if builder == nil {
L.Error("Failed to get url builder", zap.String("origin", id))
return "", fmt.Errorf("no builder for origin: %s", origin)
}
return builder(id), nil
}
type VideoMeta struct {
Title string `json:"title"`
Description string `json:"description"`
Channel string `json:"channel"`
Artist string `json:"artist"`
Track string `json:"track"`
URL string `json:"webpage_url"`
FileName string `json:"_filename"`
}
func ParseMeta(raw []byte) (*VideoMeta, error) {
res := &VideoMeta{}
if err := json.Unmarshal(raw, res); err != nil {
return nil, err
}
return res, nil
}