-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (108 loc) · 2.69 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
// nolint: revive
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/dustin/go-humanize"
"github.com/jessevdk/go-flags"
)
const userAgent string = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
var opts struct {
VideoID string `short:"v" long:"video-id" description:"Video ID" required:"true"`
FileName string `short:"o" long:"output" description:"Output file" required:"true"`
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
err = process(opts.VideoID, opts.FileName)
if err != nil {
fmt.Printf("Failed: %s", err)
os.Exit(1)
}
}
func process(videoID, filename string) error {
url := fmt.Sprintf("https://fast.wistia.net/embed/iframe/%s?videoFoam=true", videoID)
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() {
err := resp.Body.Close()
if err != nil {
fmt.Printf("failed to close the body: %s", err)
}
}()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status code: %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
bodyString := string(bodyBytes)
assets, err := findAssets(bodyString)
if err != nil {
return fmt.Errorf("cannot find the target URL. Bad video ID maybe?")
}
asset, err := chooseAsset(assets)
if err != nil {
return err
}
fmt.Printf("Video found. Resolution=%dx%d Size=%s\n", asset.Width, asset.Height, humanize.Bytes(uint64(asset.Size)))
return downloadFile(asset.URL, filename)
}
// chooseAsset finds a video stream with the highest resolution
func chooseAsset(assets []asset) (asset, error) {
var chosen asset
for _, a := range assets {
if !a.IsVideo() {
continue
}
if a.Height > chosen.Height {
chosen = a
}
}
if chosen.Height > 0 {
return chosen, nil
}
return chosen, fmt.Errorf("there is no video stream in the assets")
}
func downloadFile(url, filename string) (err error) {
fmt.Println("Starting download...")
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
return err
}
defer func() {
err := resp.Body.Close()
if err != nil {
fmt.Printf("failed to close the body: %s", err)
}
}()
out, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
err := out.Close()
if err != nil {
fmt.Printf("failed to close the output file: %s", err)
}
}()
n, err := io.Copy(out, resp.Body)
if err != nil {
return err
}
fmt.Printf("Downloaded %d bytes.\n", n)
return nil
}