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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
package ytparser
import (
"text/template"
"encoding/json"
"fmt"
"net/http"
"bytes"
"io"
"io/ioutil"
"strings"
"net/url"
)
const initDataString string = "window[\"ytInitialData\"] = "
const baseUrl string = "https://youtube.com"
type Item struct {
Id string
Title string
Url string
Thumb string
ChannelTitle string
ChannelUrl string
Published string
}
func (item Item) Format(t *template.Template) string {
var b strings.Builder
err := t.Execute(&b, item)
if err != nil {
panic(err)
}
return b.String()
}
func (item Item) String() string {
return fmt.Sprintf("id: %s, title: %s, url: %s, thumb: %s", item.Id, item.Title, item.Url, item.Thumb)
}
func parsejson(data string) ([]Item, error) {
dec := json.NewDecoder(strings.NewReader(data))
depth := 0
isArray := false
isValue := false
var items []Item
var item Item
var names []string
nbItems := 0
for {
tok, err := dec.Token()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
switch t := tok.(type) {
case json.Delim:
if t == '{' {
depth++
} else if t == '}' {
depth--
names = names[:depth]
}
isArray = t == '['
isValue = false
case string:
if !isArray {
if !isValue {
if t == "videoRenderer" {
if nbItems > 0 {
items = append(items, item)
}
item = Item{}
nbItems++
}
if depth > len(names) {
names = append(names, t)
} else {
names[depth - 1] = t
}
//fmt.Println(t, depth, len(names), names[depth - 1])
isValue = true
} else {
//fmt.Println(names[len(names) - 1])
if names[depth-1] == "videoId" {
item.Id = t
item.Url = fmt.Sprintf("https://youtube.com/watch?v=%s", t)
}
if depth >= 3 && names[depth-3] == "title" &&
names[depth-2] == "runs" &&
names[depth-1] == "text" {
item.Title = t
}
if depth >= 3 && names[depth-3] == "ownerText" &&
names[depth-2] == "runs" &&
names[depth-1] == "text" {
item.ChannelTitle = t
}
if depth >= 6 &&
names[depth-6] == "ownerText" &&
names[depth-5] == "runs" &&
names[depth-4] == "navigationEndpoint" &&
names[depth-3] == "commandMetadata" &&
names[depth-2] == "webCommandMetadata" &&
names[depth-1] == "url" {
item.ChannelUrl = baseUrl + t
}
if depth >= 4 &&
names[depth-4] == "videoRenderer" &&
names[depth-3] == "thumbnail" &&
names[depth-2] == "thumbnails" &&
names[depth-1] == "url" {
item.Thumb = t
}
if depth >= 3 &&
names[depth-3] == "videoRenderer" &&
names[depth-2] == "publishedTimeText" &&
names[depth-1] == "simpleText" {
item.Published = t
}
isValue = false
}
}
default:
}
}
return items, nil
}
func PrintItems(items []Item, format string) {
t := template.Must(template.New("items").Parse(format))
for _, i := range items {
fmt.Println(i.Format(t))
}
}
func request(query string, page int, lang string) (string, error) {
q := url.QueryEscape(query)
url := fmt.Sprintf("https://www.youtube.com/results?search_query=%s&page=%d&hl=%s", q, page, lang)
res, err := http.DefaultClient.Get(url)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
idx := bytes.Index(body, []byte(initDataString))
idx += len(initDataString)
startData := body[idx:]
idx = bytes.Index(startData, []byte(";\n"))
startData = startData[:idx]
return string(startData), nil
}
func Search(query string, page int, lang string) ([]Item, error) {
if lang == "" {
lang = "en"
}
data, err := request(query, page, lang)
if err != nil {
return nil, err
}
return parsejson(data)
}
|