This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathweibo.py
201 lines (165 loc) · 6.1 KB
/
weibo.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import requests as req
import re
import time
import random
# 过滤不必要的信息
def filter(card_group):
_list = []
for card in card_group:
if card["card_type"] == "8":
# container_id
cid = re.findall(
"(?<=containerid=).*?(?=&)|(?<=containerid=).*",
card["scheme"],
)
if len(cid) > 0:
super_item = {
"level": re.findall(r"LV.\d", card["desc1"])[0],
"title": card["title_sub"],
"id": cid[0],
"status": card["buttons"][0]["name"],
}
_list.append(super_item)
return _list
class userInfo:
def __init__(self):
self.uid = ""
self.name = ""
self.location = ""
self.description = ""
self.cover_image = ""
class weibo:
def __init__(self, conf):
self.info = userInfo()
self.info.uid = conf["uid"]
self.params = {
"gsid": conf["gsid"], # 身份验证
"c": "android", # 客户端校验
"from": conf["from"], # 客户端校验
"s": conf["s"], # 校验参数
"uid": conf["uid"], # 用于获取用户信息
}
self.headers = {
"Host": "api.weibo.cn",
"Connection": "keep-alive",
"Accept-Encoding": "gzip",
"content-type": "application/json;charset=utf-8",
"user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X)",
"Authorization": f"WB-SUT {conf['gsid']}",
}
# 获取超话列表
def get_chaohua_List(self) -> list:
since_id = ""
_list = []
try:
while True:
params = {
"containerid": "100803_-_followsuper",
"fid": "100803_-_followsuper",
"since_id": since_id,
"cout": 20,
}
params.update(self.params)
respJson = req.get(
"https://api.weibo.cn/2/cardlist",
headers=self.headers,
params=params,
).json()
# 获得超话数组
if "errno" not in respJson:
for card in respJson["cards"]:
li = filter(card["card_group"])
_list.extend(li)
# 获取下一页 id
since_id = respJson["cardlistInfo"]["since_id"]
# 获取到空就是爬取完了
if since_id == "":
print("超话列表获取完毕")
for i in _list:
print(f"{i['title']}: {i['id']}")
break
else:
raise Exception(respJson["errmsg"])
except Exception as e:
print(f"获取超话列表时出错, 原因: {e}")
return _list
# 超话签到
def chaohua_checkin(self, item: dict):
try:
if item["status"] == "签到":
params = {
"request_url": f"http://i.huati.weibo.com/mobile/super/active_checkin?pageid={item['id']}&&sg_tab_config=2&in_page=1"
}
params.update(self.params)
respJson = req.get(
"https://api.weibo.cn/2/page/button",
headers=self.headers,
params=params,
).json()
if "errno" in respJson:
raise Exception(respJson["errmsg"])
else:
print(f"[success] {item['title']}")
return {
"status": True,
"msg": "签到成功",
"rank": respJson["fun_data"]["check_count"], # 第几个签到
"score": respJson["fun_data"]["score"], # 积分
"exp": respJson["fun_data"]["int_ins"], # 经验
"continute": respJson["fun_data"]["check_int"], # 连续签到
"title": item["title"],
}
else:
print(f"[success] {item['title']}")
return {
"status": False,
"msg": "已签到",
"title": item["title"],
}
except Exception as e:
return {
"status": False,
"msg": e,
"title": item["title"],
}
# 获取用户信息
def update_user_info(self):
url = "https://api.weibo.cn/2/profile"
respJson = req.get(url, params=self.params, headers=self.headers).json()
user_info = respJson["userInfo"]
self.info.name = user_info["name"]
self.info.location = user_info["location"]
self.info.description = user_info["description"]
self.info.cover_image = user_info["cover_image"]
def start(self):
self.update_user_info()
# 获取超话列表
chaohua_list = self.get_chaohua_List()
messages = []
for item in chaohua_list:
time.sleep(random.randint(5, 10))
res = self.chaohua_checkin(item)
messages.append(res)
table = [("超话", "排名", "经验", "积分", "连续天数", "结果")]
for msg in messages:
if msg["status"]:
el = (
msg["title"],
msg["rank"],
msg["exp"],
msg["score"],
msg["continute"],
msg["msg"],
)
else:
el = (msg["title"], "", "", "", "", msg["msg"])
table.append(el)
return {
"title": "微博超话签到",
"message": [
{
"txt": {"content": f"用户名: {self.info.name}", "end": "\n\n"},
"table": {"contents": table, "end": "\n\n"},
}
],
}