[青龙面板] 阿里云香港轻量服务器抢购脚本

前言

本脚本作者由L站菟子@tuzi编写发布

因为tuzi不想熬夜所以才有了此脚本的诞生,感谢熬夜带来的福利

正文

在脚本管理 新建脚本 后缀为.py

图片[1]-[青龙面板] 阿里云香港轻量服务器抢购脚本-九七博客

在编辑器里填写access_key_idaccess_key_secret

图片[2]-[青龙面板] 阿里云香港轻量服务器抢购脚本-九七博客

access_key_id及access_key_secret 获取方式 创建阿里云AccessKey_访问控制(RAM)-阿里云帮助中心 (aliyun.com)

保存后在依赖管理 python 创建依赖

alibabacloud_tea_console
alibabacloud_tea_util
alibabacloud_tea_openapi
alibabacloud_swas_open20200601
图片[3]-[青龙面板] 阿里云香港轻量服务器抢购脚本-九七博客

依赖安装成功 在定时任务里新建任务 运行实例如下

图片[4]-[青龙面板] 阿里云香港轻量服务器抢购脚本-九七博客
图片[5]-[青龙面板] 阿里云香港轻量服务器抢购脚本-九七博客

抢购成功后记得删除access_key_id及access_key_secret

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import os
import sys
from os import path
from typing import List

from alibabacloud_swas_open20200601.client import Client as SWAS_OPEN20200601Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_swas_open20200601 import models as swas__open20200601_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_console.client import Client as ConsoleClient
from alibabacloud_tea_util.client import Client as UtilClient

class Sample:
    message = ""

    def __init__(self):
        self.client = self.create_client()

    @staticmethod
    def create_client() -> SWAS_OPEN20200601Client:
        """
        使用AK&SK初始化账号Client
        """
        config = open_api_models.Config(
            access_key_id=os.getenv('ALIBABA_CLOUD_ACCESS_KEY_ID', '填写ACCESS_KEY_ID'),
            access_key_secret=os.getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '填写ACCESS_KEY')
        )
        config.endpoint = 'swas.cn-hongkong.aliyuncs.com'
        return SWAS_OPEN20200601Client(config)

    def create_instance(self) -> None:
        """
        创建实例并处理结果
        """
        create_instances_request = swas__open20200601_models.CreateInstancesRequest(
            region_id='cn-hongkong',
            image_id='8b798eb927684a08b26bb95da94f5812',
            plan_id='swas.s2.c2m1s40b30t1.un',
            period=12,
            auto_renew=True
        )
        runtime = util_models.RuntimeOptions()
        try:
            resp = self.client.create_instances_with_options(create_instances_request, runtime)
            ConsoleClient.log(UtilClient.to_jsonstring(resp))
            self.message = "抢购成功"
        except Exception as error:
            self.message = f"抢购失败: {error.message}"
            print(f"错误信息: {error.message}")
            print(f"诊断信息: {error.data.get('Recommend')}")
            UtilClient.assert_as_string(error.message)
        self.send_notification()

    def send_notification(self) -> None:
        """
        发送推送通知
        """
        send = self.load_send()
        if callable(send):
            send("阿里云轻量抢购", self.message)
        else:
            print("加载通知服务失败")
            print(self.message)

    @staticmethod
    def load_send():
        """
        加载外部通知服务
        """
        cur_path = path.abspath(path.dirname(sys.argv[0]))
        notify_path = path.join(cur_path, "notify.py")
        
        if path.exists(notify_path):
            try:
                from notify import send
                return send
            except ImportError:
                print("无法导入 notify.py 中的 send 函数")
                return False
        else:
            print(f"未找到 notify.py 文件:{notify_path}")
            return False

if __name__ == '__main__':
    sample = Sample()
    sample.create_instance()
© 版权声明
THE END
喜欢就支持一下吧
点赞16 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容