cherry_be/lib/allFunction.py
2021-09-04 10:50:46 +08:00

89 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf-8
import sys
from requests.sessions import session
sys.path.append('./lib/public')
from crawler import Crawler
import json
from hashlib import md5
from urllib.parse import urlencode, unquote_plus
from db import insertRank, findRank
# 主函数
crawlerCache = {}
# 处理登录操作 data:{cid,pwd,sign}
# 这里三个接口公用一个session所以合并成一个接口一个session走到底一次性返回所有数据
def manageLogin(request):
# json化应该能当dict用
data = json.loads(request.form['data'])
# MD5校验
checked = checkData(data)
if checked:
c = Crawler()
c.defaultInit(data)
res = c.connection()
crawlerCache[data['cid']] = c
return res
else:
return '数据校验失败', 400
def manageSubmitVerificationCode(request):
data = json.loads(request.form['data'])
checked = checkData(data)
if checked:
c = crawlerCache[data['cid']]
res = c.submitVerificationCode(data)
if res[-1] == 200:
grade = c.getGrade()
ownSchedule = c.getOwnSchedule()
crawlerCache[data['cid']] = None
return json.dumps({'grade': grade, 'ownSchedule': ownSchedule}), 200
else:
return res
else:
return '数据校验失败', 400
# 处理更新课表游戏排名信息
def manageScheduleUpload(request):
# json化应该能当dict用
data_cache = json.loads(request.form['data'])
# MD5校验
checked = checkData(data_cache)
data_cache.pop('sign')
if checked:
add_res = insertRank( data_cache['cid'], data_cache['nick'], data_cache['count'], data_cache['time'])
return add_res
else:
return '数据校验失败', 400
# 处理获取课表游戏排名信息
def manageScheduleGet(request):
# json化应该能当dict用
data_cache = json.loads(request.form['data'])
# MD5校验
checked = checkData(data_cache)
data_cache.pop('sign')
if checked:
# 获取排名表
get_res = findRank()
return get_res
else:
return '数据校验失败', 400
# 工具函数
# MD5 校验
def checkData(data):
d = data.copy()
try:
d.pop('sign')
except KeyError:
pass
d = str(d)
d = d.replace(' ', '')
md = md5()
md.update(d.encode('utf-8'))
r = md.hexdigest().upper()
return r == data['sign']