Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit d8c0fe1

Browse files
author
Nyah Check
committed
api: Fixes to file format.
1 parent 0b253d7 commit d8c0fe1

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

api/apidata.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"net/http"
1414
"net/url"
1515
"strings"
16+
"unicode"
1617

1718
"github.com/Sirupsen/logrus"
1819
)
@@ -36,15 +37,15 @@ type RawVideoData struct {
3637
VideoId string
3738
VideoInfo string
3839
Vlength float64
39-
dpercent chan int64
40+
dpercent chan int64
4041
}
4142

4243
func (v *RawVideoData) Write(b []byte) (n int, err error) {
4344
n = len(b)
44-
totalbytes , dlevel := 0.0, 0.0
45+
totalbytes, dlevel := 0.0, 0.0
4546
v.Vlength = totalbytes + float64(n)
4647
curPercent := ((totalbytes / v.Vlength) * 100)
47-
if (dlevel <= curPercent) && (dlevel < 100) {
48+
if (dlevel <= curPercent) && (dlevel < 100) {
4849
dlevel++
4950
v.dpercent <- int64(dlevel)
5051
}
@@ -146,27 +147,39 @@ func APIGetVideoStream(format, id, path string, bitrate uint) (err error) {
146147
}
147148

148149
video.URLEncodedFmtStreamMap = streams
149-
150-
//create output file name and set path properly.
151-
file := video.Title + video.Author
152-
153150
//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)
154159
vstream := streams[0]
155160
url := vstream["url"] + "&signature" + vstream["sig"]
156161
logrus.Infof("Downloading file to %s", file)
157-
if format == "mp3" {
158-
file = file + ".mp3"
162+
if format == ".mp3" {
159163
err = ApiConvertVideo(file, id, format, bitrate, decodedVideo)
160164
if err != nil {
161165
logrus.Errorf("Error downloading audio: %v", err)
162166
}
163167

164168
} else { //defaults to flv format for video files.)
165-
file = file + ".flv"
166169
if err := ApiDownloadVideo(path, file, url, video); err != nil {
167170
logrus.Errorf("Error downloading video: %v", err)
168171
}
169172
}
170173

171174
return nil
172175
}
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

Comments
 (0)