Created
November 16, 2018 06:36
-
-
Save lizy14/acbf4c064fe11601f902ae141bf31c8f to your computer and use it in GitHub Desktop.
银行代发与研究生资助奖励查询
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import json | |
import getpass | |
import smtplib | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
import requests | |
from bs4 import BeautifulSoup | |
def get_credentials(): | |
CREDENTIAL_FILENAME = "info-credentials.json" | |
if not os.path.exists(CREDENTIAL_FILENAME): | |
print("credentials will be saved in .\info-credentials.json") | |
credentials = { | |
'username': input("Username > "), | |
'password': getpass.getpass("Password > ") | |
} | |
json.dump(credentials, open(CREDENTIAL_FILENAME, 'w')) | |
else: | |
credentials = json.load(open(CREDENTIAL_FILENAME)) | |
return credentials | |
def login_info(credentials): | |
session = requests.Session() | |
r0 = session.post( | |
"https://info.tsinghua.edu.cn/Login", | |
data={ | |
'redirect': 'NO', | |
'userName': credentials['username'], | |
'password': credentials['password'], | |
'x': 0, | |
'y': 0 | |
}) | |
if 'loginError' in r0.text: | |
raise "info 登录失败" | |
return session | |
def get_zzjl_records(session): | |
session.get( | |
'http://info.tsinghua.edu.cn/minichan/roamaction.jsp?id=26') | |
r = session.post( | |
"http://zzjl.graduate.tsinghua.edu.cn/b/yjsjzxt/v_yjszzjl_yjscwdfmx_cx/pageList", | |
data={ | |
'ffkssj': '20101231', | |
'ffjssj': '20301231', | |
'rows':'1000', | |
'page': '1', | |
'sidx': 'id', | |
'sord': 'asc' | |
}) | |
return r.json()['object']['rows'] | |
def get_yhdf_records(session): | |
a = session.get( | |
'http://info.tsinghua.edu.cn/minichan/roamaction.jsp?id=52') | |
query = '&'.join(["year={}#{}".format(y, m) for m in range(1, 13) for y in range(2010, 2030)]) | |
r = session.post('http://166.111.14.8:8080/info/search.do', data=query, headers={"Content-Type": "application/x-www-form-urlencoded"}) | |
soup = BeautifulSoup(r.text, features="lxml") | |
records = [] | |
for tr in soup.find_all('tr'): | |
tds = tr.find_all('td') | |
if len(tds) == 12: | |
records += [[' '.join(reversed(td.get_text().split('\xa0'))).strip() for td in tds]] | |
return records | |
def lookup_in_yhdf(record_id, yhdf_records): | |
timestamps = re.findall(r"\d{8} \d\d:\d\d:\d\d", record_id) | |
if len(timestamps) != 1: | |
return '' | |
timestamp = timestamps[0] | |
result_text = '' | |
for record in yhdf_records: | |
if record[-6] == timestamp: | |
result_text += ' '.join(["\n项目:", record[2], record[4]]) | |
return result_text | |
def pretty_date(d): | |
return "{} 年 {} 月 {} 日".format(d[:4], int(d[4:6]), int(d[6:8])) | |
def send_email(subject, content): | |
credentials = get_credentials() | |
my_address = credentials['username']+'@mails.tsinghua.edu.cn' | |
server = smtplib.SMTP('mails.tsinghua.edu.cn') | |
server.login(my_address, credentials["password"]) | |
msg = MIMEMultipart("alternative") | |
msg["Subject"] = subject | |
msg.attach(MIMEText(content, "plain", "utf-8")) | |
server.sendmail(my_address, my_address, msg.as_string().encode('ascii')) | |
def main(): | |
NOTIFICATION_SENT_FILENAME = "notification-sent.json" | |
if not os.path.exists(NOTIFICATION_SENT_FILENAME): | |
json.dump([], open(NOTIFICATION_SENT_FILENAME, 'w')) | |
notification_sent_ids = json.load(open(NOTIFICATION_SENT_FILENAME)) | |
yhdf_records = get_yhdf_records(login_info(get_credentials())) | |
records = get_zzjl_records(login_info(get_credentials())) | |
result_text = '' | |
email_text = '' | |
for record in reversed(records): | |
result_text_for_record = '' | |
if record['ksje'] > 0: | |
result_text_for_record += ('\n数额: {} 元(税前 {} 元、扣税 {} 元)'.format(record['sfje'], record['jfje'], record['ksje'])) | |
else: | |
result_text_for_record += ('\n数额: {} 元'.format(record['sfje'])) | |
result_text_for_record += ' '.join(["\n日期:", pretty_date(record['ffrq'])]) | |
result_text_for_record += ' '.join(["\n类目:", record['sjly'], record['dfytmc']]) | |
result_text_for_record += ' '.join(["\n部门:", record['xmssbmmc'], record['xmssbm'], record['xmfzrxm'], record['ffrxm'], record['ffr']]) | |
result_text_for_record += lookup_in_yhdf(record['id'], yhdf_records) | |
result_text_for_record += '\n' | |
result_text += result_text_for_record | |
if record['id'] not in notification_sent_ids: | |
email_text += result_text_for_record | |
notification_sent_ids += [record['id']] | |
json.dump(notification_sent_ids, open(NOTIFICATION_SENT_FILENAME, 'w')) | |
print(result_text) | |
if email_text: | |
print('有新的记录') | |
send_email("银行代发与资助奖励查询", email_text) | |
print('没有新的记录') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output
新增的记录会发送到自己的清华邮箱