You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.7 KiB

import io
from datetime import datetime
from paramiko import SSHClient
from scp import SCPClient
from app import Paper, C, db
from time import sleep
html_text = '''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<script>
if(navigator.userAgent.search('MicroMessenger') == -1)
window.location.host = '{target_host}';
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body style="margin:0;background:#101010;color:white;font-size:28px;text-align:center">
<img src="https://bbs.pku.edu.cn/attach/d3/ee/d3eee0cd4e94ce11/pastExam_wechat.png" width="100%">
<hr style="margin:0 40px">
<p>截止{time}<br>这里有{nc}门课程的{np}份资料<br>欢迎继续添砖加瓦</p>
</body>
</html>
'''
def check_and_update(last_number):
np = Paper.query.count()
if np != last_number:
nc = db.session.query(Paper.course.distinct()).count()
t = datetime.now()
username, host, path, target_host = C.wechat_html_address
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(host, username=username)
with SCPClient(ssh.get_transport()) as scp:
fl = io.BytesIO()
fl.write(bytes(html_text.format(time=t.strftime("%Y-%m-%d %H:%M"), nc=nc, np=np, target_host=target_host), encoding='utf-8'))
fl.seek(0)
scp.putfo(fl, path)
return np
n = 0
while True:
try:
print(str(datetime.now()), 'start ------')
n = check_and_update(n)
print(n)
except:
print('scp failed')
sleep(300)