How to obtain a feed of comments entered through the 'chat' box during a YouTube live broadcast?

青春壹個敷衍的年華 提交于 2019-11-26 09:35:36

问题


The YouTube API enables users to obtain a comments feed, e.g. via https://gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments?orderby=published.

However, when I try to do just that with the video ID of a live stream, the result is always empty, no matter how many comments have been submitted. The only difference between a live video and any other video (or recording of a live stream) is that the \'comments\' section is replaced with a \'chat\' box, whose comments seem not to be available via the API.

When the stream is stopped, all comments submitted through the chat box \'disappear\' entirely and can no longer be accessed. However, all comments submitted after the live broadcast has been archived (i.e. the recording has been made available) show up in the comments feed.

For a real-time application I need to access the \'chat\' comments while the broadcast is still live, to retrieve user-submitted questions.

Is there any way to do this?


回答1:


It is now possible to return chat messages for your own broadcasts using the LiveChatMessages endpoint as part of the YouTube Live Streaming API.

When creating a new liveBroadcast object, a liveChatId String will be returned as part of that liveBroadcast's snippet. Pass your broadcast's chat ID to LiveChatMessages/list endpoint's liveChatId parameter, and id, snippet, and authorDetails into the part parameter.

HTTP GET https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId={liveChatId}&part=id%2C+snippet%2C+authorDetails&key={YOUR_API_KEY}

This will return an array of liveChatMessage resources. The actual chat message is contained in the textMessageDetails dictionary as the value for the messageText key.

"textMessageDetails": {
  "messageText": string
}



回答2:


Ibrahim Ulukaya, a member of Google's Developer Relations team focusing on the YouTube API, stated the following on a similar quesiton (How to get chat content of Youtube live event in Java):

The API doesn't have connection to live chat at this moment. We are hoping to get that content incorporated into API soon.

via https://stackoverflow.com/a/26427743/1085891




回答3:


I came Up with a Basic Script for this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Starting();
        }

        public void Starting()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=Yu5Om0SH3No");

            Thread.Sleep(10000);

            //Find Comments
            IWebElement element = driver.FindElement(By.ClassName("comment-text"));
            Console.WriteLine("Text: " + element.Text);

            //Find User names
            IWebElement element2 = driver.FindElement(By.XPath(".//*[@class='g-hovercard yt-uix-sessionlink yt-user-name']"));
            Console.WriteLine("Username: " + element2.Text);



        }
    }
}

Will Need More Hours Of Work To Make It Read the Page as The Comments Flow.



来源:https://stackoverflow.com/questions/26229728/how-to-obtain-a-feed-of-comments-entered-through-the-chat-box-during-a-youtube

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!