Skip to content

Instantly share code, notes, and snippets.

@weishen
Forked from reorx/short_url.txt
Created June 12, 2019 09:57
Show Gist options
  • Save weishen/3ba9ba9fa546891524558ec2f2d8d785 to your computer and use it in GitHub Desktop.
Save weishen/3ba9ba9fa546891524558ec2f2d8d785 to your computer and use it in GitHub Desktop.
About short url generation
'''
Short URL Generator
===================
Python implementation for generating Tiny URL- and bit.ly-like URLs.
A bit-shuffling approach is used to avoid generating consecutive, predictable
URLs. However, the algorithm is deterministic and will guarantee that no
collisions will occur.
The URL alphabet is fully customizable and may contain any number of
characters. By default, digits and lower-case letters are used, with
some removed to avoid confusion between characters like o, O and 0. The
default alphabet is shuffled and has a prime number of characters to further
improve the results of the algorithm.
The block size specifies how many bits will be shuffled. The lower BLOCK_SIZE
bits are reversed. Any bits higher than BLOCK_SIZE will remain as is.
BLOCK_SIZE of 0 will leave all bits unaffected and the algorithm will simply
be converting your integer to a different base.
The intended use is that incrementing, consecutive integers will be used as
keys to generate the short URLs. For example, when creating a new URL, the
unique integer ID assigned by a database could be used to generate the URL
by using this module. Or a simple counter may be used. As long as the same
integer is not used twice, the same short URL will not be generated twice.
The module supports both encoding and decoding of URLs. The min_length
parameter allows you to pad the URL if you want it to be a specific length.
Sample Usage:
>>> import short_url
>>> url = short_url.encode_url(12)
>>> print url
LhKA
>>> key = short_url.decode_url(url)
>>> print key
12
Use the functions in the top-level of the module to use the default encoder.
Otherwise, you may create your own UrlEncoder object and use its encode_url
and decode_url methods.
Author: Michael Fogleman
License: MIT
Link: http://code.activestate.com/recipes/576918/
'''
DEFAULT_ALPHABET = 'mn6j2c4rv8bpygw95z7hsdaetxuk3fq'
DEFAULT_BLOCK_SIZE = 24
MIN_LENGTH = 5
class UrlEncoder(object):
def __init__(self, alphabet=DEFAULT_ALPHABET, block_size=DEFAULT_BLOCK_SIZE):
self.alphabet = alphabet
self.block_size = block_size
self.mask = (1 << block_size) - 1
self.mapping = range(block_size)
self.mapping.reverse()
def encode_url(self, n, min_length=MIN_LENGTH):
return self.enbase(self.encode(n), min_length)
def decode_url(self, n):
return self.decode(self.debase(n))
def encode(self, n):
return (n & ~self.mask) | self._encode(n & self.mask)
def _encode(self, n):
result = 0
for i, b in enumerate(self.mapping):
if n & (1 << i):
result |= (1 << b)
return result
def decode(self, n):
return (n & ~self.mask) | self._decode(n & self.mask)
def _decode(self, n):
result = 0
for i, b in enumerate(self.mapping):
if n & (1 << b):
result |= (1 << i)
return result
def enbase(self, x, min_length=MIN_LENGTH):
result = self._enbase(x)
padding = self.alphabet[0] * (min_length - len(result))
return '%s%s' % (padding, result)
def _enbase(self, x):
n = len(self.alphabet)
if x < n:
return self.alphabet[x]
return self._enbase(x / n) + self.alphabet[x % n]
def debase(self, x):
n = len(self.alphabet)
result = 0
for i, c in enumerate(reversed(x)):
result += self.alphabet.index(c) * (n ** i)
return result
DEFAULT_ENCODER = UrlEncoder()
def encode(n):
return DEFAULT_ENCODER.encode(n)
def decode(n):
return DEFAULT_ENCODER.decode(n)
def enbase(n, min_length=MIN_LENGTH):
return DEFAULT_ENCODER.enbase(n, min_length)
def debase(n):
return DEFAULT_ENCODER.debase(n)
def encode_url(n, min_length=MIN_LENGTH):
return DEFAULT_ENCODER.encode_url(n, min_length)
def decode_url(n):
return DEFAULT_ENCODER.decode_url(n)
if __name__ == '__main__':
for a in range(0, 200000, 37):
b = encode(a)
c = enbase(b)
d = debase(c)
e = decode(d)
assert a == e
assert b == d
c = (' ' * (7 - len(c))) + c
print '%6d %12d %s %12d %6d' % (a, b, c, d, e)
####
http://zihua.li/2012/05/implement-shortlink-using-base-62/
每个短链系统都需要一个能生成不重复的短链的算法。最简单的方法就是以数字为短链,如:
http://domain/1
http://domain/2
但是一百万个短链后短链的位数就猛增到了7位,像twitter等网站的短链服务显然没有办法接受这样的位数增长速度。为了充分减少短链位数,我们希望短链拥有更大的字符空间。
一个不错的方法是将数字转换成更高进制的数,比如62进制(即包含字符’0′..’1′, ‘a’..’z', ‘A’..’Z')。
JavaScript代码如下:
generateShortLink = function(space, value) {
if (typeof value === 'number') {
var result = '';
do {
result = space[value % space.length] + result;
value = Math.floor(value / space.length);
} while (value > 0)
} else if (typeof value === 'string') {
var result = 0;
for (var i = 0; i < value.length; ++i) {
result += space.indexOf(value[i]) *
Math.pow(space.length, value.length - i - 1);
}
}
return result;
};
如果传入的value是数字,就会将value转成字符空间为space,进制为space.length的数字(字符串);如果传入的value是字符串,则同理会将value转换成10进制数字。如:
$ generateShortLink('0123456789abcdef', 255)
'ff'
$ generateShortLink('01', 255)
'11111111'
$ generateShortLink('0123456789abcdefghijklmnopqrstuvwxyz', 123456789)
'21i3v9'
生成短链时先通过自增方式获得一个十进制数字代表短链id,然后将该id通过generateShortLink()转换成62进制(或者去除1和l,o和0等易混淆字符,根据使用场景是用户手动输入还是直接点击链接来做更多优化)短链显示给用户。用户访问短链时再把短链转换成10进制id,然后查询数据库将实际地址返还给用户。
####
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment