更新: 阿里24抢购程序及实际效果
先前的版本在处理休眠时有点问题,这个版本修正了
因为正好需要24的机器,所以晚上抢了一把,很成功。注意截图时间。 :)
/*
https://help.aliyun.com/zh/ram/user-guide/create-an-accesskey-pair 获得主帐号的AccessKey ID AccessKey Secret
确保有不少于24块的余额
*/
package main
import (
"fmt"
"os"
"time"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
swas_open20200601 "github.com/alibabacloud-go/swas-open-20200601/client"
"github.com/alibabacloud-go/tea/tea"
)
func CreateClient(Key, Secret string) (_result *swas_open20200601.Client, _err error) {
config := &openapi.Config{
AccessKeyId: tea.String(Key),
AccessKeySecret: tea.String(Secret),
}
config.Endpoint = tea.String("swas.cn-hongkong.aliyuncs.com")
_result = &swas_open20200601.Client{}
_result, _err = swas_open20200601.NewClient(config)
return _result, _err
}
func SDKCreate(Key, Secret string) error {
client, err := CreateClient(Key, Secret)
if err != nil {
return err
}
request := &swas_open20200601.CreateInstancesRequest{
RegionId: tea.String("cn-hongkong"), //香港
ImageId: tea.String("8b798eb927684a08b26bb95da94f5812"), //debian11
PlanId: tea.String("swas.s2.c2m1s40b30t1.un"), //轻量24
Period: tea.Int32(1),
}
_, err = client.CreateInstances(request)
if err != nil {
return err
}
return nil
}
func main() {
location, err := time.LoadLocation("Asia/Shanghai")
if err != nil {
fmt.Println("Error loading location:", err)
return
}
time.Local=location
args := os.Args
if len(args) < 3 {
fmt.Printf("Usage: %s <AccessKeyId> <AccessKeySecret>\n", args[0])
return
}
now := time.Now()
tomorrowMidnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local)
if now.Hour() != 0 {
fmt.Println("Waiting for 00:00:00 to start...")
sleepDuration := tomorrowMidnight.Sub(now)
time.Sleep(sleepDuration)
}
//因为阿里可能不整点放货,所以,这里不再连续抢了,因为SDK的限制是10次/分钟
//现在改为每分钟抢10次左右,一直抢5分钟。 也就是6秒左右抢一次。按照中位数,3秒,应该还是希望很大。
for i := 1; i <= 50; i++ {
err := SDKCreate(args[1], args[2])
if err != nil {
fmt.Printf("%d - Error: %s\n", i, err)
} else {
fmt.Printf("%d - Success! Please check it in your aliyun console.\n", i)
break
}
time.Sleep(6 * time.Second)
}
fmt.Println("Done")
}