-
Notifications
You must be signed in to change notification settings - Fork 0
/
Downloader.go
100 lines (78 loc) · 2.27 KB
/
Downloader.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
package subtitles
import (
"encoding/json"
"errors"
"github.com/PuerkitoBio/goquery"
"net/http"
"regexp"
)
type Format string
const (
FormatSBV Format = "SBV"
FormatSCC Format = "SCC"
FormatSRT Format = "SRT"
FormatTTML Format = "TTML"
FormatVTT Format = "VTT "
)
// Get video data
func DownloadVideo(link string) (*http.Response, error) {
resp, err := http.Get(link)
if err != nil {
return nil, err
}
return resp, nil
}
// Returns the subtitle from the response
func ExtractSubtitleURL(response *http.Response) (string, error) {
doc, err := goquery.NewDocumentFromReader(response.Body)
if err != nil {
return "", err
}
var ytPlayerResponse string
doc.Find("body").Find("script").EachWithBreak(func(i int, s *goquery.Selection) bool {
scriptContent := s.Text()
regex := regexp.MustCompile(`^(?:var ytInitialPlayerResponse = )(.*);$`)
match := regex.FindStringSubmatch(scriptContent)
if len(match) > 1 {
ytPlayerResponse = match[1]
return false
}
return true
})
var ytPlayerResponseJSON map[string]interface{}
json.Unmarshal([]byte(ytPlayerResponse), &ytPlayerResponseJSON)
//There is probably some better way to do this
baseUrl, ok := ytPlayerResponseJSON["captions"].(map[string]interface{})["playerCaptionsTracklistRenderer"].(map[string]interface{})["captionTracks"].([]interface{})[0].(map[string]interface{})["baseUrl"].(string)
if !ok {
return "", errors.New("Couldn't extract baseUrl from videoplayer data!")
}
return baseUrl, nil
}
// Get subtitles
// Does everything
func GetSubtitles(videoLink string, formats ...Format) ([]parsedSubtitle, error) {
response, err := DownloadVideo(videoLink)
if err != nil {
return nil, err
}
baseURL, err := ExtractSubtitleURL(response)
if err != nil {
return nil, err
}
//TODO: implement different formats
fullLink := baseURL + "&fmt=TTML"
resp, err := http.Get(fullLink)
if err != nil {
return nil, err
}
parsedSubtitles, err := parseSubtitleResponse(resp, FormatTTML)
return parsedSubtitles, nil
//Build full URL
//Download subtitle file(s)
//Parse subtitle files
}
func main() {
//Manual: https://www.youtube.com/watch?v=Q85l1Fenc5w
//Auto generated: https://www.youtube.com/watch?v=6QQYUnSegaU&t=613s
GetSubtitles("https://www.youtube.com/watch?v=6QQYUnSegaU&t=613s")
}