From 6cd455d2e9d90afe3bace5c97ceefc290e4142f8 Mon Sep 17 00:00:00 2001 From: wenjiaran <101538084+WenjiaRan@users.noreply.github.com> Date: Sat, 1 Jul 2023 17:35:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9E=84=E5=BB=BAdocker=E6=96=87=E4=BB=B6,?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=92=89=E9=92=89=E6=9C=BA=E5=99=A8=E4=BA=BA?= =?UTF-8?q?=E6=89=93=E5=8D=A1=E5=AE=8C=E6=AF=95=E6=8F=90=E9=86=92=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 4 +++ Dockerfile | 10 ++++++ main.py | 97 ++++++++++++++++++++++++++++++++++++--------------- runtest.py | 13 ------- study.py | 4 +-- 5 files changed, 84 insertions(+), 44 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 runtest.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..09647b1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +__pycache__ +*.pyc +*.pyo +*.pyd diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..45feab6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.8 + +WORKDIR /app + +COPY requirements.txt requirements.txt +RUN pip3 install -r requirements.txt + +COPY . . + +CMD ["python3", "main.py"] \ No newline at end of file diff --git a/main.py b/main.py index ccf08a0..068eb08 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,21 @@ import os import time -import sys +import requests +import json +import hmac +import hashlib +import base64 +import urllib.parse +import time from study import study def getAccounts(): result = [] - usernameRaw = os.getenv("USERNAME", "") + print("https://m.bjyouth.net/site/login") + print("请在上面的青年大学习网站尝试登录, 以确保你输入的账号和密码是正确的") + if len(usernameRaw.split('\n')) == 1: # Single User passwd = os.environ["PASSWORD"] @@ -32,42 +40,73 @@ def getAccounts(): ua = os.getenv('UA', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.42') - - -# Windows Automated Task Management -task_name="Daxuexi_18S4F65D" -if os.name == 'nt': - if task_name in os.popen("SCHTASKS /query").read(): - print("脚本配置的计划任务已存在") - # + 检测路径 - else: - if 1: # change to 0 if you want to manage it yourself - input("没有脚本配置的计划任务,按任意键创建;或者退出并把main.py 43行的1改成0") - k=os.popen(f"{sys.executable} ./runtest.py").read() - if 'SHOULDBEFINE\n'!=k: - if 'FAILIMPORT\n'==k: - print("依赖问题") - else: - print('创建失败?Python位置不对?') - exit(1) - create=os.popen(f'''SchTasks /Create /SC DAILY /MO 2 /TN {task_name} /TR "'{sys.executable}' '{os.path.realpath(__file__)}'" /ST 09:00''') - print('创建成功') - -# accounts = getAccounts() -accounts=[('********', '*********')] +msg="青年大学习\n\n" +accounts = getAccounts() print(f'账号数量:{len(accounts)}') +msg+=f'账号数量:{len(accounts)}\n' successful = 0 count = 0 for username, password in accounts: - if username=='********': - continue count += 1 print(f'--User {count}--') - if study(username, password, ua): + msg+=f'--User {count}--\n' + state=study(username, password, ua) + if state!=0: + msg+=state successful += 1 failed = count - successful print('--Summary--') +msg+='--Summary--\n' print(f'成功:{successful},失败:{failed}') +msg+=f'成功:{successful},失败:{failed}\n' if failed != 0: - raise Exception(f'有{failed}个失败!') \ No newline at end of file + raise Exception(f'有{failed}个失败!') + + +# 钉钉机器人的access_token +access_token = "" + +# 钉钉机器人的Secret +secret = "" + +# 获取当前时间戳(毫秒级),转换为字符串 +timestamp = str(round(time.time() * 1000)) + +# 拼接需要加密的字符串 +secret_enc = secret.encode('utf-8') +string_to_sign = '{}\n{}'.format(timestamp, secret) +string_to_sign_enc = string_to_sign.encode('utf-8') + +# 使用HmacSHA256算法计算签名,并进行Base64编码 +hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() +sign = urllib.parse.quote_plus(base64.b64encode(hmac_code)) + +# 构建请求头部 +header = { + "Content-Type": "application/json", + "Charset": "UTF-8" +} + +# 构建请求数据,此处为发送文本信息 +message ={ + "msgtype": "text", + "text": { + "content": msg + }, + "at": { + "isAtAll": True + } +} + +# 对请求数据进行json封装 +message_json = json.dumps(message) + +# 构建请求的URL,包含签名和时间戳 +webhook = "https://oapi.dingtalk.com/robot/send?access_token={}×tamp={}&sign={}".format(access_token, timestamp, sign) + +# 发送HTTP POST请求到钉钉webhook +info = requests.post(url=webhook, data=message_json, headers=header) + +# 打印请求结果 +print(info.text) diff --git a/runtest.py b/runtest.py deleted file mode 100644 index 7c21080..0000000 --- a/runtest.py +++ /dev/null @@ -1,13 +0,0 @@ -# Created by STZ at 11/12/2022 -try: - from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5 - from Crypto.PublicKey import RSA - from PIL import Image - import requests - from ddddocr import DdddOcr - import onnxruntime - import numpy as np - print('SHOULDBEFINE') -except: - print('FAILIMPORT') - exit() diff --git a/study.py b/study.py index 2b35201..23070be 100644 --- a/study.py +++ b/study.py @@ -79,7 +79,7 @@ def study(username, password, ua): if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])): print(f'{title} 在运行前已完成,退出') - return 1 + return f'{title} 在运行前已完成,退出\n' # pattern = re.compile(r'https://h5.cyol.com/special/daxuexi/(\w+)/m.html\?t=1&z=201') # result = pattern.search(url) @@ -102,7 +102,7 @@ def study(username, password, ua): if f"学习课程:《{title}》" in list(map(lambda x: x['text'], haveLearned['data'])): print(f'{title} 成功完成学习') - return 1 + return f'{title} 成功完成学习\n' else: print(f'完成{title}, 但未在检查中确认') return 0