1

Possible Duplicate:
how to shorten the url in a mathematical approach

Its a college django project.. where I can't really setup a database model to store dynamically generated URLs and redirect to short urls based on hashed primary key.. etc

I am thinking in lines of encrypting URL using some algorithm and decrypt on the server. Initially, I thought of using md5 hash but I guess we can't decrypt.

My actual URL would look like:

http://mydomain.com/photo?friend_name=dennis&me_name=ritchie&ping_url=http%3A%2F%2Fmydomain.com%2Fimg%3Furl%3Dhttps%3A%fsjcsd.googleueusercontent.com%2F-Sf6bP2nOKa8%2FUMaw8vv0QLI%2FAaaddjhnkchkjda7E%2Fs800%2FHosbsco.png%26message%3Dhi+dude

I am thinking it to convert into:

http://mydomain.com?encode=hksfhsfksuhkvbkjnukvns

Then on server side I thinking to convert hksfhsfksuhkvbkjnukvns back to actual string.

Is there any Python Lib I have

Community
  • 1
  • 1
Dennis Ritchie
  • 630
  • 1
  • 9
  • 20
  • 1
    See this similar question: http://stackoverflow.com/questions/875771/how-does-one-encode-and-decode-a-string-with-python-for-use-in-a-url – and3p Dec 12 '12 at 07:12
  • Probably not practical - your best bet is probably something like base64 encoding, but that gives me a 291 byte string for your sample input URL, which is stretching the requirement of "short" a bit. – Dominic Rodger Dec 12 '12 at 07:13
  • @DominicRodger okay.. if its simple to implement and works will. not an issue – Dennis Ritchie Dec 12 '12 at 07:28
  • 1
    base64 makes things longer, not shorter. – Barmar Dec 12 '12 at 07:37
  • base64 produces 256 characters (even if you compress input) compared to 219 in the original string. – jfs Dec 12 '12 at 07:38
  • I think there is 3rd party web applications if you need to have a temporary short(hashed) url to pass around like this http://app.x.co/ (It's google result) – tdihp Dec 12 '12 at 09:25

1 Answers1

2

You can go with url compression + encrypting/base64 approach.

For compressing you can check python's zlib or any other lossless compression libraries (lzma, etc), then convert compressed binary string to ascii by base64. For your information, zlib + base64 for your url is worse than the original url.

But the url may never be as short as you expect, simply because the url content have a lot of information, bad compression ratio, etc. So it's possible that this solution will never meet the need for applications with text length limit (like twitter).

So If you wanna a REALLY short url, there must be somewhere to store the short-long pair.

Keeping those in mind, and I googled for "url shortener" and a lot popped out.

These are third party web applications just to do this job:

Hope you can use a third party app, though.

tdihp
  • 2,329
  • 2
  • 23
  • 40