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

3 years ago
  1. import io
  2. from datetime import datetime
  3. from paramiko import SSHClient
  4. from scp import SCPClient
  5. from app import Paper, C, db
  6. from time import sleep
  7. html_text = '''
  8. <!DOCTYPE html>
  9. <html lang="zh-CN">
  10. <head>
  11. <script>
  12. if(navigator.userAgent.search('MicroMessenger') == -1)
  13. window.location.host = '{target_host}';
  14. </script>
  15. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  16. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  17. <meta property="og:title" content="华清大学课程攻略共享计划" />
  18. <meta property="og:description" content="这里有一套给你的往年考题" />
  19. <meta property="og:image" content="https://closed.social/pastExam/img/logo-256x256.png" />
  20. </head>
  21. <body style="margin:0;background:#101010;color:white;font-size:28px;text-align:center">
  22. <img src="https://bbs.pku.edu.cn/attach/d3/ee/d3eee0cd4e94ce11/pastExam_wechat.png" width="100%">
  23. <hr style="margin:0 40px">
  24. <p>{time}<br>{nc}{np}<br></p>
  25. </body>
  26. </html>
  27. '''
  28. local_html_text = '''
  29. <!DOCTYPE html>
  30. <html lang="zh-CN">
  31. <head>
  32. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  33. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  34. </head>
  35. <body style="font-size:22px;text-align:center">
  36. <p>{nc}{np}<br></p>
  37. </body>
  38. </html>
  39. '''
  40. def check_and_update(last_number):
  41. np = Paper.query.count()
  42. if np != last_number:
  43. nc = db.session.query(Paper.course.distinct()).count()
  44. t = datetime.now()
  45. username, host, path, target_host = C.wechat_html_address
  46. ssh = SSHClient()
  47. ssh.load_system_host_keys()
  48. ssh.connect(host, username=username)
  49. with open('static/login/count-info.html', 'w') as f:
  50. f.write(local_html_text.format(nc=nc, np=np, target_host=target_host))
  51. with SCPClient(ssh.get_transport()) as scp:
  52. fl = io.BytesIO()
  53. 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'))
  54. fl.seek(0)
  55. scp.putfo(fl, path)
  56. return np
  57. n = check_and_update(0)
  58. while True:
  59. try:
  60. print(str(datetime.now()), 'start ------')
  61. n = check_and_update(n)
  62. print(n)
  63. except:
  64. print('scp failed')
  65. sleep(300)