7
7
package api
8
8
9
9
import (
10
+ "encoding/json"
10
11
"errors"
11
12
"io/ioutil"
12
13
"net/http"
@@ -26,16 +27,16 @@ const (
26
27
27
28
//Youtube Downloader Data file.
28
29
type RawVideoData struct {
29
- Title string `json:"title"`
30
- Author string `json:"author`
31
- Status string `json:"status"`
32
- URLEncodedFmtStreamMap map [ string ][] string `json:"url_encoded_fmt_stream_map"`
30
+ Title string `json:"title"`
31
+ Author string `json:"author`
32
+ Status string `json:"status"`
33
+ URLEncodedFmtStreamMap string `json:"url_encoded_fmt_stream_map"`
33
34
}
34
35
35
36
//gets the Video ID from youtube url
36
37
func GetVideoId (url string ) (string , error ) {
37
38
if ! strings .Contains (url , "youtube.com" ) {
38
- return nil , errors .New ("Invalid Youtube link" )
39
+ return "" , errors .New ("Invalid Youtube link" )
39
40
}
40
41
s := strings .Split (url , "?v=" )
41
42
s = strings .Split (s [1 ], "&" )
@@ -47,7 +48,7 @@ func GetVideoId(url string) (string, error) {
47
48
}
48
49
49
50
//Gets Video Info, Decode Video Info from a Video ID.
50
- func APIGetVideoStream (id string , video RawVideoData ) (videoData []byte , err error ) {
51
+ func APIGetVideoStream (id string , video * RawVideoData ) (videoData []string , err error ) {
51
52
52
53
video = new (RawVideoData ) //raw video data
53
54
var decodedVideo []string //decoded video data
@@ -65,20 +66,21 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err
65
66
logrus .Errorf ("Error reading video data: %v" , e )
66
67
}
67
68
68
- output , er := url .ParseQuery (out )
69
+ output , er := url .ParseQuery (string ( out ) )
69
70
if e != nil {
70
- return nil , errors .New ("Error Parsing video byte stream: %v" , e )
71
+ logrus .Errorf ("Error parsing video byte stream: %v" , e )
72
+ return nil , e
71
73
}
72
74
73
75
//Process Video stream
74
- video .URLEncodedFmtStreamMap = output [ "url_encoded_fmt_stream_map" ]
75
- video .Author = output [ "author" ]
76
- video .Title = output [ "title" ]
77
- video .Status = output [ "status" ]
76
+ video .URLEncodedFmtStreamMap = output . Get ( "url_encoded_fmt_stream_map" )
77
+ video .Author = output . Get ( "author" )
78
+ video .Title = output . Get ( "title" )
79
+ video .Status = output . Get ( "status" )
78
80
79
81
//Decode Video
80
- outputStreams := strings .Split (video .URLEncodedFmtStreamMap [ 0 ] , "," )
81
- for cur , raw_data := range outputStream {
82
+ outputStreams := strings .Split (video .URLEncodedFmtStreamMap , "," )
83
+ for cur , raw_data := range outputStreams {
82
84
//decoding raw data stream
83
85
dec_data , err := url .ParseQuery (raw_data )
84
86
if err != nil {
@@ -96,7 +98,8 @@ func APIGetVideoStream(id string, video RawVideoData) (videoData []byte, err err
96
98
"format" : dec_data ["format" ][0 ],
97
99
}
98
100
99
- decodedVideo = append (decodedVideo , data )
101
+ str , _ := json .Marshal (data )
102
+ decodedVideo = append (decodedVideo , string (str ))
100
103
logrus .Infof ("\n Decoded %d bytes of %q, in %q format" , len (decodedVideo ), dec_data ["quality" ][0 ], dec_data ["format" ][0 ])
101
104
}
102
105
0 commit comments