-
Notifications
You must be signed in to change notification settings - Fork 7
/
DeleteMyHistory.py
298 lines (233 loc) · 10.2 KB
/
DeleteMyHistory.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf-8 -*-
import json
import re
import sys
import traceback
import bs4
import requests
import os
import subprocess
import base64
from aes import AEScoder
env_dist = os.environ # environ是在os.py中定义的一个dict environ = {}
if(env_dist.__contains__('NOLOG') and env_dist['NOLOG'] == '1'):
sys.stdout = open(os.devnull, 'w')
def loadCookie(sess):
if(env_dist.__contains__('COOKIEKEY1') and env_dist.__contains__('COOKIEKEY2')):
print("KEYExist");
else:
print("NOKEY")
exit(1)
cookies = open("/".join([sys.path[0], "cookie.json.enc"])).read();
key = env_dist['COOKIEKEY1'] + env_dist['COOKIEKEY2']
t = AEScoder(key);
cookies = t.decrypt(cookies);
cookies = cookies.replace("\n", "")
cookies = json.loads(cookies)
sess.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36"
for cookie in cookies:
sess.cookies[cookie["name"]] = cookie["value"]
def getTbs(sess):
success = False
while not success:
try:
res = sess.get("http://tieba.baidu.com/dc/common/tbs", timeout=5)
success = True
except Exception:
traceback.print_exc()
pass
tbs = res.json()["tbs"]
return tbs
def getThreadList(sess, startPageNumber, endPageNumber):
threadList = list()
tidExp = re.compile(r"/([0-9]{1,})")
pidExp = re.compile(r"pid=([0-9]{1,})")
for number in range(startPageNumber, endPageNumber + 1):
print("Now in thread page", number)
url = "http://tieba.baidu.com/i/i/my_tie?pn=" + str(number)
res = sess.get(url)
if res.url == "http://static.tieba.baidu.com/tb/error.html?ErrType=1":
print("Cookie has been expried, Please update it")
return
# fo = open("S___" + str(number) + ".html" , "w")
# print("文件名为: ", fo.name)
# fo.write( res.text );
# fo.close();
html = bs4.BeautifulSoup(res.text, "lxml")
elements = html.find_all(name="a", attrs={"class": "thread_title"})
for element in elements:
print(element)
thread = element.get("href")
threadDict = dict()
threadDict["tid"] = tidExp.findall(thread)[0]
threadDict["pid"] = pidExp.findall(thread)[0]
threadList.append(threadDict)
return threadList
def getReplyList(sess, startPageNumber, endPageNumber):
replyList = list()
tidExp = re.compile(r"/([0-9]{1,})")
pidExp = re.compile(r"pid=([0-9]{1,})") # 主题贴和回复都为 pid
cidExp = re.compile(r"cid=([0-9]{1,})") # 楼中楼为 cid
for number in range(startPageNumber, endPageNumber + 1):
print("Now in reply page", number)
url = "http://tieba.baidu.com/i/i/my_reply?pn=" + str(number)
res = sess.get(url)
if res.url == "http://static.tieba.baidu.com/tb/error.html?ErrType=1":
print("Cookie has been expried, Please update it")
return
html = bs4.BeautifulSoup(res.text, "lxml")
elements = html.find_all(name="a", attrs={"class": "b_reply"})
for element in elements:
reply = element.get("href")
if reply.find("pid") != -1:
tid = tidExp.findall(reply)
pid = pidExp.findall(reply)
cid = cidExp.findall(reply)
replyDict = dict()
replyDict["tid"] = tid[0]
if cid and cid[0] != "0": # 如果 cid != 0, 这个回复是楼中楼, 否则是一整楼的回复
replyDict["pid"] = cid[0]
else:
replyDict["pid"] = pid[0]
replyList.append(replyDict)
return replyList
def getFollowedBaList(sess, startPageNumber, endPageNumber):
baList = list()
for number in range(startPageNumber, endPageNumber + 1):
print("Now in followed Ba page", number)
url = "http://tieba.baidu.com/f/like/mylike?pn=" + str(number)
res = sess.get(url)
if res.url == "http://static.tieba.baidu.com/tb/error.html?ErrType=1":
print("Cookie has been expried, Please update it")
return
html = bs4.BeautifulSoup(res.text, "lxml")
elements = html.find_all(name="span")
for element in elements:
baDict = dict()
baDict["fid"] = element.get("balvid")
baDict["tbs"] = element.get("tbs")
baDict["fname"] = element.get("balvname")
baList.append(baDict)
return baList
def getConcerns(sess, startPageNumber, endPageNumber):
concernList = list()
for number in range(startPageNumber, endPageNumber + 1):
print("Now in concern page", number)
url = "http://tieba.baidu.com/i/i/concern?pn=" + str(number)
res = sess.get(url)
if res.url == "http://static.tieba.baidu.com/tb/error.html?ErrType=1":
print("Cookie has been expried, Please update it")
return
html = bs4.BeautifulSoup(res.text, "lxml")
elements = html.find_all(name="input", attrs={"class": "btn_unfollow"})
for element in elements:
concernDict = dict()
concernDict["cmd"] = "unfollow"
concernDict["tbs"] = element.get("tbs")
concernDict["id"] = element.get("portrait")
concernList.append(concernDict)
return concernList
def getFans(sess, startPageNumber, endPageNumber):
fansList = list()
tbsExp = re.compile(r"tbs : '([0-9a-zA-Z]{16})'") # 居然还有一个短版 tbs.... 绝了
for number in range(startPageNumber, endPageNumber + 1):
print("Now in fans page", number)
url = "http://tieba.baidu.com/i/i/fans?pn=" + str(number)
res = sess.get(url)
if res.url == "http://static.tieba.baidu.com/tb/error.html?ErrType=1":
print("Cookie has been expried, Please update it")
return
tbs = tbsExp.findall(res.text)[0]
html = bs4.BeautifulSoup(res.text, "lxml")
elements = html.find_all(name="input", attrs={"class": "btn_follow"})
for element in elements:
fanDict = dict()
fanDict["cmd"] = "add_black_list"
fanDict["tbs"] = tbs
fanDict["portrait"] = element.get("portrait")
fansList.append(fanDict)
return fansList
def deleteThread(sess, threadList):
url = "https://tieba.baidu.com/f/commit/post/delete"
count = 0
for threadDict in threadList:
print("Now deleting", threadDict)
postData = dict()
postData["tbs"] = getTbs(sess)
for idName in threadDict:
postData[idName] = threadDict[idName]
res = sess.post(url, data=postData)
# print(res.text)
print(res.text.encode('latin-1').decode('unicode_escape'))
if res.json()["err_code"] == 220034: #达到上限
print("Limit exceeded, exiting.")
return count
else:
count += 1
return count
def deleteFollowedBa(sess, baList):
url = "https://tieba.baidu.com/f/like/commit/delete"
for ba in baList:
print("Now unfollowing", ba)
res = sess.post(url, data=ba)
print(res.text)
def deleteConcern(sess, concernList):
url = "https://tieba.baidu.com/home/post/unfollow"
for concern in concernList:
print("Now unfollowing", concern)
res = sess.post(url, data=concern)
print(res.text)
def deleteFans(sess, fansList):
url = "https://tieba.baidu.com/i/commit"
for fans in fansList:
print("Now blocking fans", fans)
res = sess.post(url, data=fans)
print(res.text)
def check(obj):
if obj is None:
exit(0)
def main():
config = open("/".join([sys.path[0], "config.json"])).read().replace("\n", "")
config = json.loads(config)
sess = requests.session()
loadCookie(sess)
if config["thread"]["enable"]:
threadList = getThreadList(sess, config["thread"]["start"], config["thread"]["end"])
check(threadList)
print("Collected", len(threadList), "threads", end="\n\n")
count = deleteThread(sess, threadList)
print(count, "threads has been deleted", end="")
if len(threadList) != count:
print(", left", len(threadList) - count, "threads due to limit exceeded.", end="\n\n")
else:
print(".", end="\n\n")
if config["reply"]["enable"]:
replyList = getReplyList(sess, config["reply"]["start"], config["reply"]["end"])
check(replyList)
print("Collected", len(replyList), "replys", end="\n\n")
count = deleteThread(sess, replyList)
print(count, "replys has been deleted", end="")
if len(replyList) != count:
print(", left", len(replyList) - count, "replys due to limit exceeded.", end="\n\n")
else:
print(".", end="\n\n")
if config["followedBa"]["enable"]:
baList = getFollowedBaList(sess, config["followedBa"]["start"], config["followedBa"]["end"])
check(baList)
print("Collected", len(baList), "followed Ba", end="\n\n")
deleteFollowedBa(sess, baList)
print(len(baList), "followed Ba has been deleted.", end="\n\n")
if config["concern"]["enable"]:
concernList = getConcerns(sess, config["concern"]["start"], config["concern"]["end"])
check(concernList)
print("Collected", len(concernList), "concerns", end="\n\n")
deleteConcern(sess, concernList)
print(len(concernList), "concerns has been deleted.", end="\n\n")
if config["fans"]["enable"]:
fansList = getFans(sess, config["fans"]["start"], config["fans"]["end"])
check(fansList)
print("Collected", len(fansList), "fans", end="\n\n")
deleteFans(sess, fansList)
print(len(fansList), "fans has been deleted.", end="\n\n")
if __name__ == "__main__":
main()