punch.py 1004 B

123456789101112131415161718192021222324252627282930313233
  1. import json
  2. import logging
  3. import sys
  4. import conf
  5. from conf import path, text
  6. from robot.chrome import Robot
  7. from lib.send import Sender
  8. if __name__ == '__main__':
  9. logging.basicConfig(level=logging.INFO, filename=path.log,
  10. format='%(asctime)s [%(levelname)s] %(message)s')
  11. sender = Sender(conf.smtp, conf.wechat)
  12. robot = Robot()
  13. with open(sys.argv[1]) as f:
  14. users = json.load(f)
  15. for user in users:
  16. try:
  17. result = robot.punch(**user['punch'])
  18. if '成功' in result:
  19. result = text.ok
  20. if '已提交过' in result:
  21. result = text.already
  22. info = user['punch']['username'] + result
  23. logging.info(info)
  24. except Exception as e:
  25. result = text.failed
  26. info = user['punch']['username'] + result
  27. logging.exception(info, exc_info=e)
  28. finally:
  29. sender.send(result, text.subject, **user['contact'])
  30. print(info)