Fetches Youtube live chat messages with no authentication required.
- The request for fetching live chat is re-created by parsing the HTML content.
- Youtube API returns a
continuation
with new chat messages. This continuation is sent in the next API request to receive new messages and new continuation.
go get -u github.com/abhinavxd/youtube-live-chat-downloader/v2
package main
import (
"fmt"
"log"
"math/rand"
"net/http"
YtChat "github.com/abhinavxd/youtube-live-chat-downloader/v2"
)
func main() {
customCookies := []*http.Cookie{
{Name: "PREF",
Value: "tz=Europe.Rome",
MaxAge: 300},
{Name: "CONSENT",
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)
continuation, cfg, error := YtChat.ParseInitialData("https://www.youtube.com/watch?v=5qap5aO4i9A")
if error != nil {
log.Fatal(error)
}
for {
chat, newContinuation, error := YtChat.FetchContinuationChat(continuation, cfg)
if error == YtChat.ErrLiveStreamOver {
log.Fatal("Live stream over")
}
if error != nil {
log.Print(error)
continue
}
// set the newly received continuation
continuation = newContinuation
for _, msg := range chat {
fmt.Print(msg.Timestamp, " | ")
fmt.Println(msg.AuthorName, ": ", msg.Message)
}
}
}
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.