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.
 
 

74 lines
2.5 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">
<meta property="og:title" content="华清大学课程攻略共享计划" />
<meta property="og:description" content="这里有一套给你的往年考题" />
<meta property="og:image" content="https://closed.social/pastExam/img/logo-256x256.png" />
</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>
'''
local_html_text = '''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<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="font-size:22px;text-align:center">
<p>目前这里有{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 open('static/login/count-info.html', 'w') as f:
f.write(local_html_text.format(nc=nc, np=np, target_host=target_host))
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 = check_and_update(0)
while True:
try:
print(str(datetime.now()), 'start ------')
n = check_and_update(n)
print(n)
except:
print('scp failed')
sleep(300)