Fetching data from YouTube using Perl

As I am trying to follow how my screencasts are doing on YouTube I keep visiting the web site checking out the number of subscribers and the number of views of the videos. It's getting a bit boring so I thought I should automate it.

YouTube provides API for a lot of things. I wanted to be able to fetch the statistics of my account.

I started by installing the Padre on Strawberry Perl for Windows package but it would work on any other distribution of Perl and on any other operating system.

Then I went to Meta CPAN to search for something related to Youtube and settled with WebService::GData. I went to the CPAN shell in the Strawberry -> Tools submenu and typed in

  cpan> install WebService::GData

After brief reading of the docs and some copy-pasting I got this script:


  use 5.010;
  use strict;
  use warnings;

  use WebService::GData::YouTube;
  my $yt = WebService::GData::YouTube->new();

  my $p = $yt->get_user_profile('gabor529');
  say $p->about_me;
  my $s = $p->statistics;
  say $s->view_count;
  say $s->subscriber_count;
  #say $s->video_watch_count;
  say $s->total_upload_views;

This would fetch my user profile - without even logging in to YouTube - and print out the statistics. I have 105 subscribers as of this writing and I hope I can get to 200 before the end of the month. Would you help me in that?

Then I wanted to send the resulting data to myself via Gmail. Another short search on Meta CPAN for Gmail and I found Email::Send::SMTP::Gmail. Some more copy pasting and here is the script that would send an e-mail using my gmail-account to myself. I guess I could use the same code to send to anyone else as well.

  use Email::Send::SMTP::Gmail;
  my $mail=Email::Send::SMTP::Gmail->new( -smtp=>'gmail.com',
                                          -pass=>'google and me');

  my $text = '';
  $text .= "view count " . $s->view_count . "\n";
  $text .= "subscribers " . $s->subscriber_count . "\n";
  $text .= "total views"   . $s->total_upload_views . "\n";

            -subject=>'youtube update',
            -body=> $text,
  #         -attachments=>'full_path_to_file'

That was all of it. It took me almost 15 minutes to write this! (And 3 more hours to prepare the screencast :)

So go ahead. Express the fact that you like the screencasts and add yourself as a subscriber to my channel.

BTW I added a new page with some embedded code that will show my screencasts.

ps. That is not really my e-mail address. I was just using it for this demo.

If you want to contact me I have my info on my contact page.

Published on 2011-06-27 by Gabor Szabo

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
blog comments powered by Disqus
Python, Dart, JavaScript, and more.

Google Plus Twitter RSS feed