summaryrefslogtreecommitdiff
path: root/ytparser/ytparser.go
diff options
context:
space:
mode:
Diffstat (limited to 'ytparser/ytparser.go')
-rw-r--r--ytparser/ytparser.go23
1 files changed, 14 insertions, 9 deletions
diff --git a/ytparser/ytparser.go b/ytparser/ytparser.go
index 57c2a31..fe9ae66 100644
--- a/ytparser/ytparser.go
+++ b/ytparser/ytparser.go
@@ -22,6 +22,7 @@ type Item struct {
Thumb string
ChannelTitle string
ChannelUrl string
+ Published string
}
func (item Item) Format(t *template.Template) string {
@@ -38,10 +39,8 @@ func (item Item) String() string {
}
func parsejson(data string) ([]Item, error) {
- //fmt.Println(data)
dec := json.NewDecoder(strings.NewReader(data))
-
depth := 0
isArray := false
isValue := false
@@ -116,6 +115,12 @@ func parsejson(data string) ([]Item, error) {
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
}
}
@@ -123,20 +128,18 @@ func parsejson(data string) ([]Item, error) {
}
}
return items, nil
- //fmt.Println(names)
}
func PrintItems(items []Item, format string) {
t := template.Must(template.New("items").Parse(format))
for _, i := range items {
fmt.Println(i.Format(t))
- //fmt.Println(i)
}
}
-func request(query string, page int) (string, error) {
+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", q, page)
+ 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
@@ -150,12 +153,14 @@ func request(query string, page int) (string, error) {
startData := body[idx:]
idx = bytes.Index(startData, []byte(";\n"))
startData = startData[:idx]
- //fmt.Println(string(startData))
return string(startData), nil
}
-func Search(query string, page int) ([]Item, error) {
- data, err := request(query, page)
+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
}