-
Notifications
You must be signed in to change notification settings - Fork 30
/
task.py
122 lines (102 loc) · 4.31 KB
/
task.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
# import time
import json
import random
import requests
from concurrent.futures import ThreadPoolExecutor
from util import multi_accounts_task, GracefulKiller
MIN_INVOKE_TIMES = 176
MAX_INVOKE_TIMES = 237
EXECUTOR_KILLER = GracefulKiller()
def config(path, data=None):
if not data:
with open(path, mode="r") as conf:
return json.load(conf)
# fast-fail
json.loads(json.dumps(data))
with open(path, mode="w") as conf:
json.dump(data, conf)
# with open(path, mode="r+") as conf:
# if not data:
# return json.load(conf)
# json.dump(data, conf, sort_keys=True, indent=4)
def get_access_token(app):
try:
return requests.post(
"https://login.microsoftonline.com/common/oauth2/v2.0/token",
data={
"grant_type": "refresh_token",
"refresh_token": app["refresh_token"],
"client_id": app["client_id"],
"client_secret": app["client_secret"],
"redirect_uri": app["redirect_uri"],
},
).json()
except Exception:
return {}
def invoke_api(path):
app = config(path)
tokens = get_access_token(app)
access_token = tokens.get("access_token", "")
refresh_token = tokens.get("refresh_token", "")
username = app["username"]
if len(access_token) < 5 or len(refresh_token) < 5:
return f"✘ 账号 [{username}] 调用失败."
apis = [
"https://graph.microsoft.com/v1.0/groups",
"https://graph.microsoft.com/v1.0/sites/root",
"https://graph.microsoft.com/v1.0/sites/root/sites",
"https://graph.microsoft.com/v1.0/sites/root/drives",
"https://graph.microsoft.com/v1.0/sites/root/columns",
"https://graph.microsoft.com/v1.0/me/",
"https://graph.microsoft.com/v1.0/me/events",
"https://graph.microsoft.com/v1.0/me/people",
"https://graph.microsoft.com/v1.0/me/contacts",
"https://graph.microsoft.com/v1.0/me/calendars",
"https://graph.microsoft.com/v1.0/me/drive",
"https://graph.microsoft.com/v1.0/me/drive/root",
"https://graph.microsoft.com/v1.0/me/drive/root/children",
"https://graph.microsoft.com/v1.0/me/drive/recent",
"https://graph.microsoft.com/v1.0/me/drive/sharedWithMe",
"https://graph.microsoft.com/v1.0/me/onenote/pages",
"https://graph.microsoft.com/v1.0/me/onenote/sections",
"https://graph.microsoft.com/v1.0/me/onenote/notebooks",
"https://graph.microsoft.com/v1.0/me/outlook/masterCategories",
"https://graph.microsoft.com/v1.0/me/mailFolders",
"https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages/delta",
"https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messageRules",
"https://graph.microsoft.com/v1.0/me/messages",
"https://graph.microsoft.com/v1.0/me/messages?$filter=importance eq 'high'",
'https://graph.microsoft.com/v1.0/me/messages?$search="hello world"',
"https://graph.microsoft.com/beta/me/messages?$select=internetMessageHeaders&$top",
]
headers = {"Authorization": f"Bearer {access_token}"}
def single_period(period):
if EXECUTOR_KILLER.kill_now:
return ""
result = "=" * 100 + "\n"
random.shuffle(apis)
probability = random.random()
for api in apis:
if random.random() < probability:
continue
try:
if requests.get(api, headers=headers).status_code == 200:
result += "{:>20s} | {:>6s} | {:<50s}\n".format(
f"账号: {username}", f"周期: {period}", f"成功: {api}"
)
except Exception:
# time.sleep(random.random()*3)
pass
if EXECUTOR_KILLER.kill_now:
return result
return result
with ThreadPoolExecutor() as executor:
max = random.randint(MIN_INVOKE_TIMES, MAX_INVOKE_TIMES)
futures = [executor.submit(single_period, period) for period in range(1, max)]
result = "".join((f.result() for f in futures))
# save refresh_token
app["refresh_token"] = refresh_token
config(path, app)
return f"{result}✔ 账号 [{username}] 调用成功."
if __name__ == "__main__":
multi_accounts_task(invoke_api)