|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import sys |
| 7 | +import time |
| 8 | +from json.decoder import JSONDecodeError |
| 9 | +from shutil import which |
| 10 | + |
| 11 | +from selenium import webdriver |
| 12 | + |
| 13 | + |
| 14 | +def get_exec_path(): |
| 15 | + driver_name = 'chromedriver' |
| 16 | + path = which(driver_name) |
| 17 | + if path is None: |
| 18 | + path = which(driver_name, path='.') |
| 19 | + if path is None: |
| 20 | + print('No chrome driver...') |
| 21 | + sys.exit(1) |
| 22 | + return path |
| 23 | + |
| 24 | + |
| 25 | +def get_driver(): |
| 26 | + # Chrome options |
| 27 | + op = webdriver.ChromeOptions() |
| 28 | + # 关掉浏览器左上角的通知提示 |
| 29 | + op.add_argument("--disable-notifications") |
| 30 | + # 关闭'chrome正受到自动测试软件的控制'提示 |
| 31 | + op.add_argument("disable-infobars") |
| 32 | + op.add_argument("--start-maximized") |
| 33 | + # No gui |
| 34 | + op.add_argument("--headless") |
| 35 | + # Run under root user |
| 36 | + op.add_argument("--no-sandbox") |
| 37 | + op.add_argument("--disable-dev-shm-usage") |
| 38 | + op.add_argument("--disable-gpu") |
| 39 | + |
| 40 | + driver = webdriver.Chrome(executable_path=get_exec_path(), options=op) |
| 41 | + return driver |
| 42 | + |
| 43 | + |
| 44 | +def log(single, addition): |
| 45 | + info = "[{time_tag} 姓名:{yzxx} 学号: {loginName}] {add}" |
| 46 | + print(info.format(time_tag=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 47 | + yzxx=single.__getitem__('yzxx'), |
| 48 | + loginName=single.__getitem__('loginName'), add=addition) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def task(driver, single): |
| 53 | + driver.get("https://fxgl.jx.edu.cn/4136010406/public/homeQd?loginName=" |
| 54 | + + single.__getitem__('loginName') |
| 55 | + + "&loginType=" + str(single.__getitem__('loginType'))) |
| 56 | + time.sleep(1) |
| 57 | + log(single, '自动签到中...') |
| 58 | + js = 'async function(){let t=\'REPLACE\';return t=JSON.parse(t),await async function(t){return await new Promise(n=>{$.ajax({url:"https://fxgl.jx.edu.cn/4136010406/studentQd/saveStu",method:"post",data:t,success:function(t){return n(JSON.stringify(t))}})})}(t)}();' |
| 59 | + # js.replace("\"","\\\"") |
| 60 | + js = js.replace("REPLACE", json.dumps(single.__getitem__('checkIn'))) |
| 61 | + print(driver.execute_script('return ' + js)) |
| 62 | + time.sleep(3) |
| 63 | + log(single, '自动填写问卷中...') |
| 64 | + js = 'async function(){var t=\'REPLACE\',n="https://fxgl.jx.edu.cn/4136010406/";return 0==(t=JSON.parse(t)).sf?n+="dcwjEditNew/dcwjSubmit2":n+="dcwjEditNew/dcwjTsubmit2",await async function(t,n){return await new Promise(i=>{$.ajax({type:"post",url:t,data:{dcwj:JSON.stringify(n)},success:function(t){return i(JSON.stringify(t))}})})}(n,t)}();' |
| 65 | + js = js.replace("REPLACE", json.dumps(single.__getitem__('paper'))) |
| 66 | + print(driver.execute_script('return ' + js)) |
| 67 | + |
| 68 | + |
| 69 | +def main(): |
| 70 | + json_filename = "./config.json" |
| 71 | + try: |
| 72 | + with open(json_filename, 'r', encoding='utf-8') as f: |
| 73 | + data = json.load(f) |
| 74 | + driver = get_driver() |
| 75 | + for single in data: |
| 76 | + task(driver, single) |
| 77 | + time.sleep(1) |
| 78 | + driver.quit() |
| 79 | + except FileNotFoundError: |
| 80 | + print("File is not found: " + os.path.abspath(json_filename)) |
| 81 | + print("Creating file...") |
| 82 | + with open(json_filename, 'w', encoding='utf-8') as f: |
| 83 | + json.dump([{ |
| 84 | + "loginName": "student id", |
| 85 | + "yzxx": "name", |
| 86 | + "loginType": 0, |
| 87 | + "checkIn": {}, |
| 88 | + "paper": {} |
| 89 | + }], f) |
| 90 | + except PermissionError: |
| 91 | + print("No permission: " + os.path.abspath(json_filename)) |
| 92 | + except JSONDecodeError: |
| 93 | + print("Error file data...") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + main() |
0 commit comments