contador web Saltar al contenido

How to extract information from a YouTube video using Shell Script

For those who are Content Producers with Linux and live wondering the rebound of the video you just launched on Youtube , but is tired of constantly opening the video through the browser, waiting to load to get the video data like:

  • Subscribers;
  • Likes;
  • No likes / dislikes;
  • Comments

Not to mention also those who follow a video that he liked and / or commented and want to follow if there are new comments and etc. Let's create a Shell Script that will pass us this data with a simple command in the linux terminal! This exercise is part of the series. In magic, Shell Script! .

First of all we need to create a head, besides the Shebang (#!), the header needs author data, link or email, license and software version, so it would look like this:

#!/bin/bash # author:  Marcos Oliveira  # license: MIT # version: 1.0 

After that we need to create a function to get youtube data, we create a temporary file in / tmp with the command mktemp we download the youtube page for any video and play the output of wget in our temporary file that we store in a variable, read the lines that start with # (comments):

# Rodamos o comando mktempo numa subshell, logo seu contedo 
ser armazenado em page_video_youtube 
page_video_youtube=$(mktemp)
 

# Com a linha abaixo o contedo da paina HTML ser guardado no

arquivo referenciado pela varivel

wget "https://www.youtube.com/watch?v=hj-aDdRasGY" -O

$page_video_youtube 

Next step now filter the content of the data we download, after opening the file in the text editor, we find that the line showing the channel id has a string This is the only page that can help us get channel data like: Title and Subscribers.

Since Youtube uses a standard for channel addresses, so we can filter the data using Sed and by downloading the main channel page that has the most accurate information and we will arm the content we want, so as not to pollute our environment, we will re-create temporary files and throw the data in the referenced variables.

Read the comments on code

  # Pega o id do canal e armazena na varivel, rodando numa 
subshell id_do_canal=$(cat "$page_video_youtube" | grep 
'channelId' | sed 's/.*="//' | sed 's/".*//')  # Criamos outro arquivo temporrio na pasta /tmp que ser nosso 
canal page_canal=$(mktemp)  # Sabemos que o Youtube tem esse padro, ento passamos o ID que
 pegamos e adicionamos ao padro de url do Youtube # Fizemos o wget rodar "silenciosamente" jogando a sada para o 
vazio, podemos fazer no anterior tambm wget "https://www.youtube.com/channel/$id_do_canal" -O 
"$page_canal" 2>/dev/null 

Okay, we already have all the files we need, so now we only have to filter the contents and store them in a variable to display in a next function that we will take to it via array, read the comments to understand the lines, check that now we are passing the url via parameter to function, because we will send it by prompt:

function get_data_youtube () { # We filter the number of subscribers, video title, views and likes or not with sed and awk unsubscribed num = $ (cat "$ page_channel" | grep yt-subscription-button-subscribe-count-branded-horizontal | sed 's /) *> / / g '| awk '{print $ NF}') title_video = $ (cat $ page_video_youtube | sed '/ title / {p; q;}' | grep title | sed 's /) *> // g '| uniq | sed 's / ... YouTube. * //') views_video = $ (cat $ page_video_youtube | grep watch-view-count | sed 's /) *> // g ') likes = $ (cat $ page_video_youtube | grep like-button-renderer-like-button-unclicked | sed 's /) *> // g; s / // g ') dislikes = $ (cat $ page_video_youtube | grep like-button-renderer-dislike-button-unclicked | sed 's /) *> // g; s / // g ') echo "Channel Name: $ channel_title" echo "Number of subscribers: $ num_enlisted" echo "Video title: $ title_video" echo "Views: $ views_video" echo "Likes: $ likes" echo "No likes: $ dislikes"}

Now just create a condition that forces the user to enter the video and you're done, see the ready script below, copy the code, paste it into a file, give it permission to run:

YouTube Shell Script In the next article in the series we will add no comments, Colors is a condition for when there is no URL, our script takes the data from latest video! And then to finish this series, we'll have a little surprise !

If you do not have much knowledge in Shell script and are looking for an effective way to learn from scratch? Or maybe you already have shell knowledge but want to evolve and learn how to create manuals, processes, games, animations, installers and more?

So don't miss the opportunity to see the "Extremely Advanced Shell Script Course" that we offer along with the Terminal Root guys, just 3 courses for the price of one, meet here.

See you next time and good studies! space line End of text _____________________________________________________________________________ See any errors or would you like to add any suggestions to this article? Collaborate, click here.

Rate this post