-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path贴吧.py
232 lines (196 loc) · 7.03 KB
/
贴吧.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import requests
import hashlib
import time
import copy
import logging
import random
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor
from config import Config
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
ENV = os.environ
s = requests.Session()
adapter = requests.adapters.HTTPAdapter(
pool_connections=Config.HTTP_SETTINGS["POOL_CONNECTIONS"], # 连接池的连接数
pool_maxsize=Config.HTTP_SETTINGS["POOL_MAXSIZE"], # 连接池的最大数量
max_retries=Config.HTTP_SETTINGS["RETRY_TIMES"], # 最大重试次数
pool_block=False, # 连接池满时不阻塞
)
s.mount("http://", adapter)
s.mount("https://", adapter)
def get_tbs(tieba_cookie):
logger.info("获取tbs开始")
headers = copy.copy(Config.HEADERS)
headers.update({"Cookie": f"TIEBA_COOKIE={tieba_cookie}"})
try:
tbs = s.get(
url=Config.API_URLS["TBS_URL"],
headers=headers,
timeout=Config.HTTP_SETTINGS["TIMEOUT"],
).json()["tbs"]
except Exception as e:
logger.error("获取tbs出错: %s", e)
logger.info("重新获取tbs开始")
tbs = s.get(
url=Config.API_URLS["TBS_URL"],
headers=headers,
timeout=Config.HTTP_SETTINGS["TIMEOUT"],
).json()["tbs"]
logger.info("获取tbs结束")
return tbs
def get_favorite(tieba_cookie):
logger.info("获取关注的贴吧开始")
all_bars = []
page = 1
while True:
data = {
"TIEBA_COOKIE": tieba_cookie,
"_client_type": "2",
"_client_id": "wappc_1534235498291_488",
"_client_version": "9.7.8.0",
"_phone_imei": "000000000000000",
"from": "1008621y",
"page_no": str(page),
"page_size": "200",
"model": "MI+5",
"net_type": "1",
"timestamp": str(int(time.time())),
"vcode_tag": "11",
}
try:
res = s.post(
url=Config.API_URLS["LIKE_URL"],
data=encodeData(data),
timeout=Config.HTTP_SETTINGS["TIMEOUT"],
).json()
if not res.get("forum_list"):
break
for forum_type in ["non-gconforum", "gconforum"]:
if forum_type in res["forum_list"]:
items = res["forum_list"][forum_type]
if isinstance(items, list):
all_bars.extend(items)
else:
all_bars.append(items)
if res.get("has_more") != "1":
break
page += 1
except Exception as e:
logger.error(f"获取第{page}页贴吧列表失败: {e}")
break
logger.info(f"共获取到{len(all_bars)}个贴吧")
return all_bars
def encodeData(data):
s = ""
keys = data.keys()
for i in sorted(keys):
s += i + "=" + str(data[i])
sign = hashlib.md5((s + "tiebaclient!!!").encode("utf-8")).hexdigest().upper()
data.update({"sign": str(sign)})
return data
def client_sign(tieba_cookie, tbs, fid, kw):
logger.info("开始签到贴吧:" + kw)
data = copy.copy(Config.SIGN_DATA)
data.update(
{
"TIEBA_COOKIE": tieba_cookie,
"fid": fid,
"kw": kw,
"tbs": tbs,
"timestamp": str(int(time.time())),
}
)
data = encodeData(data)
res = s.post(
url=Config.API_URLS["SIGN_URL"],
data=data,
timeout=Config.HTTP_SETTINGS["TIMEOUT"],
).json()
return res
def sign_one_bar(args):
tieba_cookie, tbs, bar = args
try:
time.sleep(
random.randint(
Config.THREAD_SETTINGS["MIN_DELAY"], Config.THREAD_SETTINGS["MAX_DELAY"]
)
)
res = client_sign(tieba_cookie, tbs, bar["id"], bar["name"])
error_code = res.get("error_code", "unknown")
status = Config.ERROR_CODES.get(str(error_code), f"未知错误: {error_code}")
if error_code in Config.SUCCESS_CODES:
logger.info(f'贴吧:{bar["name"]} 签到状态:{status}')
elif error_code in Config.CRITICAL_ERRORS:
logger.warning(f'贴吧:{bar["name"]} 签到状态:{status}')
else:
logger.error(f'贴吧:{bar["name"]} 签到状态:{status}')
return {
"name": bar["name"],
"bar": bar,
"status": status,
"error_code": error_code,
"is_success": error_code in Config.SUCCESS_CODES,
}
except Exception as e:
logger.error(f'贴吧:{bar["name"]} 签到异常:{str(e)}')
return {
"name": bar["name"],
"bar": bar,
"status": "签到异常",
"error": str(e),
"is_success": False,
}
def main():
if "TIEBA_COOKIE" not in ENV:
logger.error("未配置TIEBA_COOKIE")
return
tieba_cookie = ENV["TIEBA_COOKIE"].split("#")
max_workers = min(Config.THREAD_SETTINGS["MAX_WORKERS"], os.cpu_count() * 5)
for n, cookie in enumerate(tieba_cookie):
logger.info(f"开始签到第{n+1}个用户")
try:
tbs = get_tbs(cookie)
favorites = get_favorite(cookie)
except Exception as e:
logger.error(f"用户签到失败: {e}")
continue
rounds = 0
to_sign = favorites[:]
all_results = []
while rounds < 5 and to_sign: # 当 to_sign 为空时,循环会结束
rounds += 1
logger.info(f"开始第 {rounds} 轮签到,待签到贴吧数:{len(to_sign)}")
results = []
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_to_bar = {
executor.submit(sign_one_bar, (cookie, tbs, bar)): bar
for bar in to_sign
}
# 并发执行签到
for future in concurrent.futures.as_completed(future_to_bar):
result = future.result()
if result:
results.append(result)
if result.get("error_code") in Config.CRITICAL_ERRORS:
logger.warning(
f"检测到严重问题:{result['status']},停止当前用户的签到"
)
executor._threads.clear()
break
all_results.extend(results)
# 过滤出未签到成功的贴吧
to_sign = [
result["bar"] for result in results if not result.get("is_success")
]
if to_sign:
logger.info(f"本轮签到完成,等待1分钟后进行下一轮签到")
time.sleep(60)
else:
logger.info("所有贴吧签到成功,结束签到")
break
if __name__ == "__main__":
main()