-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtieba.py
More file actions
194 lines (176 loc) · 5.77 KB
/
tieba.py
File metadata and controls
194 lines (176 loc) · 5.77 KB
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
# -*- coding:utf-8 -*-
import os
import requests
import hashlib
import time
import copy
import logging
import random
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# API_URL
LIKIE_URL = "http://c.tieba.baidu.com/c/f/forum/like"
TBS_URL = "http://tieba.baidu.com/dc/common/tbs"
SIGN_URL = "http://c.tieba.baidu.com/c/c/forum/sign"
HEADERS = {
'Host': 'tieba.baidu.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36',
}
SIGN_DATA = {
'_client_type': '2',
'_client_version': '9.7.8.0',
'_phone_imei': '000000000000000',
'model': 'MI+5',
"net_type": "1",
}
# VARIABLE NAME
COOKIE = "Cookie"
BDUSS = "BDUSS"
EQUAL = r'='
EMPTY_STR = r''
TBS = 'tbs'
PAGE_NO = 'page_no'
ONE = '1'
TIMESTAMP = "timestamp"
DATA = 'data'
FID = 'fid'
SIGN_KEY = 'tiebaclient!!!'
UTF8 = "utf-8"
SIGN = "sign"
KW = "kw"
s = requests.Session()
def get_tbs(bduss):
logger.info("开始获取tbs")
headers = copy.copy(HEADERS)
headers.update({COOKIE: EMPTY_STR.join([BDUSS, EQUAL, bduss])})
try:
tbs = s.get(url=TBS_URL, headers=headers, timeout=5).json()[TBS]
except Exception as e:
logger.error("获取tbs出错" + e)
logger.info("开始重新获取tbs")
tbs = s.get(url=TBS_URL, headers=headers, timeout=5).json()[TBS]
logger.info("获取tbs完成")
return tbs
def get_favorite(bduss):
logger.info("开始获取关注的贴吧")
# 客户端关注的贴吧
returnData = {}
i = 1
data = {
'BDUSS': bduss,
'_client_type': '2',
'_client_id': 'wappc_1534235498291_488',
'_client_version': '9.7.8.0',
'_phone_imei': '000000000000000',
'from': '1008621y',
'page_no': '1',
'page_size': '200',
'model': 'MI+5',
'net_type': '1',
'timestamp': str(int(time.time())),
'vcode_tag': '11',
}
data = encodeData(data)
try:
res = s.post(url=LIKIE_URL, data=data, timeout=5).json()
except Exception as e:
logger.error("获取关注的贴吧出错" + e)
return []
returnData = res
if 'forum_list' not in returnData:
returnData['forum_list'] = []
if res['forum_list'] == []:
return {'gconforum': [], 'non-gconforum': []}
if 'non-gconforum' not in returnData['forum_list']:
returnData['forum_list']['non-gconforum'] = []
if 'gconforum' not in returnData['forum_list']:
returnData['forum_list']['gconforum'] = []
while 'has_more' in res and res['has_more'] == '1':
i = i + 1
data = {
'BDUSS': bduss,
'_client_type': '2',
'_client_id': 'wappc_1534235498291_488',
'_client_version': '9.7.8.0',
'_phone_imei': '000000000000000',
'from': '1008621y',
'page_no': str(i),
'page_size': '200',
'model': 'MI+5',
'net_type': '1',
'timestamp': str(int(time.time())),
'vcode_tag': '11',
}
data = encodeData(data)
try:
res = s.post(url=LIKIE_URL, data=data, timeout=5).json()
except Exception as e:
logger.error("获取关注的贴吧出错" + e)
continue
if 'forum_list' not in res:
continue
if 'non-gconforum' in res['forum_list']:
returnData['forum_list']['non-gconforum'].append(res['forum_list']['non-gconforum'])
if 'gconforum' in res['forum_list']:
returnData['forum_list']['gconforum'].append(res['forum_list']['gconforum'])
t = []
for i in returnData['forum_list']['non-gconforum']:
if isinstance(i, list):
for j in i:
if isinstance(j, list):
for k in j:
t.append(k)
else:
t.append(j)
else:
t.append(i)
for i in returnData['forum_list']['gconforum']:
if isinstance(i, list):
for j in i:
if isinstance(j, list):
for k in j:
t.append(k)
else:
t.append(j)
else:
t.append(i)
logger.info("共获取到【"+str(len(t))+"】个关注的贴吧")
return t
def encodeData(data):
s = EMPTY_STR
keys = data.keys()
for i in sorted(keys):
s += i + EQUAL + str(data[i])
sign = hashlib.md5((s + SIGN_KEY).encode(UTF8)).hexdigest().upper()
data.update({SIGN: str(sign)})
return data
def client_sign(bduss, tbs, fid, kw, idx, count):
# 客户端签到
print("【" + kw +"】吧,开始签到("+str(idx+1)+"/"+str(count)+")")
data = copy.copy(SIGN_DATA)
data.update({BDUSS: bduss, FID: fid, KW: kw, TBS: tbs, TIMESTAMP: str(int(time.time()))})
data = encodeData(data)
res = s.post(url=SIGN_URL, data=data, timeout=5).json()
# print(res)
if res['error_code'] == '0':
print("签到成功,你是第"+res['user_info']['user_sign_rank']+"个签到的")
if res['error_code'] == '160002':
print(res['error_msg'])
return res
def main():
b = os.getenv('BDUSS').split('#')
for n, i in enumerate(b):
if len(i) <= 0:
logger.info("未检测到BDUSS")
continue
logger.info("第" + str(n+1) + "个用户开始签到" )
tbs = get_tbs(i)
favorites = get_favorite(i)
count = len(favorites)
for idx,j in enumerate(favorites):
time.sleep(random.randint(1,5))
client_sign(i, tbs, j["id"], j["name"],idx,count)
logger.info("第" + str(n+1) + "个用户签到完成")
logger.info("所有用户签到完成")
if __name__ == '__main__':
main()