This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathbaidu-tieba-auto-sign.py
151 lines (122 loc) · 5.48 KB
/
baidu-tieba-auto-sign.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
# -*- coding: utf-8 -*-
import urllib
import urllib2
import cookielib
import re
import hashlib
import json
import threading
import platform
import os
def _setup_cookie(my_cookie):
cookie = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
opener.addheaders = [('User-agent', 'Mozilla/5.0 (SymbianOS/9.3; Series60/3.2 NokiaE72-1/021.021; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/525 (KHTML, like Gecko) Version/3.0 BrowserNG/7.1.16352'),
('Cookie', my_cookie), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
def _fetch_like_tieba_list():
print u'获取喜欢的贴吧ing...' if system_env else '获取喜欢的贴吧ing...'
page_count = 1
find_like_tieba = []
while True:
like_tieba_url = 'http://tieba.baidu.com/f/like/mylike?&pn=%d' % page_count
req = urllib2.Request(like_tieba_url)
resp = urllib2.urlopen(req).read()
resp = resp.decode('gbk').encode('utf8')
re_like_tieba = '<a href="\/f\?kw=.*?" title="(.*?)">.+?<\/a>'
temp_like_tieba = re.findall(re_like_tieba, resp)
if not temp_like_tieba:
break
if not find_like_tieba:
find_like_tieba = temp_like_tieba
else:
find_like_tieba += temp_like_tieba
page_count += 1
return find_like_tieba
def _fetch_tieba_info(tieba):
tieba_wap_url = "http://tieba.baidu.com/mo/m?kw=" + tieba
wap_resp = urllib2.urlopen(tieba_wap_url).read()
if not wap_resp:
return
re_already_sign = '<td style="text-align:right;"><span[ ]>(.*?)<\/span><\/td><\/tr>'
already_sign = re.findall(re_already_sign, wap_resp)
re_fid = '<input type="hidden" name="fid" value="(.+?)"\/>'
_fid = re.findall(re_fid, wap_resp)
fid = _fid and _fid[0] or None
re_tbs = '<input type="hidden" name="tbs" value="(.+?)"\/>'
_tbs = re.findall(re_tbs, wap_resp)
tbs = _tbs and _tbs[0] or None
return already_sign, fid, tbs
def _decode_uri_post(postData):
SIGN_KEY = "tiebaclient!!!"
s = ""
keys = postData.keys()
keys.sort()
for i in keys:
s += i + '=' + postData[i]
sign = hashlib.md5(s + SIGN_KEY).hexdigest().upper()
postData.update({'sign': str(sign)})
return postData
def _make_sign_request(tieba, fid, tbs, BDUSS):
sign_url = 'http://c.tieba.baidu.com/c/c/forum/sign'
sign_request = {"BDUSS": BDUSS, "_client_id": "03-00-DA-59-05-00-72-96-06-00-01-00-04-00-4C-43-01-00-34-F4-02-00-BC-25-09-00-4E-36", "_client_type":
"4", "_client_version": "1.2.1.17", "_phone_imei": "540b43b59d21b7a4824e1fd31b08e9a6", "fid": fid, "kw": tieba, "net_type": "3", 'tbs': tbs}
sign_request = _decode_uri_post(sign_request)
sign_request = urllib.urlencode(sign_request)
sign_request = urllib2.Request(sign_url, sign_request)
sign_request.add_header(
"Content-Type", "application/x-www-form-urlencoded")
return sign_request
def _handle_response(sign_resp):
sign_resp = json.load(sign_resp)
error_code = sign_resp['error_code']
sign_bonus_point = 0
try:
# Don't know why but sometimes this will trigger key error.
sign_bonus_point = int(sign_resp['user_info']['sign_bonus_point'])
except KeyError:
pass
if error_code == '0':
print u"签到成功,经验+%d" % sign_bonus_point if system_env else "签到成功,经验+%d" % sign_bonus_point
else:
error_msg = sign_resp['error_msg']
if error_msg == u'亲,你之前已经签过了':
print u'之前已签到' if system_env else '之前已签到'
else:
print u'签到失败' if system_env else '签到失败'
print "Error:" + unicode(error_code) + " " + unicode(error_msg)
def _sign_tieba(tieba, BDUSS):
already_sign, fid, tbs = _fetch_tieba_info(tieba)
if not already_sign:
print tieba.decode('utf-8') + u'......正在尝试签到' if system_env else tieba + '......正在尝试签到'
else:
if already_sign[0] == "已签到":
print tieba.decode('utf-8') + u"......之前已签到" if system_env else tieba + "......之前已签到"
return
if not fid or not tbs:
print u"签到失败,原因未知" if system_env else "签到失败,原因未知"
return
sign_request = _make_sign_request(tieba, fid, tbs, BDUSS)
sign_resp = urllib2.urlopen(sign_request, timeout=5)
_handle_response(sign_resp)
def sign(my_cookie, BDUSS):
_setup_cookie(my_cookie)
_like_tieba_list = _fetch_like_tieba_list()
if len(_like_tieba_list) == 0:
print u"获取喜欢的贴吧失败,请检查Cookie和BDUSS是否正确" if system_env else "获取喜欢的贴吧失败,请检查Cookie和BDUSS是否正确"
return
thread_list = []
for tieba in _like_tieba_list:
t = threading.Thread(target=_sign_tieba, args=(tieba, BDUSS))
thread_list.append(t)
t.start()
for t in thread_list:
t.join(2)
def main():
my_cookie = "填入你的Cookie"
BDUSS = "填入你的BDUSS"
sign(my_cookie, BDUSS)
if __name__ == "__main__":
system_env = True if platform.system()=='Windows' else False
main()
os.system("date /T >> tieba_log.log") if system_env else os.system("date >> tieba_log.log")