Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make unique short URL with Python?

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.

like image 424
Esteban Feldman Avatar asked Sep 30 '09 11:09

Esteban Feldman


People also ask

How does URL shortener work Python?

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.

How do you create a short URL algorithm?

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.

How do I create a custom short URL for free?

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.

How can I make a short URL with Python?

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.

How to generate a unique short URL with Python hashids?

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.

How to use Bitly API to shorten a URL?

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.

How does a URL shortener generate an int?

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.


2 Answers

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.

like image 67
FogleBird Avatar answered Sep 24 '22 14:09

FogleBird


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.

like image 27
Ned Batchelder Avatar answered Sep 22 '22 14:09

Ned Batchelder