Skip to content

Commit 1bf6fd3

Browse files
committed
Fix ytools-info because likes are now missing
1 parent 5b0a524 commit 1bf6fd3

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $ ytools-search Black Mambo
99
...
1010
$ ytools-info 2
1111
Glass Animals - Black Mambo
12-
14.643.046 Views 81.526 ▼ 1.626 Published on 17 Feb 2015
12+
14.643.046 Views 81.526 Likes Published on 17 Feb 2015
1313

1414
Our new record “How To Be a Human Being” featuring “Youth” and
1515
...

cmd/ytools-info/main.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"github.com/codesoap/ytools"
77
"os"
8-
"strings"
98
)
109

1110
type Info struct {
@@ -16,7 +15,6 @@ type Info struct {
1615
// to load something like this:
1716
// https://www.youtube.com/annotations_invideo?video_id=DuoTdnq_OqE
1817
Likes string
19-
Dislikes string
2018
Date string
2119
Description string
2220
}
@@ -49,9 +47,20 @@ type VideoPrimaryInfoRenderer struct {
4947
}
5048
}
5149
}
52-
SentimentBar struct {
53-
SentimentBarRenderer struct {
54-
Tooltip string
50+
VideoActions struct {
51+
MenuRenderer struct {
52+
TopLevelButtons []struct {
53+
ToggleButtonRenderer struct {
54+
TargetId string
55+
DefaultText struct {
56+
Accessibility struct {
57+
AccessibilityData struct {
58+
Label string
59+
}
60+
}
61+
}
62+
}
63+
}
5564
}
5665
}
5766
DateText struct {
@@ -92,8 +101,8 @@ func main() {
92101

93102
func printInfo(info Info) {
94103
fmt.Println(info.Title)
95-
f := "%s ▲ %s ▼ %s %s\n\n"
96-
fmt.Printf(f, info.Views, info.Likes, info.Dislikes, info.Date)
104+
f := "%s %s %s\n\n"
105+
fmt.Printf(f, info.Views, info.Likes, info.Date)
97106
fmt.Println(info.Description)
98107
}
99108

@@ -122,7 +131,7 @@ func extractInfo(dataJson []byte) (info Info, err error) {
122131
if err = fillViews(&info, primaryInfo); err != nil {
123132
return
124133
}
125-
if err = fillVotes(&info, primaryInfo); err != nil {
134+
if err = fillLikes(&info, primaryInfo); err != nil {
126135
return
127136
}
128137
if err = fillDate(&info, primaryInfo); err != nil {
@@ -152,15 +161,14 @@ func fillViews(info *Info, data VideoPrimaryInfoRenderer) error {
152161
return nil
153162
}
154163

155-
func fillVotes(info *Info, data VideoPrimaryInfoRenderer) error {
156-
tooltip := data.SentimentBar.SentimentBarRenderer.Tooltip
157-
numbers := strings.Split(tooltip, " / ")
158-
if len(numbers) != 2 {
159-
return fmt.Errorf("could not parse likes and dislikes")
160-
}
161-
info.Likes = numbers[0]
162-
info.Dislikes = numbers[1]
163-
return nil
164+
func fillLikes(info *Info, data VideoPrimaryInfoRenderer) error {
165+
for _, button := range data.VideoActions.MenuRenderer.TopLevelButtons {
166+
if button.ToggleButtonRenderer.TargetId == "watch-like" {
167+
info.Likes = button.ToggleButtonRenderer.DefaultText.Accessibility.AccessibilityData.Label
168+
return nil
169+
}
170+
}
171+
return fmt.Errorf("like count not found")
164172
}
165173

166174
func fillDate(info *Info, data VideoPrimaryInfoRenderer) error {

0 commit comments

Comments
 (0)