Youtube subscriber counter

On the Electric Imp forum someone used php and cron to pull the subscriber count from youtube, and display it on a big 7 segment display. But it could be done with less things, actually the Electric Imp can do all of it on its own (with help from the agent), and here is the code that can do it.

Agent code:

device.on("getYoutubeFollowers", function(msg)
{
  local url = "http://gdata.youtube.com/feeds/api/users/blddk"; //change username to match the one you want to check

  http.get(url).sendasync(function(resp)
  {
    if (resp.statuscode = 200)
    {
      local findBegin = "subscriberCount='";
      local findEnd = "'";
      local string = resp.body;
      local begin = string.find(findBegin) + findBegin.len();
      string = string.slice(begin,begin+10);
      local end = string.find(findEnd) + findEnd.len() -1;
      string = string.slice(0,end);

      server.log(string); //Return the number of followers
    }
    else
    {
      server.log("Could not get youtube followers: " + format("%i: %s", resp.statuscode, resp.body));
    }
  })
});

Device code:

getSubscribers()
{
  agent.send("getYoutubeFollowers", 0);
  imp.wakeup(60*60, getStats); //Check again in one hour
}
getSubscribers();

This will then check the subscribers count once every hour and return that with server.log(), here it should be fairly easy to grab that and use it in another function.

Agent only

If you want it to run on the agent only, you can use this instead

function youtubeSubscribers()
{
  local url = "http://gdata.youtube.com/feeds/api/users/blddk";

  http.get(url).sendasync(function(resp)
  {
    if (resp.statuscode = 200)
    {
      local findBegin = "subscriberCount='";
      local findEnd = "'";
      local string = resp.body;
      local begin = string.find(findBegin) + findBegin.len();
      string = string.slice(begin,begin+10);
      local end = string.find(findEnd) + findEnd.len() -1;
      string = string.slice(0,end);
      
      server.log(string); //Return the number of followers
    }
    else
    {
      server.log("Could not get youtube followers: " + format("%i: %s", resp.statuscode, resp.body));
    }
  });
  
  imp.wakeup(60*60, youtubeSubscribers);
}
youtubeSubscribers();

API limitation

So far it seems that not many are using the Electric Imp to get info from the youtube api, but as this gets more popular, or just until someone do something stupid, which could be accessing the api with an unnecessarily low delay between requests, you might need to find another way to contact the api. To make sure you wont run out of requests because of someone else’s mistake, you can host a php page yourself, then use that to pull the subscriber count.

Such a page could contain something as simple as this:

<?php
echo youtube_subscribers("blddk");
	
	
function youtube_subscribers($user)
{
	$xml = file_get_contents('http://gdata.youtube.com/feeds/api/users/' . strtolower($user));
	$xml = str_replace('yt:', 'yt', $xml);
	$xml = new SimpleXMLElement($xml);
	$subs = $xml->ytstatistics['subscriberCount'];
	return(trim($subs));
}
?>

This will only reply with the number of subscribers, which can easily be handled by the Electric Imp too.

Leave a Reply