From 476df33e155278d1853446728813a031dab4688e Mon Sep 17 00:00:00 2001 From: Nyah Check Date: Sat, 16 Sep 2017 17:47:31 -0600 Subject: [PATCH] api: Fixed compilation issues in api --- api/apiconv.go | 25 ++++++++++++++++--------- api/apidata.go | 33 ++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/api/apiconv.go b/api/apiconv.go index 7bab3dc..712abb9 100644 --- a/api/apiconv.go +++ b/api/apiconv.go @@ -7,6 +7,9 @@ package api import ( + "bytes" + "encoding/gob" + "errors" "fmt" "io" "net/http" @@ -19,7 +22,7 @@ import ( //Converts Decoded Video file to mp3 by default with 123 bitrate or to //flv if otherwise specified and downloads to system -func APIConvertVideo(file string, bitrate int, id string, decVideo []byte) error { +func APIConvertVideo(file string, bitrate int, id string, decVideo []string) error { cmd := exec.Command("ffmpeg", "-i", "-", "-ab", fmt.Sprintf("%dk", bitrate), file) stdin, err := cmd.StdinPipe() if err != nil { @@ -31,22 +34,26 @@ func APIConvertVideo(file string, bitrate int, id string, decVideo []byte) error logrus.Infof("Converting video to %q format", filepath.Ext(file)) if filepath.Ext(file) == ".mp3" { - // NOTE: To modify to use Go ffmpeg bindings or cgo + /* NOTE: To modify to use Go ffmpeg bindings or cgo */ + + buf := &bytes.Buffer{} + gob.NewEncoder(buf).Encode(decVideo) _, err = exec.LookPath("ffmpeg") if err != nil { - logrus.Errorf("ffmpeg not found on system") + return errors.New("ffmpeg not found on system") } cmd.Start() logrus.Infof("Downloading mp3 file to disk %s", file) - cmd.Write(decVideo) //download file. + stdin.Write(buf.Bytes()) //download file. } else { - cmd, err = os.Create(file) + out, err := os.Create(file) if err != nil { - logrus.Error("Unable to download video file.", err) + logrus.Errorf("Unable to download video file.", err) + return err } - err = apiDownloadVideo(id, cmd) + err = apiDownloadVideo(id, out) return err } @@ -54,10 +61,10 @@ func APIConvertVideo(file string, bitrate int, id string, decVideo []byte) error } //Downloads decoded video stream. -func apiDownloadVideo(videoUrl, cmd io.Writer) error { +func apiDownloadVideo(url string, out io.Writer) error { logrus.Infof("Downloading file stream") - resp, err := http.Get(url) + resp, err := http.Get(videoExtractor + url) if err != nil { return fmt.Errorf("requesting stream: %s", err) } diff --git a/api/apidata.go b/api/apidata.go index 849a294..87d8f51 100644 --- a/api/apidata.go +++ b/api/apidata.go @@ -7,6 +7,7 @@ package api import ( + "encoding/json" "errors" "io/ioutil" "net/http" @@ -26,16 +27,16 @@ const ( //Youtube Downloader Data file. type RawVideoData struct { - Title string `json:"title"` - Author string `json:"author` - Status string `json:"status"` - URLEncodedFmtStreamMap map[string][]string `json:"url_encoded_fmt_stream_map"` + Title string `json:"title"` + Author string `json:"author` + Status string `json:"status"` + URLEncodedFmtStreamMap string `json:"url_encoded_fmt_stream_map"` } //gets the Video ID from youtube url func GetVideoId(url string) (string, error) { if !strings.Contains(url, "youtube.com") { - return nil, errors.New("Invalid Youtube link") + return "", errors.New("Invalid Youtube link") } s := strings.Split(url, "?v=") s = strings.Split(s[1], "&") @@ -47,7 +48,7 @@ func GetVideoId(url string) (string, error) { } //Gets Video Info, Decode Video Info from a Video ID. -func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err error) { +func APIGetVideoStream(id string, video *RawVideoData) (videoData []string, err error) { video = new(RawVideoData) //raw video data var decodedVideo []string //decoded video data @@ -65,20 +66,21 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err logrus.Errorf("Error reading video data: %v", e) } - output, er := url.ParseQuery(out) + output, er := url.ParseQuery(string(out)) if e != nil { - return nil, errors.New("Error Parsing video byte stream: %v", e) + logrus.Errorf("Error parsing video byte stream: %v", e) + return nil, e } //Process Video stream - video.URLEncodedFmtStreamMap = output["url_encoded_fmt_stream_map"] - video.Author = output["author"] - video.Title = output["title"] - video.Status = output["status"] + video.URLEncodedFmtStreamMap = output.Get("url_encoded_fmt_stream_map") + video.Author = output.Get("author") + video.Title = output.Get("title") + video.Status = output.Get("status") //Decode Video - outputStreams := strings.Split(video.URLEncodedFmtStreamMap[0], ",") - for cur, raw_data := range outputStream { + outputStreams := strings.Split(video.URLEncodedFmtStreamMap, ",") + for cur, raw_data := range outputStreams { //decoding raw data stream dec_data, err := url.ParseQuery(raw_data) if err != nil { @@ -96,7 +98,8 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err "format": dec_data["format"][0], } - decodedVideo = append(decodedVideo, data) + str, _ := json.Marshal(data) + decodedVideo = append(decodedVideo, string(str)) logrus.Infof("\nDecoded %d bytes of %q, in %q format", len(decodedVideo), dec_data["quality"][0], dec_data["format"][0]) }