-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (113 loc) · 4.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from dotenv import load_dotenv
import requests
import json
import time
import logging
from datetime import datetime
import pytz
from apscheduler.schedulers.blocking import BlockingScheduler
load_dotenv()
authorization = os.getenv('Authorization')
min_day = os.getenv('MIN_DAY')
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
headers = {
"Authorization": authorization,
"Content-Type": "application/json;charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
}
def open_machine(instance_uuid: str = None):
if not instance_uuid:
return False
url = "https://www.autodl.com/api/v1/instance/power_on"
body = {
"instance_uuid": str(instance_uuid),
"payload": "non_gpu"
}
response = requests.post(url=url, headers=headers, data=json.dumps(body))
json_data = response.json()
logger.info(f"uuid: {instance_uuid}, open 机器开机")
logger.info(f"{instance_uuid} response: {json_data}")
if json_data['code'] == "Success":
logger.info(f"uuid: {instance_uuid}, 机器开机成功")
return True
return False
def close_machine(instance_uuid: str = None):
if not instance_uuid:
return False
url = "https://www.autodl.com/api/v1/instance/power_off"
body = {
"instance_uuid": str(instance_uuid)
}
response = requests.post(url=url, headers=headers, data=json.dumps(body))
json_data = response.json()
logger.info(f"uuid: {instance_uuid}, close")
logger.info(f"uuid: {instance_uuid}, close 机器关机")
logger.info(f"{instance_uuid} response: {json_data}")
if json_data['code'] == "Success":
logger.info(f"uuid: {instance_uuid}, 机器关机成功")
return True
return False
def check_instance(page: int = 1):
url = "https://www.autodl.com/api/v1/instance"
body = {
"date_from": "",
"date_to": "",
"page_index": page,
"page_size": 100,
"status": [],
"charge_type": []
}
response = requests.post(url=url, headers=headers, data=json.dumps(body))
json_data = response.json()
if json_data['code'] == "Success":
data_list = json_data['data']['list']
for each_data in data_list:
uuid = each_data.get('uuid', '')
if uuid == "":
return
machine_alias = each_data.get('machine_alias', '')
region_name = each_data.get('region_name', '')
status = each_data.get('status', '')
status_at = each_data.get('status_at', '')
phone = each_data.get('phone', '')
status_at_time = datetime.fromisoformat(status_at)
current_date = datetime.now(pytz.timezone('Asia/Shanghai'))
date_difference = current_date - status_at_time
date_difference_day = date_difference.days
logger.info(f"now: {current_date.strftime('%Y-%m-%d %H:%M:%S')}, "
f"phone: {phone}, name: {region_name} {machine_alias} {uuid}, "
f"status: {status}, date_diff: {date_difference_day}天")
# 小于1天
if date_difference_day >= int(min_day):
logger.info(f"准备续费: {uuid}")
# 续费逻辑
open_machine(uuid)
time.sleep(60)
close_machine(uuid)
time.sleep(5)
else:
logger.info(f"等待下次扫描: {uuid}")
# 不作操作
pass
else:
logger.error(f"扫描失败: {json_data}")
def main():
if not authorization:
logger.error("Authorization is None !")
exit(-1)
logger.info(f"now: {datetime.now(pytz.timezone('Asia/Shanghai')).strftime('%Y-%m-%d %H:%M:%S')}")
check_instance()
if __name__ == "__main__":
# 先检查一次 再定时检查
main()
scheduler = BlockingScheduler()
scheduler.add_job(main, 'interval', hours=1)
try:
# 启动调度器
scheduler.start()
except (KeyboardInterrupt, SystemExit):
print("KeyboardInterrupt Or SystemExit")