Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

构建docker文件,增加钉钉机器人打卡完毕提醒功能 #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
*.pyc
*.pyo
*.pyd
10 changes: 10 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
97 changes: 68 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
@@ -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"]
Expand All @@ -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}个失败!')
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={}&timestamp={}&sign={}".format(access_token, timestamp, sign)

# 发送HTTP POST请求到钉钉webhook
info = requests.post(url=webhook, data=message_json, headers=header)

# 打印请求结果
print(info.text)
13 changes: 0 additions & 13 deletions runtest.py

This file was deleted.

4 changes: 2 additions & 2 deletions study.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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