URL_shortener简单实现

Jun 3, 2016


URL Shortening是一项将长网址转换为短网址的技术,方便用户沟通交流,尤其是在限制字符长度的场景下。

比如 “http://en.m.wikipedia.org/wiki/URL_shortening” 可以缩短为”https://goo.gl/vvwncI”.

更多资料请参考WikiURL Shortener

思路:

  1. uuid1为每个网址生成一个唯一id
  2. 将id和网址用redis存储
  3. 用”abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789” 64个字符来编码网址id,再加上固定短网址前缀即可
  4. 解码时,需要用短网址来获取id,再从redis中取得长网址,返回即可

Python实现

#encoding:utf-8
import uuid
import redis

r = redis.Redis(host='localhost',port=6379,db=0)

ALPHABET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
BASE = len(ALPHABET)
#短地址前缀
STATIC_URL="http://yoururl/"

#生成唯一id
def generate_id():
    return uuid.uuid1().int
#编码
def encode(num):
    ret_str=''
    while num:
        index = num % BASE
        ret_str += ALPHABET[index]
        num /= BASE
    return ret_str[::-1]

#解码
def decode(str):
    num=0
    length = len(str)
    index=0
    while index < length:
        num = num*BASE + ALPHABET.index(str[index])
        index += 1
    return num

#获取短url
def url_short(url):
    url_id = generate_id()
    encode_url = encode(url_id)
    r.set(url_id,url)
    return STATIC_URL+encode_url

#返回长url
def url_long(short_url):
    short_url = short_url[-22:]
    url_id = decode(short_url)
    long_url = r.get(url_id)
    return long_url

#example
if __name__ == "__main__":
    url ="http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener"
    url_short_str = url_short(url)
    url_long_str = url_long(url_short_str)
    print url_short_str
    print url_long_str

Reference

  1. URL_shortening
  2. Building Your Own URL Shortener
  3. How to code a URL shortener?
  4. 站长之家—在线短链生成器

上一篇博客:支付宝即时到账接口
下一篇博客:信息检索之简单搜索引擎的实现