forked from kkdai/youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.go
214 lines (191 loc) · 5.11 KB
/
youtube.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package youtube
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
)
func SetLogOutput(w io.Writer) {
log.SetOutput(w)
}
func NewYoutube() *Youtube {
return &Youtube{DownloadPercent: make(chan int64, 100)}
}
type stream map[string]string
type Youtube struct {
StreamList []stream
VideoID string
videoInfo string
DownloadPercent chan int64
contentLength float64
totalWrittenBytes float64
downloadLevel float64
}
func (y *Youtube) DecodeURL(url string) error {
err := y.findVideoId(url)
if err != nil {
return fmt.Errorf("findvideoID error=%s", err)
}
err = y.getVideoInfo()
if err != nil {
return fmt.Errorf("getVideoInfo error=%s", err)
}
err = y.parseVideoInfo()
if err != nil {
return fmt.Errorf("parse video info failed, err=%s", err)
}
return nil
}
func (y *Youtube) StartDownload(destFile string) error {
//download highest resolution on [0]
targetStream := y.StreamList[0]
url := targetStream["url"] + "&signature=" + targetStream["sig"]
log.Println("Download url=", url)
log.Println("Download to file=", destFile)
err := y.videoDLWorker(destFile, url)
return err
}
func (y *Youtube) parseVideoInfo() error {
answer, err := url.ParseQuery(y.videoInfo)
if err != nil {
return err
}
status, ok := answer["status"]
if !ok {
err = fmt.Errorf("no response status found in the server's answer")
return err
}
if status[0] == "fail" {
reason, ok := answer["reason"]
if ok {
err = fmt.Errorf("'fail' response status found in the server's answer, reason: '%s'", reason[0])
} else {
err = errors.New(fmt.Sprint("'fail' response status found in the server's answer, no reason given"))
}
return err
}
if status[0] != "ok" {
err = fmt.Errorf("non-success response status found in the server's answer (status: '%s')", status)
return err
}
// read the streams map
stream_map, ok := answer["url_encoded_fmt_stream_map"]
if !ok {
err = errors.New(fmt.Sprint("no stream map found in the server's answer"))
return err
}
// read each stream
streams_list := strings.Split(stream_map[0], ",")
var streams []stream
for stream_pos, stream_raw := range streams_list {
stream_qry, err := url.ParseQuery(stream_raw)
if err != nil {
log.Println(fmt.Sprintf("An error occured while decoding one of the video's stream's information: stream %d: %s\n", stream_pos, err))
continue
}
var sig string
if _, exist := stream_qry["sig"]; exist {
sig = stream_qry["sig"][0]
}
stream := stream{
"quality": stream_qry["quality"][0],
"type": stream_qry["type"][0],
"url": stream_qry["url"][0],
"sig": sig,
"title": answer["title"][0],
"author": answer["author"][0],
}
streams = append(streams, stream)
log.Printf("Stream found: quality '%s', format '%s'", stream_qry["quality"][0], stream_qry["type"][0])
}
y.StreamList = streams
return nil
}
func (y *Youtube) getVideoInfo() error {
url := "http://youtube.com/get_video_info?video_id=" + y.VideoID
log.Printf("url: %s", url)
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
y.videoInfo = string(body)
return nil
}
func (y *Youtube) findVideoId(url string) error {
videoId := url
if strings.Contains(videoId, "youtu") || strings.ContainsAny(videoId, "\"?&/<%=") {
re_list := []*regexp.Regexp{
regexp.MustCompile(`(?:v|embed|watch\?v)(?:=|/)([^"&?/=%]{11})`),
regexp.MustCompile(`(?:=|/)([^"&?/=%]{11})`),
regexp.MustCompile(`([^"&?/=%]{11})`),
}
for _, re := range re_list {
if is_match := re.MatchString(videoId); is_match {
subs := re.FindStringSubmatch(videoId)
videoId = subs[1]
}
}
}
log.Printf("Found video id: '%s'", videoId)
y.VideoID = videoId
if strings.ContainsAny(videoId, "?&/<%=") {
return errors.New("invalid characters in video id")
}
if len(videoId) < 10 {
return errors.New("the video id must be at least 10 characters long")
}
return nil
}
func (y *Youtube) Write(p []byte) (n int, err error) {
n = len(p)
y.totalWrittenBytes = y.totalWrittenBytes + float64(n)
currentPercent := ((y.totalWrittenBytes / y.contentLength) * 100)
if (y.downloadLevel <= currentPercent) && (y.downloadLevel < 100) {
y.downloadLevel++
y.DownloadPercent <- int64(y.downloadLevel)
}
return
}
func (y *Youtube) videoDLWorker(destFile string, target string) error {
resp, err := http.Get(target)
if err != nil {
log.Printf("Http.Get\nerror: %s\ntarget: %s\n", err, target)
return err
}
defer resp.Body.Close()
y.contentLength = float64(resp.ContentLength)
if resp.StatusCode != 200 {
log.Printf("reading answer: non 200[code=%v] status code received: '%v'", resp.StatusCode, err)
return errors.New("non 200 status code received")
}
err = os.MkdirAll(filepath.Dir(destFile), 666)
if err != nil {
return err
}
out, err := os.Create(destFile)
if err != nil {
return err
}
mw := io.MultiWriter(out, y)
_, err = io.Copy(mw, resp.Body)
if err != nil {
log.Println("download video err=", err)
return err
}
return nil
}