-
Notifications
You must be signed in to change notification settings - Fork 3
/
clean_tieba.py
359 lines (328 loc) · 13.8 KB
/
clean_tieba.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
import requests
from datetime import datetime
from bs4 import BeautifulSoup
from requests.utils import cookiejar_from_dict
import lxml
import json
import os
import time
from optparse import OptionParser
def log(text):
s = '[%s] %s' % (str(datetime.now()), text)
open('clean_tieba.log', 'a').write(s + '\n')
print('[%s] %s' % (str(datetime.now()), text))
class Tieba:
user_id = -1
username = ''
match = '.*'
r = requests.Session()
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Referer': 'http://tieba.baidu.com/',
'X-Requested-With': 'XMLHttpRequest',
'Accept-Language': 'en-US,en;q=0.9',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Origin': 'http://tieba.baidu.com',
}
def error_check(self, text):
log(text)
try:
_ = json.loads(text)
if _['err_code'] == 0:
log('Success')
return True
elif _['err_code'] == 220034:
log('Failed: 您的操作太频繁了')
return 'exit'
elif _['err_code'] == 260005:
log('Failed: Cookies失效')
return False
elif _['err_code'] == 230308:
log('Failed: 据说tbs不对')
return False
else:
log('Failed: 不造啥错误')
return False
except json.decoder.JSONDecodeError:
return False
def get_tie(self):
tie_list = []
page = 1
while(1):
url = 'https://tieba.baidu.com/i/%s/my_tie?&pn=%d' % (self.user_id, page)
log('-->%s' % url)
_ = self.r.get(url, headers=self.headers)
my_tie = BeautifulSoup(_.text, 'lxml').select('.simple_block_container')[0].ul
lis = my_tie.select('li')
if len(lis) == 0:
break
for li in lis:
a = li.select('a')
bar_name = a[0].text
bar_url = 'https://tieba.baidu.com' + a[0]['href']
tie_name = a[1].text
tie_url = 'https://tieba.baidu.com' + a[1]['href']
new_tie = {
'bar_name': bar_name,
'bar_url': bar_url,
'tie_name': tie_name,
'tie_url': tie_url
}
log('add new tie: [%s][%s]' % (bar_name, tie_name))
tie_list.append(new_tie)
page += 1
return tie_list
# print(json.dumps(tie_list, ensure_ascii=False, indent=4))
def get_reply(self):
reply_list = []
page = 1
while(1):
url = 'https://tieba.baidu.com/i/%s/my_reply?&pn=%d' % (self.user_id, page)
log('-->%s' % (url))
_ = self.r.get(url, headers=self.headers)
my_reply = BeautifulSoup(_.text, 'lxml').select('.t_forward')
if len(my_reply) == 0:
break
# print(my_reply)
for one in my_reply:
try:
reply_content = one.select('.for_reply_context')[0].text
reply_url = 'https://tieba.baidu.com' + one.select('.for_reply_context')[0]['href']
except IndexError:
reply_content = ''
reply_url = 'https://tieba.baidu.com' + one.select('.b_reply')[0]['href']
tie_name = one.select('.thread_title')[0].text
tie_url = 'https://tieba.baidu.com' + one.select('.thread_title')[0]['href']
bar = one.select('.common_source_main')[0].select('a')[-1]
bar_name = bar.text
bar_url = 'https://tieba.baidu.com' + bar['href']
new_reply = {
'reply_content': reply_content,
'reply_url': reply_url,
'tie_name': tie_name,
'tie_url': tie_url,
'bar_name': bar_name,
'bar_url': bar_url,
}
if re.match(self.match, reply_content):
log('add new reply: [%s][%s]' % (reply_content, tie_name))
reply_list.append(new_reply)
else:
log('NOT match, pass: [%s][%s]' % (reply_content, tie_name))
page += 1
return reply_list
# print(json.dumps(reply_list, ensure_ascii=False, indent=4))
def del_tie(self, reply):
log(json.dumps(reply, ensure_ascii=False, indent=4))
log('-->%s' % reply['tie_url'])
_ = self.r.get(reply['tie_url'], headers=self.headers)
html = _.text
check = re.findall('该贴已被删除', html)
if len(check) > 0:
tid = re.findall('p/(\d+)\?', reply['tie_url'])[0]
url = 'https://tieba.baidu.com/errorpage/deledErrorInfo?tid=%s' % tid
error = json.loads(self.r.get(url, headers=self.headers).text)
type_no = int(error['data']['type'])
if type_no == 0:
log('很抱歉,该贴已被删除')
elif type_no == 1:
log('小广告太多啦。商品交易贴,度娘建议每天不能超过5条哦')
elif type_no == 2:
log('亲,由于您使用机器刷贴,影响了吧友在贴吧的浏览体验,导致贴子被删')
elif type_no == 3:
log('亲,由于您的贴子内含有敏感词汇/图片,影响了吧友在贴吧的浏览体验,导致贴子被删')
elif type_no == 4:
log('很抱歉,您的贴子已被系统删除')
elif type_no == 5:
log('很抱歉,您的贴子已被自己删除')
elif type_no == 6:
log('很抱歉,您的贴子已被吧务删除')
else:
log('Failed')
return False
log('Success')
return True
if '该吧被合并您所访问的贴子无法显示' in html:
log('该吧被合并您所访问的贴子无法显示')
log('Success')
return True
elif '您访问的贴子被隐藏' in html:
log('抱歉,您访问的贴子被隐藏,暂时无法访问')
log('Failed')
return False
else:
pass
data = {
'ie': re.findall('\"?charset\"?\s*:\s*[\'\"]?(.*?)[\'\"]', html)[0].lower(),
# 'tbs': re.findall('"tbs" : "([\d\w]+)"', html)[0],
'tbs': re.findall('\"?tbs\"?\s*:\s*[\'\"]?([\w\d]+)[\'\"]', html)[0],
'kw': re.findall('name="kw" value="(.*?)"', html)[0].encode().decode(),
'fid': re.findall("fid:'(\d+)'", html)[0],
'tid': re.findall("tid:'(\d+)'", html)[0],
'username': self.username,
'delete_my_post': 1,
'delete_my_thread' : 0,
'is_vipdel': 0,
# 'pid': re.findall('pid=(\d+)&', reply['tie_url'])[0],
'pid': re.findall('cid=(\d+)#', reply['tie_url'])[0],
'is_finf': 'false'
}
if data['pid'] == '0':
data['pid'] = re.findall('pid=(\d+)', reply['tie_url'])[0]
url = 'https://tieba.baidu.com/f/commit/post/delete'
log('-->%s' % url)
log('delete reply')
h = self.headers
h.update({'Referer': reply['tie_url']})
log(data)
_ = self.r.post(url, data=data, headers=h)
log(_.status_code)
# log(_.content.decode())
# log(_.request.headers)
# exit(0)
return self.error_check(_.text)
def del_reply(self, reply):
log(json.dumps(reply, ensure_ascii=False, indent=4))
log('-->%s' % reply['reply_url'])
_ = self.r.get(reply['reply_url'], headers=self.headers)
html = _.text
if '该吧被合并您所访问的贴子无法显示' in html:
log('该吧被合并您所访问的贴子无法显示')
log('Success')
return True
elif '您访问的贴子被隐藏' in html:
log('抱歉,您访问的贴子被隐藏,暂时无法访问')
log('Failed')
return False
else:
pass
pid = re.findall('cid=(\d+)#', reply['reply_url'])[0]
if pid == '0':
pid = re.findall('pid=(\d+)&', reply['reply_url'])[0]
data = {
'ie': re.findall('\"?charset\"?\s*:\s*[\'\"]?(.*?)[\'\"]', html)[0].lower(),
'tbs': re.findall('\"?tbs\"?\s*:\s*[\'\"]?([\w\d]+)[\'\"]', html)[0],
'kw': re.findall('name="kw" value="(.*?)"', html)[0].encode().decode(),
'fid': re.findall("fid:'(\d+)'", html)[0],
'tid': re.findall("tid:'(\d+)'", html)[0],
'username': self.username,
'delete_my_post': 1,
'delete_my_thread' : 0,
'is_vipdel': 0,
# 'pid': re.findall('pid=(\d+)&', reply['reply_url'])[0],
'pid': pid,
'is_finf': 'false'
}
url = 'https://tieba.baidu.com/f/commit/post/delete'
log('-->%s' % url)
log('delete reply')
_ = self.r.post(url, data=data, headers=self.headers)
log(_.status_code)
return self.error_check(_.text)
def login(self):
print('这次不用输入前缀`Cookie:`了,直接复制后面的key-value对')
cookie = input('give me cookies[xxx=xxx; xxx=xxx]:')
q = {k:v for k,v in re.findall(r'([^=]*)=([^;]*);{0,1}\s{0,1}', cookie)}
self.r.cookies = cookiejar_from_dict(q)
url = 'https://tieba.baidu.com'
log('-->%s' % url)
_ = self.r.get(url)
self.username = re.findall('"user_name": "(.*?)",', _.text)[0]
log('get username: %s' % (self.username))
url = 'https://tieba.baidu.com/home/profile?un=%s' % self.username
log('-->%s' % url)
_ = self.r.get(url)
self.user_id = re.findall('user_id":(\d+)', _.text)[0]
def start(self, input_file=True):
if input_file:
tie_json = input('Do you have tie json, give me file, if not, just enter:')
if tie_json != '':
tie_list = json.load(open(tie_json, 'r'))
else:
tie_list = self.get_tie()
open('clean_tieba_tie_list.json', 'w').write(json.dumps(tie_list, ensure_ascii=False, indent=4))
else:
if os.path.exists('clean_tieba_tie_fail.json'):
log('load tie failed from file')
tie_list = json.load(open('clean_tieba_tie_fail.json', 'r'))
else:
tie_list = self.get_tie()
open('clean_tieba_tie_list.json', 'w').write(json.dumps(tie_list, ensure_ascii=False, indent=4))
if input_file:
reply_json = input('Do you have reply json, give me file, if not, just enter:')
if reply_json != '':
reply_list = json.load(open(reply_json, 'r'))
else:
reply_list = self.get_reply()
open('clean_tieba_tie_reply.json', 'w').write(json.dumps(reply_list, ensure_ascii=False, indent=4))
else:
if os.path.exists('clean_tieba_reply_fail.json'):
log('load reply failed from file')
reply_list = json.load(open('clean_tieba_reply_fail.json', 'r'))
else:
reply_list = self.get_reply()
open('clean_tieba_reply_list.json', 'w').write(json.dumps(reply_list, ensure_ascii=False, indent=4))
tie_count = len(tie_list)
tie_fail = []
reply_count = len(reply_list)
reply_fail = []
if tie_count == 0 and reply_count == 0:
log('done')
exit()
tie_is_max = False
for i in range(tie_count):
log('tie: %d/%d' % (i + 1, tie_count))
if tie_is_max:
tie_fail.append(tie_list[i])
continue
status = self.del_tie(tie_list[i])
if status == 'exit':
print('达到每日上限,等待下一轮')
tie_is_max = True
tie_fail.append(tie_list[i])
elif status == False:
tie_fail.append(tie_list[i])
else:
pass
open('clean_tieba_tie_fail.json', 'w').write(json.dumps(tie_fail, ensure_ascii=False, indent=4))
reply_is_max = False
for i in range(reply_count):
log('reply: %d/%d' % (i + 1, reply_count))
if reply_is_max:
reply_fail.append(reply_list[i])
continue
status = self.del_reply(reply_list[i])
if status == 'exit':
print('达到每日上限,等待下一轮')
reply_is_max = True
reply_fail.append(reply_list[i])
elif status == False:
reply_fail.append(reply_list[i])
else:
pass
open('clean_tieba_reply_fail.json', 'w').write(json.dumps(reply_fail, ensure_ascii=False, indent=4))
if __name__ == '__main__':
tieba = Tieba()
parser = OptionParser()
parser.add_option('-m', '--match',
help="give me re format, if match in reply, I will delete")
(options, args) = parser.parse_args()
if options.match is not None:
tieba.match = match
log('match had set: (%s)' % tieba.match)
else:
log('match had set: (%s)' % tieba.match)
tieba.login()
tieba.start()
while(1):
sleep_hours = 4
log('will sleep %d hours' % (sleep_hours))
for i in range(0, sleep_hours, 1):
log('start after %d hours' % (sleep_hours - i))
time.sleep(60 * 60)
tieba.start(False)