Skip to content

Instantly share code, notes, and snippets.

@creamidea
Created May 2, 2014 14:58
Show Gist options
  • Save creamidea/d85ac982722ba4658c3b to your computer and use it in GitHub Desktop.
Save creamidea/d85ac982722ba4658c3b to your computer and use it in GitHub Desktop.
基于django的体育刷卡次数抓取展示程序主题
# -*- coding: utf-8 -*-
# Create your views here.
from __future__ import division #加载这个之后,除法就不是地板除了。
import re
import urllib
import urllib2
import cookielib
from datetime import datetime, date
from django.http import HttpResponse
from django.shortcuts import render
from django.template import RequestContext
# 设置刷卡起止时间
STARTDATE = (2014, 3, 3)
ENDDATE = (2014, 6, 6)
def loginpe(request):
# 登录学校的http://10.28.102.51/student/查询刷卡次数
if request.method == "POST":
# 处理POST数据
uid = request.POST["uid"]
password = request.POST["password"]
# 判断用户输入的信息是否为空,如果为空则立即返回
if uid == "" or password == "":
return render(request, 'es-error.html', {
"title": u"出错界面",
"error": "呀的,你要我查什么?告诉我这么「神秘」的信息!!",
})
html = [] # 用于装载两个页面的html
error = None #用于处理错误
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
# 先设置简单的Request Headers
headers = {
'Use-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6',
}
url = {
'checkUser': "http://10.28.102.51/student/checkUser.jsp",
'queryPhyInfo': "http://10.28.102.51/student/queryPhyInfo.jsp",
'queryHealthInfo': "http://10.28.102.51/student/queryHealthInfo.jsp",
'queryExerInfo': "http://10.28.102.51/student/queryExerInfo.jsp",
}
try:
postdata = urllib.urlencode(dict(userName=uid, passwd=password))
# 登录获取Cookie
reqCheckUser = urllib2.Request(url=url["checkUser"], headers=headers, data=postdata)
# 读取页面,为后面数据处理等做准备。
rltCheckUser = opener.open(reqCheckUser).read()
except Exception, e:
error = str(e)
else:
# 检查是否是出错页面
html.append(rltCheckUser.decode('gb18030').encode('utf-8'))
if re.search(r'<b>登录失败!&nbsp;请重新登录!</b>', html[0]):
error = u'''少年,你被 体育部的服务器酱 拒绝了哦,喵~。
可能是帐号或者密码输错了,返回继续吧。(/・ω・)/12
'''
# 登录失败,处理一下
if error:
return render(request, 'es-error.html', {
"title": u"出错界面",
"error": error,
})
# 首先获取cookie,然后去获取具体的其他信息
cookies = {}
for c in cookie:
cookies[c.name] = c.value
headers["Cookie"] = "%s=%s" % ("JSESSIONID", cookies["JSESSIONID"])
try:
reqQueryExerInfo = urllib2.Request(url=url["queryExerInfo"], headers=headers)
rltQueryExerInfo = opener.open(reqQueryExerInfo).read()
except Exception, e:
return render(request, 'es-error.html', {
"title": u"出错界面",
"error": str(e),
})
else:
html.append(rltQueryExerInfo.decode('gb18030').encode('utf-8'))
# 数据处理区域
source = "".join(html)
info = re.findall(r'<td bgcolor="#FFFFFF">&nbsp;(.*)</td>', source)
# info [学号 姓名 男 年级 行政班级 体育班级 早操 俱乐部考勤 。。。]
username = info[1]
gender = info[2]
grade = info[3]
morningNum = int(re.sub(r'(\d+).*', r'\1', info[6])) #早操
clubNum = int(re.sub(r'(\d+).*', r'\1', info[7])) #俱乐部
number = morningNum + clubNum
# 时间处理
today = datetime.now()
endday = today.replace(ENDDATE[0], ENDDATE[1], ENDDATE[2])
leaving = (endday - today).days
isover = False
if leaving <= 0: isover = True
# 计算已经刷了多少天卡
hasday = (today - today.replace(STARTDATE[0], STARTDATE[1], STARTDATE[2])).days
if hasday > 0:
# 保留两位小数
# from __future__ import division #加载这个之后,除法就不是地板除了。
avg = round((number / hasday), 2)
else:
avg = 0
RQNUM = 30
FULL = 70
alert = False
congratulation = False
if ((RQNUM - number) > leaving):
alert = True
if ((leaving + number) >= FULL):
congratulation = True
response = render(request, 'es-loginpe-response.html',
{
'title': u'查询结果',
'username': username,
'gender': gender,
'grade': grade,
'morningNum': morningNum,
'clubNum': clubNum,
'number': number,
'today': today.strftime("%Y年%m月%d日 %H时%M分%S喵~"),
'hasday': hasday,
'avg': avg,
'leaving': leaving,
'alert': alert,
'congratulation': congratulation,
'isover': isover,
})
# 登录成功,将其记录进cookie(这里的cookie是微信端的)
response.set_cookie('uid', uid)
response.set_cookie('password', password)
return response
else:
# 处理GET数据
uid = request.COOKIES.get('uid', "")
password = request.COOKIES.get('password', "")
return render(request,
'es-loginpe.html',
{
"title": u"查询你的早起次数",
"uid": uid,
"password": password,
},
context_instance=RequestContext(request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment