This repository has been archived by the owner on Feb 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
grade.py
executable file
·114 lines (104 loc) · 3.52 KB
/
grade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from newknn import Captcha
import urllib, urllib2, cookielib, os, zlib, time, getpass, sys
from mail import send_email
from config import *
headers = {
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
}
cookie_support = urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
def login():
captcha = Captcha()
has_login = False
while not has_login:
print 'Trying to login...'
req = urllib2.Request(
url='http://mis.teach.ustc.edu.cn/',
headers=headers
)
urllib2.urlopen(req, timeout=req_timeout).read()
req = urllib2.Request(
url='http://mis.teach.ustc.edu.cn/randomImage.do',
headers=headers
)
content = urllib2.urlopen(req, timeout=req_timeout).read()
code = captcha.hack(content)
print 'Recognized captcha code:', code
postdata = urllib.urlencode({
'userbz': 's',
'hidjym': '',
'userCode': student_no,
'passWord': ustcmis_password,
'check': code
})
req = urllib2.Request(
url='http://mis.teach.ustc.edu.cn/login.do',
data=postdata,
headers=headers
)
result = urllib2.urlopen(req, timeout=req_timeout).read()
# print result
if "alert" in result:
print 'Login incorrect!'
else:
has_login = True
print 'Login OK!'
def get_grade():
req = urllib2.Request(
url='http://mis.teach.ustc.edu.cn/initfqcjxx.do?tjfs=1',
headers=headers
)
return urllib2.urlopen(req, timeout=req_timeout).read()
def parse_grade(grade):
soup = BeautifulSoup(grade, "html5lib")
# for i,line in enumerate(soup.find_all('tr')):
# for j,elem in enumerate(line.find_all('td')):
# print 'line=',i,' row=',j,' ',elem.get_text().encode('utf-8')
flag = 0
rows = soup.find_all('tr')
data = []
for row in rows:
elems = row.find_all('td')
if len(elems) == 7:
if flag:
data.append([td.get_text() for td in elems])
flag = 1
return data
olddata = []
first_run = True
while True:
print 'Query...'
try:
grade = get_grade()
if "userinit" in grade:
print 'Not login.'
login()
continue
data = parse_grade(grade)
print time.strftime('%Y-%m-%d %X', time.localtime(time.time())), 'Count :', len(data)
test_mail = False
if first_run:
ans = raw_input('Send test email? (y/n)')
if ans.lower() in ['', 'y', 'yes']:
test_mail = True
if len(data) != len(olddata) and not first_run or test_mail:
text = ' , '.join(row[2] + ' ' + row[3] for row in data if row not in olddata)
if test_mail:
text = 'Test email. ' + text
print 'Sending mail...'
print 'Text:', text.encode('utf8')
if enable_mail:
send_email(text, text.encode('utf-8'))
print 'Mail sent.'
olddata = data
first_run = False
except Exception as e:
if not isinstance(e, KeyboardInterrupt):
print time.strftime('%Y-%m-%d %X', time.localtime(time.time())), 'Error:', str(e)
else:
break
time.sleep(interval)