How can I make unique URL in Python a la http://imgur.com/gM19g or http://tumblr.com/xzh3bi25y When using uuid from python I get a very large one. I want something shorter for URLs.
URL Shortener, as the name suggests, is a service to help to reduce the length of the URL so that it can be shared easily on platforms like Twitter, where number of characters is an issue.
To generate a unique short URL, we can compute it using the Unique Hash(MD5, SHA256, etc.) of the original URL and then encode using base62. If we use the MD5 algorithm as our hash function, it'll produce a 128-bit hash value. After base62 encoding, we'll get a string having more than seven characters.
TinyURL is a Free link shortener service to make posting long URLs easier, and may only be used for actual URLs. This URL changer tool offers a preview feature and also offers a toolbar button to shorten any URLs quickly.
If you need something shorter, you should be able to truncate it to the desired length and still get something that will reasonably probably avoid clashes. Show activity on this post. Hashids is an awesome tool for this. Here's how to use Hashids to generate a unique short URL with Python: Show activity on this post.
Here's how to use Hashids to generate a unique short URL with Python: It doesn't really matter that this is Python, but you just need a hash function that maps to the length you want. For example, maybe use MD5 and then take just the first n characters.
Bitly URL Shortener is very simple to use. You just need to make an account on Bitly. Then, go to Group Settings and click on Advanced settings. There you will find the API option. Since API is now depreciated, click on OAuth option. Then, generate the OAuth Token. Copy the token. Now, install bitly_api.
The basic principle behind any URL shortener is to get an int from long URL then use base62 (base32, etc) encoding to convert this int to a more readable short URL. How is this int generated? Most of the URL shortener uses some auto-incrementing datastore to add URL to datastore and use the autoincrement id to get base62 encoding of int.
Edit: Here, I wrote a module for you. Use it. http://code.activestate.com/recipes/576918/
Counting up from 1 will guarantee short, unique URLS. /1, /2, /3 ... etc.
Adding uppercase and lowercase letters to your alphabet will give URLs like those in your question. And you're just counting in base-62 instead of base-10.
Now the only problem is that the URLs come consecutively. To fix that, read my answer to this question here:
Map incrementing integer range to six-digit base 26 max, but unpredictably
Basically the approach is to simply swap bits around in the incrementing value to give the appearance of randomness while maintaining determinism and guaranteeing that you don't have any collisions.
I'm not sure most URL shorteners use a random string. My impression is they write the URL to a database, then use the integer ID of the new record as the short URL, encoded base 36 or 62 (letters+digits).
Python code to convert an int to a string in arbitrary bases is here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With