@@ -13,6 +13,7 @@ import (
13
13
"net/http"
14
14
"net/url"
15
15
"strings"
16
+ "unicode"
16
17
17
18
"github.com/Sirupsen/logrus"
18
19
)
@@ -36,15 +37,15 @@ type RawVideoData struct {
36
37
VideoId string
37
38
VideoInfo string
38
39
Vlength float64
39
- dpercent chan int64
40
+ dpercent chan int64
40
41
}
41
42
42
43
func (v * RawVideoData ) Write (b []byte ) (n int , err error ) {
43
44
n = len (b )
44
- totalbytes , dlevel := 0.0 , 0.0
45
+ totalbytes , dlevel := 0.0 , 0.0
45
46
v .Vlength = totalbytes + float64 (n )
46
47
curPercent := ((totalbytes / v .Vlength ) * 100 )
47
- if (dlevel <= curPercent ) && (dlevel < 100 ) {
48
+ if (dlevel <= curPercent ) && (dlevel < 100 ) {
48
49
dlevel ++
49
50
v .dpercent <- int64 (dlevel )
50
51
}
@@ -146,27 +147,39 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
146
147
}
147
148
148
149
video .URLEncodedFmtStreamMap = streams
149
-
150
- //create output file name and set path properly.
151
- file := video .Title + video .Author
152
-
153
150
//Download Video stream to file
151
+ if format == "" {
152
+ format = ".flv"
153
+ } else {
154
+ format = ".mp3"
155
+ }
156
+ //create output file name and set path properly.
157
+ file := video .Title + format
158
+ file = SpaceMap (file )
154
159
vstream := streams [0 ]
155
160
url := vstream ["url" ] + "&signature" + vstream ["sig" ]
156
161
logrus .Infof ("Downloading file to %s" , file )
157
- if format == "mp3" {
158
- file = file + ".mp3"
162
+ if format == ".mp3" {
159
163
err = ApiConvertVideo (file , id , format , bitrate , decodedVideo )
160
164
if err != nil {
161
165
logrus .Errorf ("Error downloading audio: %v" , err )
162
166
}
163
167
164
168
} else { //defaults to flv format for video files.)
165
- file = file + ".flv"
166
169
if err := ApiDownloadVideo (path , file , url , video ); err != nil {
167
170
logrus .Errorf ("Error downloading video: %v" , err )
168
171
}
169
172
}
170
173
171
174
return nil
172
175
}
176
+
177
+ //remove whitespaces in filename
178
+ func SpaceMap (str string ) string {
179
+ return strings .Map (func (r rune ) rune {
180
+ if unicode .IsSpace (r ) {
181
+ return - 1
182
+ }
183
+ return r
184
+ }, str )
185
+ }
0 commit comments