分享网络学习历程及生活

zstack任务取消和企业微信群机器人推送

太久没更新了,发一下随手写的两个小玩意

1.zstack任务取消工具

import sys
import requests
import json
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton, QTextEdit, QVBoxLayout

class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Zstack结束任务小工具')
        self.setGeometry(300, 300, 400, 400)
        layout = QVBoxLayout()

        ip_label = QLabel('IP地址:                                无需填写端口,默认5000')
        self.ip_input = QLineEdit()

        uuid_label = QLabel('任务UUID:')
        self.uuid_input = QLineEdit()

        self.result_text = QTextEdit()
        self.confirm_button = QPushButton('确定')
        self.confirm_button.clicked.connect(self.on_confirm_button_clicked)

        layout.addWidget(ip_label)
        layout.addWidget(self.ip_input)
        layout.addWidget(uuid_label)
        layout.addWidget(self.uuid_input)
        layout.addWidget(self.result_text)
        layout.addWidget(self.confirm_button)

        self.setLayout(layout)

    def request_api(self, ip, uuid):
        url = f'http://{ip}:5000/api/operation/action/cancel?actionId={uuid}'
        res = requests.get(url)
        data = json.loads(res.text)
        return data

    def on_confirm_button_clicked(self):
        ip = self.ip_input.text()
        uuid = self.uuid_input.text()

        if ip == '' or uuid == '':
            err = 'IP地址或UUID不能为空'
            return self.result_text.setText(err)
        else:
            try:
                data = self.request_api(ip, uuid)
                if data['success'] == True:
                    name = '请求任务名称: ' + data['action']['name']
                    userName = '请求用户名称: ' + data['action']["userName"]
                    loginIp = '任务用户 IP: ' + data['action']["loginIp"]
                    status = '取消任务结果: ' + data['action']["status"]
                    result = f'IP地址: {ip}\n' \
                             f'UUID: {uuid}\n' \
                             f'-----------------------------------------------------------------\n'
                    self.result_text.setText(result + name + '\n' + userName + '\n' + loginIp + '\n' + status)
                elif data['success'] == False:
                    status = '请求状态:' + str(data['success'])
                    err = '\n错误信息:' + data["error"]
                    return self.result_text.setText(status + err)
            except Exception as e:
                err = '捕获到异常,请检查IP地址或UUID是否正确\n' + '错误信息:' + str(e)
                return self.result_text.setText(err)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyWindow()

    # 设置QSS样式
    qss = '''
        QWidget {
            background-color: #f0f0f0;
        }

        QLabel {
            font-size: 14px;
            color: #333;
        }

        QLineEdit {
            padding: 6px;
            border: 1px solid #aaa;
            border-radius: 3px;
        }

        QPushButton {
            padding: 8px 16px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 3px;
        }

        QTextEdit {
            padding: 6px;
            border: 1px solid #aaa;
            border-radius: 3px;
        }
    '''
    app.setStyleSheet(qss)

    window.show()
    sys.exit(app.exec_())

2.企业微信推送小机器人

用linux运行
使用 pip install requests
使用 crontab -e 设置定时任务

import requests
import json

def get_english():
    url = 'https://api.oioweb.cn/api/common/OneDayEnglish'
    res = requests.get(url)
    json_data = json.loads(res.text)
    title = json_data['result']['dateline']+' 每日英语推送'
    description = json_data['result']['note']
    picurl = json_data['result']['img']
    tts = json_data['result']['tts']
    return title, description, picurl, tts

title, description, picurl, tts = get_english()
reboot_url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=你的机器人key'
headers = {'Content-Type': 'application/json'}
data = {
        "msgtype": "news",
        "news": 
            {
                "articles": [{
                    "title": title,
                    "description": description,
                    "url": tts,
                    "picurl": picurl
                    }]
            }
        }
try:
    requests.post(reboot_url, headers=headers, data=json.dumps(data))
except Exception as e:
    print(e)

添加新评论