The Pitch
With all that's happened in the year 2020, have you ever wondered how happy the comments in YouTube videos are? Chances are you probably haven't, but now, with the YouTube HappyMeter, you can do just that. Using the power of a microcontroller, a servo, and less than basic artistic skills, the YouTube HappyMeter will show you a very basic estimate of the positivity in the dreaded YouTube comment section.
disclaimer: happiness/sadness will not be guaranteed by the HappyMeter. Watch videos at your own discretion.
Building The Meter
The HappyMeter is not a very difficult contraption to build. I built this one with a sheet of copy paper, a small cardboard box, and some tape, in addition to the aforementioned resources.
*placeholder - image of box*
the servo that moves the arrow for the meter is placed through a hole in the top of the box and then held up with tape or some other securing measure. Wires come out of another opening on the side of the box. (See circuit diagram for proper wiring). The arrow is placed on top of the servo so it can rotate within the desired 180 degree range. Just a little decoration and the meter is suited to your design taste!
Coding the Meter
The HappyMeter uses two APIs:
twinword's Sentiment Analysis API, found at
https://rapidapi.com/twinword/api/sentiment-analysis/details
ytdlfree's Youtube v3 API, found at
https://rapidapi.com/ytdlfree/api/youtube-v31/details
Big props to them for making this project possible.
We start by creating webhooks for each API.
the URL endpoint for Youtube v3 is https://youtube-v31.p.rapidapi.com/commentThreads and it takes 2 headers and 3 query parameters:
Headers:
- x-rapidapi-key: obtainable on RapidAPI, tied to account
- x-rapidapi-host: also obtainable on RapidAPI, tied to API itself
Query Parameters:
- part : default value is snippet, don't change it
- videoId : insert YouTube video ID - for Particle we set this as {{{PARTICLE_EVENT_VALUE}}} for input data
- maxResults : doesn't actually do anything as of 1/20/21, API always returns 100 comments
For a Response Template we use {{items.x.snippet.topLevelComment.snippet.textDisplay}}
, to grab the text of the comment. Replace x with each entry you'd like to include, like
{{items.0.snippet.topLevelComment.snippet.textDisplay}}
{{items.1.snippet.topLevelComment.snippet.textDisplay}}
{{items.2.snippet.topLevelComment.snippet.textDisplay}}
//etc
A fair warning is that the following call won't work if the string is too long, so be wary of how many comments are being pulled.
Next, the webhook for Sentiment Analysis.
the URL endpoint for Sentiment Analysis is https://twinword-sentiment-analysis.p.rapidapi.com/analyze/ and it takes 2 headers and 1 query parameter:
Headers:
- x-rapidapi-key: obtainable on RapidAPI, tied to account
- x-rapidapi-host: also obtainable on RapidAPI, tied to API itself
Query Parameter:
- text : the string to analyze. for Particle, we use {{{PARTICLE_EVENT_VALUE}}} for input data.
For a Response Template we simply use {{score}}
, to grab the numerical score that the text analysis we gave the string.
Now that we have webhooks, we can move to the Particle Web IDE and put in some real lines of code.
Lines 2-6 shown here define some key variables and objects.
VIDEOID will be the data we feed into the Youtube v3 webhook call. TOPCOMMENT will be the data we get back from said call and it will then be fed to the Sentiment Analysis Call. myservo will control the arrow on the meter, and pos will be the value that holds the servo angle. For now, they are just global variables.
As stated on line 10, lines 11 and 12 connect particle to the webhooks. The handlers, in this case commentHandler and vibeHandler, are what do all the work, so it is essential those are listed correctly. The Third Parameter is the device that will be connected to the webhook to receive data.
commentHandler is the handler for the response from Youtube v3. All it does is take the string of the comments from the webhook, copy it to the global variable TOPCOMMENT, wait a second, then publish the Sentiment Analysis event, with the youtube comment string attached to it.
This is the handler for the data returned from the Sentiment Analysis API. The sentiment score is copied to a float, which is then multiplied by a large quantity, as most scores range between -0.4 and 0.4. This value then changes the position, which causes the arrow to move left or right based on the scale and sign of the score.
Lastly, the function that runs the code. Here are lines 11 and 12 from before, with a Serial.begin in case you would like to print values in the process, along with the servo attachment and a reset of the servo back to neutral, in the middle. Finally, the webhook call is made, and the handlers shown previously do their work.
Full code and schematics included below.
Comments