Random YouTube Videos

It’s funny how seemingly trivial things can turn into rather significant undertakings...

Like when a colleague of mine asked a very benign-sounding question over Skype about generating random 11-character strings.

My first response to the question of generating random 11-character strings was to regurgitate some code I found on php.net:

$str = preg_replace(’/([ ])/e’, ’chr(mt_rand(97,122))’, ’           ');

When I discovered the basis for the question - to create random YouTube video IDs - I realised I would have to change it slightly:

$vid = preg_replace(’/([ ])/e’,
    'substr( "0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
             mt_rand(0,62), 1 )', '           ');

However, when I ran a script that generated random YouTube links based on these random video IDs, I discovered that I was basically looking for needles in a very, very, very large haystack. That is, most of the video IDs that I generated randomly did not actually exist (the numeric space in those 11-character strings is several orders of magnitude greater than the number of YouTube videos that actually exist).

So that brief Skype chat has turned into a rather interesting side project, the aim of which is to create a database of several thousand truly unique YouTube video IDs.

One of the reasons we want the random video IDs is to power randomyoutube.net. This site was built with Drupal 7 in about 45 minutes (yay Drupal!) and the back-end that does the searching, storing and retrieving of the video IDs uses Zend Framework. Although Zend Framework is probably overkill for our current needs, I must say that the Zend_Gdata_YouTube class definitely makes working with the YouTube API very, very painless.

There are, however, some much more interesting things that can be done with random YouTube video IDs, some of which I’ll share with the world in a later post.

Yet Another Programming Blog

Where James Gordon rambles about PHP and web development in general.

Find me on Twitter Find me on Stack Exchange Find me on Github Subscribe