【轉載】百度模擬登陸,自看

  1 # -*- coding: utf-8 -*-
  2 import urllib
  3 import urllib2
  4 import cookielib
  5 import re
  6 import hashlib
  7 import json
  8 import threading
  9 import platform
 10 import os
 11 
 12 
 13 def _setup_cookie(my_cookie):
 14     cookie = cookielib.CookieJar()
 15     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
 16     urllib2.install_opener(opener)
 17     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'),
 18                          ('Cookie', my_cookie), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')]
 19 
 20 
 21 def _fetch_like_tieba_list():
 22     print u'獲取喜歡的貼吧ing...' if system_env else '獲取喜歡的貼吧ing...'
 23     page_count = 1
 24     find_like_tieba = []
 25     while True:
 26         like_tieba_url = 'http://tieba.baidu.com/f/like/mylike?&pn=%d' % page_count
 27         req = urllib2.Request(like_tieba_url)
 28         resp = urllib2.urlopen(req).read()
 29         resp = resp.decode('gbk').encode('utf8')
 30         re_like_tieba = '<a href="\/f\?kw=.*?" title="(.*?)">.+?<\/a>'
 31         temp_like_tieba = re.findall(re_like_tieba, resp)
 32         if not temp_like_tieba:
 33             break
 34         if not find_like_tieba:
 35             find_like_tieba = temp_like_tieba
 36         else:
 37             find_like_tieba += temp_like_tieba
 38         page_count += 1
 39 
 40     return find_like_tieba
 41 
 42 
 43 def _fetch_tieba_info(tieba):
 44     tieba_wap_url = "http://tieba.baidu.com/mo/m?kw=" + tieba
 45     wap_resp = urllib2.urlopen(tieba_wap_url).read()
 46 
 47     if not wap_resp:
 48         return
 49     re_already_sign = '<td style="text-align:right;"><span[ ]>(.*?)<\/span><\/td><\/tr>'
 50     already_sign = re.findall(re_already_sign, wap_resp)
 51 
 52     re_fid = '<input type="hidden" name="fid" value="(.+?)"\/>'
 53     _fid = re.findall(re_fid, wap_resp)
 54     fid = _fid and _fid[0] or None
 55 
 56     re_tbs = '<input type="hidden" name="tbs" value="(.+?)"\/>'
 57     _tbs = re.findall(re_tbs, wap_resp)
 58 
 59     tbs = _tbs and _tbs[0] or None
 60     return already_sign, fid, tbs
 61 
 62 
 63 def _decode_uri_post(postData):
 64     SIGN_KEY = "tiebaclient!!!"
 65     s = ""
 66     keys = postData.keys()
 67     keys.sort()
 68     for i in keys:
 69         s += i + '=' + postData[i]
 70     sign = hashlib.md5(s + SIGN_KEY).hexdigest().upper()
 71     postData.update({'sign': str(sign)})
 72     return postData
 73 
 74 
 75 def _make_sign_request(tieba, fid, tbs, BDUSS):
 76     sign_url = 'http://c.tieba.baidu.com/c/c/forum/sign'
 77     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":
 78                     "4", "_client_version": "1.2.1.17", "_phone_imei": "540b43b59d21b7a4824e1fd31b08e9a6", "fid": fid, "kw": tieba, "net_type": "3", 'tbs': tbs}
 79 
 80     sign_request = _decode_uri_post(sign_request)
 81     sign_request = urllib.urlencode(sign_request)
 82 
 83     sign_request = urllib2.Request(sign_url, sign_request)
 84     sign_request.add_header(
 85         "Content-Type", "application/x-www-form-urlencoded")
 86     return sign_request
 87 
 88 
 89 def _handle_response(sign_resp):
 90     sign_resp = json.load(sign_resp)
 91     error_code = sign_resp['error_code']
 92     sign_bonus_point = 0
 93     try:
 94         # Don't know why but sometimes this will trigger key error.
 95         sign_bonus_point = int(sign_resp['user_info']['sign_bonus_point'])
 96     except KeyError:
 97         pass
 98     if error_code == '0':
 99         print u"簽到成功,經驗+%d" % sign_bonus_point if system_env else "簽到成功,經驗+%d" % sign_bonus_point
100     else:
101         error_msg = sign_resp['error_msg']
102         if error_msg == u'親,你以前已經簽過了':
103             print u'以前已簽到' if system_env else '以前已簽到'
104         else:
105             print u'簽到失敗' if system_env else '簽到失敗'
106             print "Error:" + unicode(error_code) + " " + unicode(error_msg)
107 
108 
109 def _sign_tieba(tieba, BDUSS):
110     already_sign, fid, tbs = _fetch_tieba_info(tieba)
111     if not already_sign:
112         print tieba.decode('utf-8') + u'......正在嘗試簽到' if system_env else tieba + '......正在嘗試簽到'
113     else:
114         if already_sign[0] == "已簽到":
115             print tieba.decode('utf-8') + u"......以前已簽到" if system_env else tieba + "......以前已簽到"
116             return
117 
118     if not fid or not tbs:
119         print u"簽到失敗,緣由未知" if system_env else "簽到失敗,緣由未知"
120         return
121 
122     sign_request = _make_sign_request(tieba, fid, tbs, BDUSS)
123     sign_resp = urllib2.urlopen(sign_request, timeout=5)
124     _handle_response(sign_resp)
125 
126 
127 def sign(my_cookie, BDUSS):
128     _setup_cookie(my_cookie)
129     _like_tieba_list = _fetch_like_tieba_list()
130     if len(_like_tieba_list) == 0:
131         print u"獲取喜歡的貼吧失敗,請檢查Cookie和BDUSS是否正確" if system_env else "獲取喜歡的貼吧失敗,請檢查Cookie和BDUSS是否正確"
132         return
133     thread_list = []
134     for tieba in _like_tieba_list:
135         t = threading.Thread(target=_sign_tieba, args=(tieba, BDUSS))
136         thread_list.append(t)
137         t.start()
138 
139     for t in thread_list:
140         t.join(2)
141 
142 
143 def main():
144     my_cookie = "BAIDUID=28DED2C1D55CFDE54F8E2CD3A15BF3F1:FG=1; BAIDU_WISE_UID=wapp_1454638333587_420; SSUDBTSP=1456819983; SSUDB=g4aDhWZGlMOG5XYTRNS0N0c3Z0WlFQM2gxTDRvYnptaW5kSWYteW1Rb1AzUHhXQVFBQUFBJCQAAAAAAAAAAAEAAACiF3GFsKLDxbLiytTVy7rFMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9P1VYPT9VWQ3; BIND_BDUSS=g4aDhWZGlMOG5XYTRNS0N0c3Z0WlFQM2gxTDRvYnptaW5kSWYteW1Rb1AzUHhXQVFBQUFBJCQAAAAAAAAAAAEAAACiF3GFsKLDxbLiytTVy7rFMgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA9P1VYPT9VWQ3; Hm_lvt_294dbbdeb1fabbed8433533a3564560e=1456955951; Hm_lpvt_294dbbdeb1fabbed8433533a3564560e=1456955951; BAIDU_WISE_UID=wapp_1454638333587_420; HISTORY=84d77288446438a85b054f5d60b736dbaf2812b2463afda802dd40ed95d344bd00a0b8bde2c8; UBI=fi_PncwhpxZ%7ETaCBjG8iz7iygSLl0B%7EPtDhYP8RxDZtr%7EE3BiJKli3vCqylMcfeqJGf0LgR6%7ERNWtI1GZynwObUCz2S3HHOx4BdseMG55iqfDj5FgnjfRLyqWB8qMFTn7R1VAuJHlyUvXTCQVNJ4bc2avqCNuEmnqCz02XisGF6UJ3kro23cOBZRJHwETSteHr82SPBnWZYSzZ4oxuLUlyMqQomXxd7zQ0SO0X4; BDUSS=tGcDI1elJJYUdxZGEwMWlwdHVPZEh1c0xYc0luYkMzYmZweng0V04zVHZIRDlYQVFBQUFBJCQAAAAAAAAAAAEAAACHJXIzsKLDxTEyMzE0MzUzMjQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO-PF1fvjxdXVm; PTOKEN=null; CUID=C3C7CD9F366B27250D75EF6DCF08C3C9|217935320573468; cuid=C3C7CD9F366B27250D75EF6DCF08C3C9"
145     BDUSS = "tGcDI1elJJYUdxZGEwMWlwdHVPZEh1c0xYc0luYkMzYmZweng0V04zVHZIRDlYQVFBQUFBJCQAAAAAAAAAAAEAAACHJXIzsKLDxTEyMzE0MzUzMjQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAO-PF1fvjxdXVm"
146     sign(my_cookie, BDUSS)
147 
148 if __name__ == "__main__":
149     system_env = True if platform.system()=='Windows' else False
150     main()
151     os.system("date /T >> tieba_log.log") if system_env else os.system("date >> tieba_log.log")
相關文章
相關標籤/搜索