[For beginners] Let’s get comments by linking TouchDesigner and YoutubeLive vol.2

miwa_maroon
5 min readMay 31, 2021

Video Description

This is a continuation of vol1.
We’re actually using the API to get comments!
Click!

In the last vol.1, I used google cloud plattform to get API keys.
I apologize for the boring environment setup.

From here on, we’re going to play around with the Youtube Data API.
We’ll finally be able to play around with the Youtube Data API and get live chat!

This is the API to use this time!

The first one is

Youtube Data API

Dere Dere Den
It’s got a sound effect that says “yeah!

This is the big brother of the Youtube API.

-Searching for videos
-Editing Thumbnails
-Uploading Videos
and so on.

You can do basic operations related to Youtube.

In this article, we will use the parameters we created in the previous article to get the list of videos.

https://www.googleapis.com/youtube/v3/videos
I use it.

If you introduce yourself with API key and then say
Video Id and say I want information about this video.

My brother will do it!

and he’ll bring it to you.

That’s great! I’m always grateful for your help.
In the “items” you get, there is a
chat id
In the information (items) that you have retrieved, there is a chat id.
I’m going to put this one in my pocket.

And the second one.

Youtube LiveStreaming API

Boom! This is a sound effect.

This is a special unit of Youtube that specializes in Live.
It has a function that says, “Leave it to me to handle Youtube Live.
I just got this from my brother.
chat id
with the API key and other parameters.
and other parameters.

  • The time the LiveChat started.
  • The account name of the person who sent the LiveChat
  • SuperChat

I’ll bring you any information (items) about Live, such as
That’s the Special Forces!

This time, we will use the following method to get information about LiveChat.
https://www.googleapis.com/youtube/v3/liveChat/messages
and get the LiveChat content and account name.

Perform asynchronous processing

How exactly are you going to implement this in TouchDesigner?
I’ll tell you how.

In TextDAT

import requests

url =’https://www.googleapis.com/youtube/v3/videos'

data = requests.get(url)

and run this script at any timing.

TouchDesigner will stop!

(By the way, the above code is not working or correct because it’s a devil’s advocate.)

This is too inconvenient!
The solution to this is

Web Client DAT!!

What is that? Where is it? I’ve never used it.
I’ve never used it.
It’s by far the best DAT out there.
You can use APIs like this one to retrieve information from the web and pass it back.

If you use this, TouchDesiigner will never stop!

Well, why not?
Because Web Client DAT is an asynchronous process. It’s here! Asynchronous processing!

Asynchronous processing means that
Asynchronous processing refers to a method that allows you to execute a task while another task is being executed, without stopping the processing.

You don’t have to stop the TouchDesigner processing while the API itself is fetching the video!

In other words, if you were running with textDAT earlier, the TouchDesigner processing stopped until the API processing was completed.
That’s right. Yes, that’s right.

I want to say something about WebClientDAT.

I’ve explained many things up to this point.
I’m finally back to the TouchDesigner screen.
In the previous section, we merged the APIkeys and IDs into the Input of the Web Client DAT.
Now, let’s pull the nodes from the merge and put them on the Web Client DAT.

Okay, I’ve added the API to the url.
There you go, GET!

And then…
What? It says that the API key is not valid and I can’t get it…

Hey! Why are you doing this?
This often happens.

This is because the Input is in the upper part.
I need to put it at the bottom.

Please be careful.

The Web Client DAT to get the chad id is
Put the chat id into another textDAT as follows
Open TextDAT(callback) from the bottom right of the WebClientDAT

# me — this DAT.

# webClientDAT — The connected Web Client DAT

# statusCode — The status code of the response, formatted as a dictionary with two key-value pairs: ‘code’, ‘message’.

# headerDict — The header of the response from the server formatted as a dictionary. Only sent once when streaming.

# data — The data of the response

import json

table = op(‘chatid’)

commentOp = op(‘comment’)

table = op(‘chatid’)

def onConnect(webClientDAT): return

def onDisconnect(webClientDAT): return

def onResponse(webClientDAT, statusCode, headerDict, data):

table.clear()

commentOp.clear()

tdata = json.loads(data)

liveStreamingDetails = tdata[‘items’][0][‘liveStreamingDetails’]

if ‘activeLiveChatId’ in liveStreamingDetails.keys():

chat_id = liveStreamingDetails[‘activeLiveChatId’] table.appendRow([‘liveChatId’,chat_id])

return

The operator is mysteriously added, but please watch the video for details.
Once it’s converted to json, it’s retrieved from there and put into tableDAT.

The process of retrieving the LiveChat message is also written in the callback of the second WebClientDAT.

# me — this DAT.

# webClientDAT — The connected Web Client DAT

# statusCode — The status code of the response, formatted as a dictionary with two key-value pairs: ‘code’, ‘message’.

# headerDict — The header of the response from the server formatted as a dictionary. Only sent once when streaming.

# data — The data of the response

import json

commentOp = op(‘comment’)

def onConnect(webClientDAT): return

def onDisconnect(webClientDAT): return

def onResponse(webClientDAT, statusCode, headerDict, data):

tdata = json.loads(data)

for item in tdata[‘items’]:

usr = item[‘authorDetails’][‘displayName’]

msg = item[‘snippet’][‘displayMessage’]

commentOp.appendRow([usr, msg]) return

This is also converted to json in the same way, and placed in another TableDAT (named “comment”).
This is the process after the GET in WebClientDAT.

I got youtube livecomment.

That’s how we were able to get the Live comments using the Youtube API!
From here, let’s make it visual.
In the next vol3

see you!

And…
If you think you can expand the possibilities of online events with TouchDesigner
If you have any questions, please feel free to contact me.

--

--

miwa_maroon

I want to bring a smile to someone's life. Use Touchdesigner.