-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest.go
78 lines (65 loc) · 1.73 KB
/
request.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
package gotube
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strconv"
)
// ExtractQueryParam Extract the id from a youtube url
func ExtractQueryParam(videoURL string) (string, error) {
var id string
var err error
isMatch, err := regexp.MatchString(`https://www\.youtube\.com/watch\?v=[\w-]+`, videoURL) // TODO need better regex pattern
if err != nil {
return id, err
}
if !isMatch {
return id, fmt.Errorf("Invalid YouTube URL")
}
var reprURL *url.URL
reprURL, err = url.Parse(videoURL)
if err != nil {
return id, err
}
id = reprURL.Query()["v"][0]
return id, nil
}
// GetMetaData Get all the data of a youtube video and return it in json format
func GetMetaData(url string) (Video, error) {
id, err := ExtractQueryParam(url)
if err != nil {
return Video{}, fmt.Errorf("ExtractQueryParam failed")
}
resp, err := http.Get(fmt.Sprintf("https://www.youtube.com/watch?v=%s", id))
if err != nil {
return Video{}, fmt.Errorf("get video failed")
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Video{}, fmt.Errorf("failed parsing response body")
}
extractJSON := extractValue(string(bodyBytes), "ytInitialPlayerResponse = ", ";</script>")
var youtubeRequest Video
err = json.Unmarshal([]byte(extractJSON), &youtubeRequest)
if err != nil {
return Video{}, fmt.Errorf("failed extract json")
}
return youtubeRequest, nil
}
// GetDownloadSize returns the size of a download
func GetDownloadSize(url string) (int64, error) {
resp, err := http.Head(url)
if err != nil {
return 0, err
}
size, err := strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return 0, err
}
downloadSize := int64(size)
return downloadSize, nil
}