短链接-短网址

生成短链接服务

# coding: utf-8
import django
import os
import xxhash

from django.shortcuts import redirect
from DwzUrl.models import ShortURLModel

os.environ.setdefault('DJANGO_SETTING_MODULE', 'GitLab.settings')
django.setup()


class DealUrl(object):

    def get_hash(self, text):
        digest_tool = xxhash.xxh64()
        digest_tool.reset()
        digest_tool.update(text)
        result = digest_tool.intdigest()
        return result

    def encode(self, num):
        alphabet = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
        if num == 0:
            return alphabet[0]
        arr = []
        base = len(alphabet)
        while num:
            num, rem = divmod(num, base)
            arr.append(alphabet[rem])
        arr.reverse()
        return ''.join(arr)

    def convert_short_url(self, url):
        """生成短链接"""
        hash_code = self.get_hash(url)
        url_code = self.encode(hash_code)
        return url_code

    def deal_url(self, url):

        url_code = self.convert_short_url(url)
        try:
            shortmodel = ShortURLModel()
            get_url_code_count = ShortURLModel.objects.filter(short_url=url_code).count()
            if get_url_code_count == 0:
                shortmodel.long_url = url
                shortmodel.short_url = url_code
                shortmodel.save()
            else:
                print("数据已存在无需重复添加")
            short_url = "http://10.1.240.102:9003/dwz/" + url_code
            return short_url, url_code
        except Exception as e:
            print(e)

    def verify_url_code(self, url_code):
        res = ShortURLModel.objects.filter(short_url=url_code).first()
        try:
            if res:
                response = redirect(res.long_url)
            else:
                response = "数据不存在"
            return response
        except Exception as e:
            print(e)


def dwz(request, url_code):
    dealurl = DealUrl()
    response = dealurl.verify_url_code(url_code)
    return response




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值