2015, ഓഗസ്റ്റ് 19, ബുധനാഴ്‌ച

How URL shortening works

Theoretical background

You need a Bijective Function f. This is necessary so that you can find a inverse function g('abc') = 123 for your f(123) = 'abc' function. This means:
  • There must be no x1, x2 (with x1 ≠ x2) that will make f(x1) = f(x2),
  • and for every y you must be able to find an x so that f(x) = y.

How to convert the ID to a shortened URL

  1. Think of an alphabet we want to use. In your case that's [a-zA-Z0-9]. It contains 62 letters.
  2. Take an auto-generated, unique numerical key (the auto-incremented id of a MySQL table for example).
    For this example I will use 12510 (125 with a base of 10).
  3. Now you have to convert 12510 to X62 (base 62).
    12510 = 2×621 + 1×620 = [2,1]
    This requires use of integer division and modulo. A pseudo-code example:
    digits = []
    
    while num > 0
      remainder = modulo(num, 62)
      digits.push(remainder)
      num = divide(num, 62)
    
    digits = digits.reverse
    
    Now map the indices 2 and 1 to your alphabet. This is how your mapping (with an array for example) could look like:
    0  → a
    1  → b
    ...
    25 → z
    ...
    52 → 0
    61 → 9
    
    With 2 → c and 1 → b you will receive cb62 as the shortened URL.
    http://shor.ty/cb
    

How to resolve a shortened URL to the initial ID

The reverse is even easier. You just do a reverse lookup in your alphabet.
  1. e9a62 will be resolved to "4th, 61st, and 0th letter in alphabet".
    e9a62 = [4,61,0] = 4×622 + 61×621 + 0×620 = 1915810
  2. Now find your database-record with WHERE id = 19158 and do the redirect.

C# version:
public class UrlShortener 
{
    private static String ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static int    BASE     = 62;

    public static String encode(int num)
    {
        StringBuilder sb = new StringBuilder();

        while ( num > 0 )
        {
            sb.Append( ALPHABET[( num % BASE )] );
            num /= BASE;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = sb.Length - 1; i >= 0; i--)
        {
            builder.Append(sb[i]);
        }
        return builder.ToString(); 
    }

    public static int decode(String str)
    {
        int num = 0;

        for ( int i = 0, len = str.Length; i < len; i++ )
        {
            num = num * BASE + ALPHABET.IndexOf( str[(i)] ); 
        }

        return num;
    }   
}

2015, ഓഗസ്റ്റ് 18, ചൊവ്വാഴ്ച

Movie Data API - All information about movies

The OMDb API is a free web service to obtain movie information, all content and images on the site are contributed and maintained by our users.

http://www.omdbapi.com/
http://www.omdbapi.com/?t=Furious+seven&y=2015&plot=full&r=xml
http://www.omdbapi.com/?t=Furious+seven&y=2015&plot=short&r=json
https://www.omdbapi.com/?t=Furious+seven&y=2015&plot=full&r=json

http://imdbapi.poromenos.org/
http://imdbapi.poromenos.org/js/?name=how+i+met+your+mother

2009, നവംബർ 12, വ്യാഴാഴ്‌ച

Never expect things to happen
Struggle and make them happen
Never expect yourself to be given a good value

Create a value of your own

2009, ഒക്‌ടോബർ 28, ബുധനാഴ്‌ച

Bye bye Anup

Anup is leaving Orbit & Bahrain for a new job. We had a get together before that, though there were some more people, they left party early.
Posted by Picasa

2009, ഒക്‌ടോബർ 21, ബുധനാഴ്‌ച

ഇന്നത്തെ ചിന്താവിഷയം

Thought for the day! "Without your involvement you can't succeed.....

With your involvement you can't fail.... "

"If you miss an opportunity don't fill your eyes with tears...
It may hide yet another opportunity lying in front of you... "

മലയാലം!!

വിദേശ മലയാളികളും സ്വദേശ മലയാളികളും മലയാളം മറക്കുന്ന കാലമാണ്‌ ..

കേരളത്തിന് പുറത്തു ജനിച്ചു വളര്‍ന്ന രണ്ടു സുഹൃത്തുകള്‍ക്ക് വേണ്ടി അന്വേഷിച്ചു കണ്ടു പിടിച്ച അക്ഷരമാല ഇതാ മറ്റുള്ളവര്‍ക്ക് കൂടി ...

[I think at-least this blog need English translation :)
This is for my kerala friends who born & brought up abroad. You can learn Malayalam alphabets easily through this website]

http://www.omniglot.com/writing/malayalam.htm

പക്ഷി നിരീക്ഷണം

പുതിയ പണി തുടങ്ങി.. പക്ഷി നിരീക്ഷണം !
മരുഭുമിയിലും പക്ഷികളോ!! ആദ്യം സംശയിച്ചു എങ്കിലും വളരെ അധികം ഒണ്ടു എന്ന് മനസ്സില്‍ ആയ്‌ !! എങ്കില്‍ പണ്ടു പള്ളിക്കൂടത്തില്‍ പഠിച്ചത് എല്ലാം ആദ്യം തൊട്ടു ഒന്നു കൂടെ പഠിച്ചേക്കാം എന്ന് വച്ചു..

http://www.birding.in/bird_topography.htm#ത്യഗ്രം
http://www.bird-watching-guide.us/bird_watching_guide_chapter_1.ഹറം
http://birding.in/
http://www.birding.com/beginning_birding.asp