Commit 936785a1 authored by wangkaixiong's avatar wangkaixiong 🚴🏼
Browse files

init version

parent df892bc0
*.yml
task*
\ No newline at end of file
...@@ -20,22 +20,8 @@ bash notify_attention.sh ...@@ -20,22 +20,8 @@ bash notify_attention.sh
7. exe goodnight 7. exe goodnight
TODO:
1. pyinstaller 实现 exe;
2. server.py 同步增加 email 通知功能;
3. 实现服务端启动后,程序不终止,或者 notify 之后, 自动保留后台进展,直到完成任务;
```bash
#!/bin/bash
python download_model.py
touch finished
if [ -e "/path/to/your/file" ]; then
bash notify_attention.sh "finished download model."
else
bash notify_attention.sh "unfinished download-model task, please be aware."
fi
```
- notify_attention.sh 脚本实现自动加入screen后台; 任务完成后发邮件通知自己;
import yaml
import smtplib
from email.mime.text import MIMEText
import os
import sys
import time
import schedule
# 加载配置文件
def load_config():
with open('eml.yml', 'r') as file:
return yaml.safe_load(file)
# 发送邮件
def send_email(subject, body):
config = load_config()
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = config['email']['from']
msg['To'] = config['email']['to']
with smtplib.SMTP(config['email']['smtp_server'], config['email']['smtp_port']) as server:
server.starttls()
server.login(config['email']['username'], config['email']['password'])
server.sendmail(config['email']['from'], config['email']['to'], msg.as_string())
# 初始化任务文件
def init_task_file(task_path):
with open(task_path, 'w') as file:
file.write("")
send_email("Task Initialized", f"Task file created at {task_path}")
# 添加任务
def add_task(task_path, task):
with open(task_path, 'a') as file:
file.write(task + "\n")
tasks = get_tasks(task_path)
send_email("Task Added", f"Task added: {task}\n\nCurrent Tasks:\n{tasks}")
# 删除任务
def remove_task(task_path, task):
with open(task_path, 'r') as file:
tasks = file.readlines()
with open(task_path, 'w') as file:
for t in tasks:
if t.strip() != task:
file.write(t)
tasks = get_tasks(task_path)
send_email("Task Removed", f"Task removed: {task}\n\nCurrent Tasks:\n{tasks}")
# 获取当前任务列表
def get_tasks(task_path):
with open(task_path, 'r') as file:
return file.read()
# 检查路径并发送通知
def check_path_and_notify(path, time_limit):
if os.path.exists(path):
send_email("Path Exists", f"The path {path} exists.")
else:
time.sleep(time_limit)
if not os.path.exists(path):
send_email("Path Not Completed", f"The path {path} was not completed within the specified time.")
# 整理当日任务并发送邮件
def summarize_tasks(task_path):
tasks = get_tasks(task_path)
send_email("Daily Task Summary", f"Tasks for today:\n{tasks}")
# 主函数
def main():
if len(sys.argv) < 2:
print("Usage: exe <command> [args]")
return
command = sys.argv[1]
task_path = "tasks.txt" # 默认任务文件路径
if command == "init":
if len(sys.argv) < 3:
print("Usage: exe init <task.path>")
return
init_task_file(sys.argv[2])
elif command == "add":
if len(sys.argv) < 3:
print("Usage: exe add <task>")
return
add_task(task_path, sys.argv[2])
elif command == "remove":
if len(sys.argv) < 3:
print("Usage: exe remove <task>")
return
remove_task(task_path, sys.argv[2])
elif command == "cat":
print(get_tasks(task_path))
elif command == "notify":
if len(sys.argv) < 4:
print("Usage: exe notify <path> <time>")
return
check_path_and_notify(sys.argv[2], int(sys.argv[3]))
elif command == "goodnight":
summarize_tasks(task_path)
else:
print("Unknown command")
if __name__ == "__main__":
main()
\ No newline at end of file
schedule
flask
\ No newline at end of file
from flask import Flask, render_template, request, jsonify
import os
app = Flask(__name__)
# 任务文件路径
TASK_FILE = "tasks.txt"
# 初始化任务文件
def init_task_file():
if not os.path.exists(TASK_FILE):
with open(TASK_FILE, 'w') as file:
file.write("")
# 获取所有任务
def get_tasks():
with open(TASK_FILE, 'r') as file:
tasks = file.readlines()
return [task.strip() for task in tasks]
# 添加任务
def add_task(task):
with open(TASK_FILE, 'a') as file:
file.write(task + "\n")
# 删除任务
def remove_task(task):
tasks = get_tasks()
with open(TASK_FILE, 'w') as file:
for t in tasks:
if t != task:
file.write(t + "\n")
# 首页路由
@app.route('/')
def index():
tasks = get_tasks()
return render_template('index.html', tasks=tasks)
# 添加任务接口
@app.route('/add', methods=['POST'])
def add():
task = request.form.get('task')
if task:
add_task(task)
return jsonify({"status": "success", "tasks": get_tasks()})
# 删除任务接口
@app.route('/remove', methods=['POST'])
def remove():
task = request.form.get('task')
if task:
remove_task(task)
return jsonify({"status": "success", "tasks": get_tasks()})
# 启动服务
if __name__ == '__main__':
init_task_file()
app.run(debug=True)
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Task Manager</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #333;
}
ul {
list-style-type: none;
padding: 0;
}
li {
padding: 10px;
border: 1px solid #ddd;
margin-bottom: 5px;
display: flex;
justify-content: space-between;
align-items: center;
}
button {
background-color: #ff4d4d;
color: white;
border: none;
padding: 5px 10px;
cursor: pointer;
}
button:hover {
background-color: #cc0000;
}
input {
padding: 5px;
width: 200px;
}
</style>
</head>
<body>
<h1>Task Manager</h1>
<form id="addTaskForm">
<input type="text" id="taskInput" placeholder="Enter a new task" required>
<button type="submit">Add Task</button>
</form>
<ul id="taskList">
{% for task in tasks %}
<li>
<span>{{ task }}</span>
<button onclick="removeTask('{{ task }}')">Remove</button>
</li>
{% endfor %}
</ul>
<script>
// 添加任务
document.getElementById('addTaskForm').addEventListener('submit', function (e) {
e.preventDefault();
const task = document.getElementById('taskInput').value;
fetch('/add', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `task=${encodeURIComponent(task)}`,
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
location.reload(); // 刷新页面
}
});
});
// 删除任务
function removeTask(task) {
fetch('/remove', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `task=${encodeURIComponent(task)}`,
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
location.reload(); // 刷新页面
}
});
}
</script>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment