-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
136 lines (119 loc) · 2.96 KB
/
search.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
package youtube_info_extractor
import (
"encoding/json"
"fmt"
"log"
"net/url"
"os/exec"
"strings"
)
var (
YtDlpPath = "yt-dlp" // If you want to set other program or anything, overwrite it.
)
type Video struct {
Id string `json:"id"`
Title string `json:"title"`
ThumbnailUrl string `json:"thumbnail"`
Duration uint32 `json:"duration"` // in seconds
PlayUrl string `json:"urls"` // googlevideo.com domain.
}
// SearchOneVideoKeyword
//
// Deprecated: Use SearchVideoKeyword instead.
func SearchOneVideoKeyword(keyword string, output chan *Video) {}
// SearchVideoKeyword
// This is only for searching video on YouTube.
// Support for only one searching result.
// amount is the amount of searching result.
func SearchVideoKeyword(keyword string, amount int, output chan *Video) {
if amount <= 0 {
close(output)
return
}
ytCommand := exec.Command(
YtDlpPath,
"--default-search=ytsearch",
"ytsearch"+fmt.Sprint(amount)+":"+keyword,
"--skip-download",
"--no-playlist",
"--dump-json",
)
res, err := ytCommand.Output()
if err != nil {
log.Fatalln("Error during getting output: ", err)
}
const detect = "\"repository\": \"yt-dlp/yt-dlp\"}}"
arr := strings.Split(string(res), detect)
arr = arr[:len(arr)-1]
for _, raw := range arr {
video := &Video{}
err = json.Unmarshal([]byte(raw+detect), video)
if err != nil {
log.Fatalln("Error on converting json: ", err)
}
output <- video
}
close(output)
}
// SearchOneVideoUrl
// This is only for searching video on YouTube using url.
// If url is not valid, just close `output` channel.
// Not support for playlist link.
func SearchOneVideoUrl(rawUrl string, output chan *Video) {
_, err := url.ParseRequestURI(rawUrl)
if err != nil {
close(output)
return
}
ytCommand := exec.Command(
YtDlpPath,
rawUrl,
"--skip-download",
"--no-playlist",
"--dump-json",
)
res, err := ytCommand.Output()
if err != nil {
log.Fatalln("Error during getting output: ", err)
}
video := &Video{}
err = json.Unmarshal(res, video)
if err != nil {
log.Fatalln("Error on converting json: ", err)
}
output <- video
close(output)
}
// ExtractPlaylist Extract video info from playlist.
// this is not support for searching playlist.
// So you have to input playlist url directly.
func ExtractPlaylist(playlistUrl string, amount int, output chan *Video) {
if amount <= 0 {
close(output)
return
}
ytCommand := exec.Command(
YtDlpPath,
playlistUrl,
"--skip-download",
"--yes-playlist",
"--playlist-end="+fmt.Sprint(amount),
"--dump-json",
)
res, err := ytCommand.Output()
if err != nil {
log.Fatalln("Error during getting output: ", err)
}
const detect = "\"repository\": \"yt-dlp/yt-dlp\"}}"
arr := strings.Split(string(res), detect)
arr = arr[:len(arr)-1]
for _, raw := range arr {
video := &Video{}
err = json.Unmarshal([]byte(raw+detect), video)
if err != nil {
log.Fatalln("Error on converting json: ", err)
}
output <- video
}
close(output)
}