This commit is contained in:
RainSun 2020-08-04 22:47:34 +08:00
commit 2723c0de11
9 changed files with 389 additions and 0 deletions

0
lib/__init__.py Normal file
View File

42
lib/db.py Normal file
View File

@ -0,0 +1,42 @@
from pymongo import MongoClient
# 获取数据集
def col():
# 链接数据库
conn = MongoClient('mongodb://surl:jV9cL0eW6mX4@mongo:27017/surl')
return conn.surl['surl']
# 通过尾缀获取源地址
def getUrlByTail(tail):
try:
url = col().find_one({'tail': tail}, {"_id": 0, "url": 1})
if url:
return (url, 200)
else:
return ('URL不存在', 100)
except Exception as e:
print(e)
return ('数据库查询失败', 101)
# 通过源地址获取尾缀
def getTailByUrl(url):
try:
tail = col().find_one({'url': url}, {"_id": 0, "tail": 1})
if tail:
return (tail, 200)
else:
return ('尾缀不存在', 102)
except Exception as e:
print(e)
return ('数据库查询失败', 103)
# 新增记录
def insertTail(data):
try:
col().insert_one(data)
return ('OK', 200)
except Exception as e:
print(e)
return ('数据库插入失败', 104)
# time 时间戳 url 源地址 tail 生成的尾缀

43
lib/functions.py Normal file
View File

@ -0,0 +1,43 @@
from .db import getUrlByTail, getTailByUrl, insertTail
from .systemSwitch import getTail, getTime
# 处理新增 参数url
def manageInsert(request):
# 校验参数
try:
data = request.json
if not data.__contains__('url'):
raise Exception
except Exception as e:
print(e)
return '数据不合法', 400
url = data.url
tail_res = getTailByUrl(url)
# 如果存在对应尾缀或者数据库查询失败就直接返回
if tail_res[-1] != 102:
return tail_res
# 生成新的尾缀
tail = getTail()
# 查询是否存在重复的尾缀,直到生成一个不重复的尾缀
while getUrlByTail(tail)[-1] != 100:
tail = getTail()
# 组织数据
data = {
'url': url,
'time': getTime(),
'tail': tail
}
# 插入数据
insert_res = insertTail(data)
if(insert_res[-1] == 200):
return {'tail': tail}, 200
return insert_res
# 访问链接进行跳转
def manageRequest(tail):
if(str(tail).length != 5):
return ('参数错误', 400)
url_res = getUrlByTail(tail)
if(url_res[-1] != 200):
return ('无该链接', 404)
return url_res[0], 200

31
lib/systemSwitch.py Normal file
View File

@ -0,0 +1,31 @@
import math
import random
import datetime
char_arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
# 获取36进制字符串
def systemSwitch(num):
num = int(num)
char = ''
while num > 0:
key = (num - 1) % 36
char += char_arr[key]
num = math.floor((num - key) / 36)
return char
# 生成唯一不重名字符串
def randomId():
nowTime = datetime.datetime.now().strftime("%H%M%S") # 生成当前时间
randomNum = random.randint(0, 99) # 生成的随机整数n其中0<=n<=99
if randomNum < 10:
randomNum = str(0) + str(randomNum)
uniqueNum = str(nowTime) + str(randomNum)
return uniqueNum
# 获取随机字符串
def getTail():
return systemSwitch(randomId())
# 获取当前时间
def getTime():
return datetime.datetime.now().strftime("%Y%m%d%H%M%S")

37
shortUrl.py Normal file
View File

@ -0,0 +1,37 @@
from flask import Flask, escape, url_for, request, render_template, redirect, abort, send_from_directory
from lib.functions import manageInsert, manageRequest
app = Flask(__name__)
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True, port="80")
@app.route('/insert',methods=['POST'])
def insert():
return manageInsert(request)
@app.route('/<tail>')
def tail(tail):
res = manageRequest(tail)
if(res[-1] != 200):
abort(res[-1])
else:
redirect(res[0])
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(400)
def page_not_found(e):
return render_template('400.html'), 400
@app.errorhandler(500)
def page_not_found(e):
return render_template('500.html'), 500
@app.errorhandler(410)
def page_not_found(e):
return render_template('410.html'), 500

59
templates/400.html Normal file
View File

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>参数错误</title>
<style type="text/css">
body{height:100%;width:100%;margin:0; padding:0; font-size:14px; font-family:"微软雅黑",Arial, Helvetica, sans-serif;color: #333;}
img{ border:0;outline: none;}
body {
background: #33065b;
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}
.bg {
display: block;
height: auto;
width: 100%;
position: fixed;
bottom:0;
left: 0;
z-index: -1;
}
.title {
width: 80%;
margin: 0 auto;
font-size: 1rem;
color: #fff;
margin-top: 4rem;
}
.content {
width: 80%;
margin: 0 auto;
font-size: .625rem;
color:#fff;
margin-top:0.3rem;
}
</style>
<script type="text/javascript">
//px2rem
window.onresize = setHtmlFontSize
function setHtmlFontSize () {
const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
const htmlDom = document.getElementsByTagName('html')[0]
if(htmlWidth >= 500) htmlDom.style.fontSize = 500 / 10 + 'px'
else htmlDom.style.fontSize = htmlWidth / 10 + 'px'
}
setHtmlFontSize();
</script>
</head>
<body>
<img class="bg" src="https://gss0.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/b7003af33a87e950544a28181f385343fbf2b461.jpg" alt="">
<div class="title">主人~</div>
<div class="content">参数有点奇怪呢</div>
</body>
</html>

59
templates/404.html Normal file
View File

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>页面走丢了</title>
<style type="text/css">
body{height:100%;width:100%;margin:0; padding:0; font-size:14px; font-family:"微软雅黑",Arial, Helvetica, sans-serif;color: #333;}
img{ border:0;outline: none;}
body {
background: #33065b;
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}
.bg {
display: block;
height: auto;
width: 100%;
position: fixed;
bottom:0;
left: 0;
z-index: -1;
}
.title {
width: 80%;
margin: 0 auto;
font-size: 1rem;
color: #fff;
margin-top: 4rem;
}
.content {
width: 80%;
margin: 0 auto;
font-size: .625rem;
color:#fff;
margin-top:0.3rem;
}
</style>
<script type="text/javascript">
//px2rem
window.onresize = setHtmlFontSize
function setHtmlFontSize () {
const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
const htmlDom = document.getElementsByTagName('html')[0]
if(htmlWidth >= 500) htmlDom.style.fontSize = 500 / 10 + 'px'
else htmlDom.style.fontSize = htmlWidth / 10 + 'px'
}
setHtmlFontSize();
</script>
</head>
<body>
<img class="bg" src="https://gss0.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/b7003af33a87e950544a28181f385343fbf2b461.jpg" alt="">
<div class="title">抱歉,</div>
<div class="content">您访问的页面不存在</div>
</body>
</html>

59
templates/410.html Normal file
View File

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>二维码已失效</title>
<style type="text/css">
body{height:100%;width:100%;margin:0; padding:0; font-size:14px; font-family:"微软雅黑",Arial, Helvetica, sans-serif;color: #333;}
img{ border:0;outline: none;}
body {
background: #33065b;
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}
.bg {
display: block;
height: auto;
width: 100%;
position: fixed;
bottom:0;
left: 0;
z-index: -1;
}
.title {
width: 80%;
margin: 0 auto;
font-size: 1rem;
color: #fff;
margin-top: 4rem;
}
.content {
width: 80%;
margin: 0 auto;
font-size: .625rem;
color:#fff;
margin-top:0.3rem;
}
</style>
<script type="text/javascript">
//px2rem
window.onresize = setHtmlFontSize
function setHtmlFontSize () {
const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
const htmlDom = document.getElementsByTagName('html')[0]
if(htmlWidth >= 500) htmlDom.style.fontSize = 500 / 10 + 'px'
else htmlDom.style.fontSize = htmlWidth / 10 + 'px'
}
setHtmlFontSize();
</script>
</head>
<body>
<img class="bg" src="https://gss0.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/b7003af33a87e950544a28181f385343fbf2b461.jpg" alt="">
<div class="title">抱歉,</div>
<div class="content">该二维码已失效</div>
</body>
</html>

59
templates/500.html Normal file
View File

@ -0,0 +1,59 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
<title>服务器开小差了</title>
<style type="text/css">
body{height:100%;width:100%;margin:0; padding:0; font-size:14px; font-family:"微软雅黑",Arial, Helvetica, sans-serif;color: #333;}
img{ border:0;outline: none;}
body {
background: #33065b;
-moz-user-select: none;
-khtml-user-select: none;
user-select: none;
}
.bg {
display: block;
height: auto;
width: 100%;
position: fixed;
bottom:0;
left: 0;
z-index: -1;
}
.title {
width: 80%;
margin: 0 auto;
font-size: 1rem;
color: #fff;
margin-top: 4rem;
}
.content {
width: 80%;
margin: 0 auto;
font-size: .625rem;
color:#fff;
margin-top:0.3rem;
}
</style>
<script type="text/javascript">
//px2rem
window.onresize = setHtmlFontSize
function setHtmlFontSize () {
const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
const htmlDom = document.getElementsByTagName('html')[0]
if(htmlWidth >= 500) htmlDom.style.fontSize = 500 / 10 + 'px'
else htmlDom.style.fontSize = htmlWidth / 10 + 'px'
}
setHtmlFontSize();
</script>
</head>
<body>
<img class="bg" src="https://gss0.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/b7003af33a87e950544a28181f385343fbf2b461.jpg" alt="">
<div class="title">哎呀~</div>
<div class="content">服务器开小差了呢</div>
</body>
</html>