Roman Toasov
Roman Toasov

Reputation: 821

PHP Extract youtube live chat

When there's live broadcast on youtube there's always a chat to the right of video player. I am trying to download messages from chat to my PHP page in order to filter trough them as they scroll really fast and i cannot catch up with them.

I noticed that when you open youtube page with live chat it runs GET to this URL every 10 seconds

https://www.youtube.com/live_comments?action_get_comments=1&video_id=<video_id>&lt=<timestamp of last recorded message>&format=proto&pd=10000&rc=26&scr=true&comment_version=1

If i do

$xml= file_get_contents('https://www.youtube.com/live_comments?action_get_comments=1&video_id=<video_id>&lt=<timestamp of last recorded message>&format=proto&pd=10000&rc=26&scr=true&comment_version=1');
echo "<textarea>{$xml}</textarea>";

I get following

<?xml version="1.0" encoding="utf-8"?>
<root>
    <latest_time>
        <![CDATA[1445398692]]>
    </latest_time>
    <return_code>
        <![CDATA[0]]>
    </return_code>
    <likes_data>
        <![CDATA[{"percent_dislikes":7,"num_likes":"1,383","num_dislikes":"91","percent_likes":93}]]>
    </likes_data>
    <comments>
        <![CDATA[EOcKGFsgkE4=]]>
    </comments>
</root>

I assume this would get me new messages that appear however i am currently testing on chat that has no people in it and i cannot confirm.

There's also POST request every 10 seconds however i am not trying to post just download.

My question is how do i download with PHP existing messages that show in the chat when you open page initially?

Upvotes: 1

Views: 1705

Answers (1)

Sanxofon
Sanxofon

Reputation: 981

This is not an adequate answer (no PHP, no code...), but if you are interested in a dirty and simple solution (grabbing a Youtube Live video Chat as JSON), you can use your browser to do so. In Firefox:

  1. Load video url in Firefox Browser, on top of video chat window.
  2. Select "Live Chat Replay" to get all messages of chat.
  3. Pause video.
  4. On FF Menu go to "Web Developer Tools" and then "Network".
  5. Mark checkbox of "Persistent Register" (optional)
  6. Select "XHR" only and filter results by "live_chat_replay".
  7. Play video and let it row...
  8. At the end, pause video again and at Top-Right of "Network" console find and clik on "HAR" selector.
  9. Select "Save all as HAR"
  10. Select file destination and save.
  11. Open "HAR" file in text/code editor (It's a JSON of JSONS)
  12. Parse it using your favorite languaje.
  13. Cheers!

Upvotes: 1

Related Questions