Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavxd committed Mar 28, 2022
1 parent 06e5cb9 commit 82df5ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
)

func main() {
// Add your cookies here
// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
customCookies := []*http.Cookie{
{Name: "PREF",
Value: "tz=Europe.Rome",
Expand All @@ -35,9 +31,13 @@ func main() {
Value: fmt.Sprintf("YES+yt.432048971.it+FX+%d", 100+rand.Intn(999-100+1)),
MaxAge: 300},
}
// Google would sometimes ask user to solve a CAPTCHA before accessing it's websites.
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
// Adding cookies is OPTIONAL
YtChat.AddCookies(customCookies)

// Set customCookies as nil, if you do not require cookies.
continuation, cfg, error := YtChat.ParseInitialData("https://www.youtube.com/watch?v=5qap5aO4i9A", customCookies)
continuation, cfg, error := YtChat.ParseInitialData("https://www.youtube.com/watch?v=5qap5aO4i9A")
if error != nil {
log.Fatal(error)
}
Expand All @@ -60,6 +60,7 @@ func main() {
}
}


```

## Screenshot
Expand Down
11 changes: 6 additions & 5 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
)

func main() {
// Add your cookies here
// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
customCookies := []*http.Cookie{
{Name: "PREF",
Value: "tz=Europe.Rome",
Expand All @@ -22,8 +18,13 @@ func main() {
Value: fmt.Sprintf("YES+yt.432048971.it+FX+%d", 100+rand.Intn(999-100+1)),
MaxAge: 300},
}
// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites.
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
// Adding cookies is OPTIONAL
YtChat.AddCookies(customCookies)

continuation, cfg, error := YtChat.ParseInitialData("https://www.youtube.com/watch?v=5qap5aO4i9A", customCookies)
continuation, cfg, error := YtChat.ParseInitialData("https://www.youtube.com/watch?v=5qap5aO4i9A")
if error != nil {
log.Fatal(error)
}
Expand Down
15 changes: 10 additions & 5 deletions yt_chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ type InitialData struct {
}

var (
LIVE_CHAT_URL = `https://www.youtube.com/youtubei/v1/live_chat/get_%s?key=%s`
LIVE_CHAT_URL = `https://www.youtube.com/youtubei/v1/live_chat/get_%s?key=%s`
// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
customCookies []*http.Cookie
ErrLiveStreamOver error = errors.New("live stream over")
ErrStreamNotLive error = errors.New("stream not live")
)
Expand Down Expand Up @@ -246,15 +250,12 @@ func fetchChatMessages(initialContinuationInfo string, ytCfg YtCfg) ([]ChatMessa
return chatMessages, initialContinuationInfo, timeoutMs, nil
}

func ParseInitialData(videoUrl string, customCookies []*http.Cookie) (string, YtCfg, error) {
func ParseInitialData(videoUrl string) (string, YtCfg, error) {
req, err := http.NewRequest("GET", videoUrl, nil)
if err != nil {
return "", YtCfg{}, err
}

// Google would sometimes ask you to solve a CAPTCHA before accessing it's websites
// or ask for your CONSENT if you are an EU user
// You can add those cookies here.
for _, cookie := range customCookies {
req.AddCookie(cookie)
}
Expand Down Expand Up @@ -315,3 +316,7 @@ func FetchContinuationChat(continuation string, ytCfg YtCfg) ([]ChatMessage, str
}
return chatMessages, continuation, nil
}

func AddCookies(cookies []*http.Cookie) {
customCookies = cookies
}

0 comments on commit 82df5ae

Please sign in to comment.