/
youtube.go
144 lines (129 loc) · 3.22 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
package framework
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os/exec"
"strings"
)
const (
ERROR_TYPE = -1
VIDEO_TYPE = 0
PLAYLIST_TYPE = 1
)
type (
videoResponse struct {
Formats []struct {
Url string `json:"url"`
} `json:"formats"`
Title string `json:"title"`
}
VideoResult struct {
Media string
Title string
}
PlaylistVideo struct {
Id string `json:"id"`
}
YTSearchContent struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
ChannelTitle string `json:"channel_title"`
Duration string `json:"duration"`
}
ytApiResponse struct {
Error bool `json:"error"`
Content []YTSearchContent `json:"content"`
}
Youtube struct {
Conf *Config
}
)
func (youtube Youtube) getType(input string) int {
if strings.Contains(input, "upload_date") {
return VIDEO_TYPE;
}
if strings.Contains(input, "_type") {
return PLAYLIST_TYPE
}
return ERROR_TYPE
}
func (youtube Youtube) Get(input string) (int, *string, error) {
cmd := exec.Command("youtube-dl", "--skip-download", "--print-json", "--flat-playlist", input)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return ERROR_TYPE, nil, err
}
str := out.String()
return youtube.getType(str), &str, nil
}
func (youtube Youtube) Video(input string) (*VideoResult, error) {
var resp videoResponse
err := json.Unmarshal([]byte(input), &resp)
if err != nil {
return nil, err
}
return &VideoResult{resp.Formats[0].Url, resp.Title}, nil
}
func (youtube Youtube) Playlist(input string) (*[]PlaylistVideo, error) {
lines := strings.Split(input, "\n")
videos := make([]PlaylistVideo, 0)
for _, line := range lines {
if len(line) == 0 {
continue
}
var video PlaylistVideo
fmt.Println("line,", line)
err := json.Unmarshal([]byte(line), &video)
if err != nil {
return nil, err
}
videos = append(videos, video)
}
return &videos, nil
}
/*func (youtube Youtube) OldGet(id string) (*VideoResult, error) {
cmd := exec.Command("youtube-dl", "--skip-download", "--print-json", "--flat-playlist", id)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
fmt.Println("Error getting youtube info,", err)
return nil, err
}
fmt.Println(string(out.Bytes()))
var resp response
json.Unmarshal(out.Bytes(), &resp)
u := resp.Formats[0].Url
return &VideoResult{u, resp.Title}, nil
}*/
func (youtube Youtube) buildUrl(query string) (*string, error) {
base := youtube.Conf.ServiceUrl + "/v1/youtube/search"
address, err := url.Parse(base)
if err != nil {
return nil, err
}
params := url.Values{}
params.Add("search", query)
address.RawQuery = params.Encode()
str := address.String()
return &str, nil
}
func (youtube Youtube) Search(query string) ([]YTSearchContent, error) {
addr, err := youtube.buildUrl(query)
if err != nil {
return nil, err
}
resp, err := http.Get(*addr)
if err != nil {
return nil, err
}
var apiResp ytApiResponse
json.NewDecoder(resp.Body).Decode(&apiResp)
return apiResp.Content, nil
}