def init_wx(self): print '初始化微信' url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=%s&skey=%s&r=%s'%(self.pass_ticket,self.skey,self.get_time()) data = { "BaseRequest":{ "Uin":self.uin, "Sid":self.sid, "Skey":self.skey, "DeviceID":self.deviceid } } request = urllib2.Request(url = url, data = json.dumps(data)) request.add_header('ContentType', 'application/json; charset=UTF-8') response = urllib2.urlopen(request) data = response.read() json_data = json.loads(data) self.sync = json_data['SyncKey'] self.fname = json_data['User']['UserName'] MPSubscribeMsgList = json_data['MPSubscribeMsgList'] for MPSubscribeMsg in MPSubscribeMsgList: MPArticleList = MPSubscribeMsg['MPArticleList'] for MPArticle in MPArticleList: t = Message_crawl(MPArticle['Url']) t.start()
def get_sync(self, selector): # print '尝试处理新消息' url = 'https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid='+self.sid+'&skey='+self.skey+'&lang=zh_CN&pass_ticket='+self.pass_ticket data = { "BaseRequest" : { "Uin":self.uin, "Sid":self.sid }, "SyncKey" : self.sync, "rr" : int(time.time()) } request = urllib2.Request(url = url, data = json.dumps(data)) request.add_header('ContentType', 'application/json; charset=UTF-8') response = urllib2.urlopen(request) data = response.read() json_data = json.loads(data) # print json_data self.sync = json_data['SyncKey'] # if selector == '6': new_messages = json_data['AddMsgList'] for new_message in new_messages: from_user = new_message['FromUserName'] url = new_message['Url'] if from_user in self.pub_accounts: if url: print '收到来自',self.pub_accounts[from_user],'的文章:',url t = Message_crawl(url) t.start() con = new_message['Content'] t_urls = con.split('&scene=0#rd')[2:-1] for tu in t_urls: u = tu.split('[CDATA[')[-1] print '收到来自',self.pub_accounts[from_user],'的文章:',u t = Message_crawl(u) t.start() return