by Daniel, on 5th Jul, 2013

10 Yetis - Facebook Competition Tab

Forrk were asked by 10Yetis to create an interesting and dynamic competition page tab for their Facebook page. 10Yetis has over 4,000 likes on Facebook so it was important the tab worked well to engage exisiting users as well as attract new 'likes'.

The brief was a short one (always difficult) - we were just informed it was to be a competition page tab based on Social Media Survival.

The below is what we came up with in full:

 

 

As you can see we intrepreted Survival and came up with a cool and quirky 'at sea' scene complete with textured backgrounds to give depth. 

by Daniel, on 7th Jan, 2013

Finding a short Twitter Username (Mission Impossible)

Hello, this is my first every blog post for Forrk, a Web Development agency which I have co-founded with Ross Tanner & Andy Barr (& may many more follow)

You may notice if you clicked any of the links I have provided above to my esteemed colleagues profiles that they both go to their respective owners twitter profiles. Why? Because (in my opinion) this is almost always how best to get an idea of somebody in the online environment these days. Facebook, whilst remaining as popular as ever is a slightly more private place where people often offer a restrictive profile view to those users who are not already 'Friends'  of the user. Twitter however is a whole different ball game and although they do offer the opportunity for users to screen their followers it is an option many do not take up. 

In the Web Design/Development field I follow a lot of advice and read even more blogs based purely on the person in questions presence on Twitter. This in its-self says a lot about the positive influence having good social networking profiles can have for you or your business. But I'm not here to sell you on social networking (you should already be sold). I'm here to help you find a short username because in Social Media nothing is more valuable real estate than your username.

A good twitter username is important for all the obvious reasons and then you remember that Twitter imposes a 140 character limit on Tweets which means having a short username can help people conversing with you get those extra word in the conversation. That and they look MUCH cooler.

So the challenge was to try and find the shortest possible username currently available on Twitter. I started by looking at the Twitter V2 docs but found to my dismay the Twitter rate limit for authorised user/lookup requests to just 150 per hour. Now my early estimations for a three (letter only) character username told me I would need to check over 17,575 combinations which would take about 117 hours if we went via the API route. This was clearly not an option.

So I played around in my browser by requesting invalid username such as (http://twitter.com/hghdguyrgyg) and noted these went to 404 pages. Success - this allowed me to separate the invalid/available usernames from the names already taken.

First of all I mocked up a basic PHP script which generated every possible combination of usernames and via CURL checked to see if the profile was valid. This worked, however, it still took some time to run and I didn't like waiting for the page to buffer usernames to get the most recent results so I tried a different tact (and I also needed to try 4 letter usernames). I set up the loop in Javascript and used AJAX/PHP handle the CURL checking and returning of results.

Now for the code:

First off below is my complete JS Code. I will explain each part in detail.

$(document).ready(function() {
var count = 0;
dance:  
for (var first = 97; first < 123; first++) 
 {
  for (var second = 97; second < 123; second++)  
   {
    for (var third = 97; third < 123; third++)  
     {
      for(var fourth = 97; fourth < 123; fourth++)
       {
        var username = String.fromCharCode(first,second,third,fourth);
        $.ajax({
          dataType: 'json',
          url: "http://192.168.1.40/username/twitter.php?username="+username,
         cache: false
        }).success(function( data ) {
          var count = parseInt($('#counter').text());
          $( "#counter" ).html( count+1 );
          if(data.status =='available')
          {
           $("body").append('The twitter username - '+data.username+' appears to be available'); 
          } 
          else 
          { 
           console.log(data); 
          } 
          }); 
          count++; 
       } 
     } 
   } 
 } 
});

First and foremost we start with the standard document ready. Then I have labelled the for loop so I/you can break it wherever necessary to avoid running through each username every time you want to run the script.

So here what we are doing is looping through each ASCII character (a-z) on four different levels which will help me produce a four letter username beginning with 'aaaa', 'aaab','aaac' etc etc. You could potentially increase the number of characters by nesting further loops:

 for (var first = 97; first < 123; first++) 
 {
  for (var second = 97; second < 123; second++)  
   {
    for (var third = 97; third < 123; third++)  
     {
      for(var fourth = 97; fourth < 123; fourth++)
       {

Once the ASCII numbers are generated I convert them into a char string using:

 var username = String.fromCharCode(first,second,third,fourth);

Now we have the username generated and ready to verify with twitter I send it off as a parameter to a PHP script (which I will also run through) to check with twitter.

 for (var first = 97; first < 123; first++) 
 $.ajax({
          dataType: 'json',
          url: "http://192.168.1.40/username/twitter.php?username="+username,
         cache: false
        }).success(function( data ) {
                var count = parseInt($('#counter').text());
                $( "#counter" ).html( count+1 );
                if(data.status =='available')
                {
                    $("body").append('

The twitter username - '+data.username+' appears to be available'); } else { console.log(data); } }); count++;

So above I am receiving the result from my PHP script via AJAX in JSON format. If my script advises the username is available I simply append the username to my HMTL body. If not I have console.log the return but this is not necessarily important or something you need to do. Please note above I have also used a counter to visually aid in how many usernames I have checked, again this is optional.

This is it for our Javascript. To recap: We have generated - using for loops - every possible combination of (letter only) four character usernames and sent them off to a PHP script via AJAX for checking. Once the result is returned we handle accordingly.

So below we have the PHP which handles the actual checking of the username and all of the 'hard work', once again the full code is below and I will explain in detail each section:

    $username = $_GET['username'];
    if(!empty($username) && $username != '')
    {
          $url="http://twitter.com/".$username;
          
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $return = curl_exec($ch);
          $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
                              
          if ($http_status == "404")
          {
                echo json_encode(array('status' => 'available', 'username' => $username));
          }
          else
          {
                echo json_encode(array('status' => 'not available', 'username' => $username));
          }  
    }

So to begin with we verify that we do indeed have the username we passed through in the URL.

 
 $username = $_GET['username'];
 if(!empty($username) && $username != '')
 {

Once this has been done we generate the URL we are going to check which is the standard Twitter profile URL with the generated username passed in.

 
 $url="http://twitter.com/".$username;

Now we initiate CURL, set our options and execute - we need SSL_VERIFYHOST set to false because all Twitter profile URL's are https. All we really want is the HTTP return header from the page so we store this in a variable called $http_status which we can use for checking.

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $return = curl_exec($ch);
  $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  curl_close($ch);

Finally we have a very simple IF statement which checks whether we have stumbled upon a valid profile or an available one. The result is echoed in a JSON encoded array which is in return received by the earlier AJAX script to output on the page.

  if ($http_status == "404")
  {
        echo json_encode(array('status' => 'available', 'username' => $username));
  }
  else
  {
        echo json_encode(array('status' => 'not available', 'username' => $username));
  }  

After running through 17,576 three letter usernames I found 16 which were not already being used and all of these had been made unavailable by twitter for some reason or other. I am currently running through the four letter usernames and will post any I find available (that I do not want!) in this post and on our Forrk Twitter so check back and give us a follow!

request a callback

Why not check us out on some of our social networking accounts. Alternatively, if you would prefer to contact us directly, you can always drop us a line below or on hello@forrk.co.uk or tell us about your project and get a quote.

GET A QUOTE Back to top