python如何查看微信好友是否删除自己

本文实例为大家分享了python微信好友删除的具体代码,供大家参考,具体内容如下

#weixin.py
#coding:utf-8
# !/usr/bin/env python
# coding=utf-8
#通过该程序可以发现被删除的好友
import os
import urllib, urllib2
import re
import cookielib
import time
import xml.dom.minidom
import json
import sys
import math
debug = false
max_group_num = 35 # 每组人数
qrimagepath = os.getcwd() + ‘/qrcode.jpg’
tip = 0
uuid = ”
base_uri = ”
redirect_uri = ”
skey = ”
wxsid = ”
wxuin = ”
pass_ticket = ”
deviceid = ‘e000000000000000’
baserequest = {}
contactlist = []
my = []
def getuuid():
global uuid
url = ‘https://login.weixin.qq.com/jslogin’
params = {
‘appid’: ‘wx782c26e4c19acffb’,
‘fun’: ‘new’,
‘lang’: ‘zh_cn’,
‘_’: int(time.time()),
}
request = urllib2.request(url=url, data=urllib.urlencode(params))
response = urllib2.urlopen(request)
data = response.read()
# print data
# window.qrlogin.code = 200; window.qrlogin.uuid = “ozwt_bffrg==”;
regx = r’window.qrlogin.code = (\d+); window.qrlogin.uuid = “(\s+?)”‘
pm = re.search(regx, data)
code = pm.group(1)
uuid = pm.group(2)
if code == ‘200’:
return true
return false
def showqrimage():
global tip
url = ‘https://login.weixin.qq.com/qrcode/’ + uuid
params = {
‘t’: ‘webwx’,
‘_’: int(time.time()),
}
request = urllib2.request(url=url, data=urllib.urlencode(params))
response = urllib2.urlopen(request)
tip = 1
f = open(qrimagepath, ‘wb’)
f.write(response.read())
f.close()
if sys.platform.find(‘darwin’) >= 0:
os.system(‘open %s’ % qrimagepath)
elif sys.platform.find(‘linux’) >= 0:
os.system(‘xdg-open %s’ % qrimagepath)
else:
os.system(‘call %s’ % qrimagepath)
print ‘请使用微信扫描二维码以登录’
def waitforlogin():
global tip, base_uri, redirect_uri
url = ‘https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uu % (tip, uuid, int(time.time()))
request = urllib2.request(url=url)
response = urllib2.urlopen(request)
data = response.read()
# print data
# window.code=500;
regx = r’window.code=(\d+);’
pm = re.search(regx, data)
code = pm.group(1)
if code == ‘201’: # 已扫描
print ‘成功扫描,请在手机上点击确认以登录’
tip = 0
elif code == ‘200’: # 已登录
print ‘正在登录…’
regx = r’window.redirect_uri=”(\s+?)”;’
pm = re.search(regx, data)
redirect_uri = pm.group(1) + ‘&fun=new’
base_uri = redirect_uri[:redirect_uri.rfind(‘/’)]
elif code == ‘408’: # 超时
pass
# elif code == ‘400’ or code == ‘500’:
return code
def login():
global skey, wxsid, wxuin, pass_ticket, baserequest
request = urllib2.request(url=redirect_uri)
response = urllib2.urlopen(request)
data = response.read()
# print data
”’

0
ok
xxx
xxx
xxx
xxx
1

”’
doc = xml.dom.minidom.parsestring(data)
root = doc.documentelement
for node in root.childnodes:
if node.nodename == ‘skey’:
skey = node.childnodes[0].data
elif node.nodename == ‘wxsid’:
wxsid = node.childnodes[0].data
elif node.nodename == ‘wxuin’:
wxuin = node.childnodes[0].data
elif node.nodename == ‘pass_ticket’:
pass_ticket = node.childnodes[0].data
# print ‘skey: %s, wxsid: %s, wxuin: %s, pass_ticket: %s’ % (skey, wxsid, wxuin, pass_ticket)
if skey == ” or wxsid == ” or wxuin == ” or pass_ticket == ”:
return false
baserequest = {
‘uin’: int(wxuin),
‘sid’: wxsid,
‘skey’: skey,
‘deviceid’: deviceid,
}
return true
def webwxinit():
url = base_uri + ‘/webwxinit?pass_ticket=%s&skey=%s&r=%s’ % (pass_ticket, skey, int(time.time()))
params = {
‘baserequest’: baserequest
}
request = urllib2.request(url=url, data=json.dumps(params))
request.add_header(‘contenttype’, ‘application/json; charset=utf-8’)
response = urllib2.urlopen(request)
data = response.read()
if debug == true:
f = open(os.getcwd() + ‘/webwxinit.json’, ‘wb’)
f.write(data)
f.close()
# print data
global contactlist, my
dic = json.loads(data)
contactlist = dic[‘contactlist’]
my = dic[‘user’]
errmsg = dic[‘baseresponse’][‘errmsg’]
if len(errmsg) > 0:
print errmsg
ret = dic[‘baseresponse’][‘ret’]
if ret != 0:
return false
return true
def webwxgetcontact():
url = base_uri + ‘/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s’ % (pass_ticket, skey, int(time.time()))
request = urllib2.request(url=url)
request.add_header(‘contenttype’, ‘application/json; charset=utf-8’)
response = urllib2.urlopen(request)
data = response.read()
if debug == true:
f = open(os.getcwd() + ‘/webwxgetcontact.json’, ‘wb’)
f.write(data)
f.close()
# print data
dic = json.loads(data)
memberlist = dic[‘memberlist’]
# 倒序遍历,不然删除的时候出问题..
specialusers = [‘newsapp’, ‘fmessage’, ‘filehelper’, ‘weibo’, ‘qqmail’, ‘fmessage’, ‘tmessage’, ‘qmessage’,
‘qqsync’, ‘floatbottle’, ‘lbsapp’, ‘shakeapp’, ‘medianote’, ‘qqfriend’, ‘readerapp’, ‘blogapp’,
‘facebookapp’, ‘masssendapp’, ‘meishiapp’, ‘feedsapp’, ‘voip’, ‘blogappweixin’, ‘weixin’,
‘brandsessionholder’, ‘weixinreminder’, ‘wxid_novlwrv3lqwv11’, ‘gh_22b87fa7cb3c’,
‘officialaccounts’, ‘notification_messages’, ‘wxid_novlwrv3lqwv11’, ‘gh_22b87fa7cb3c’, ‘wxitil’,
‘userexperience_alarm’, ‘notification_messages’]
for i in xrange(len(memberlist) – 1, -1, -1):
member = memberlist[i]
if member[‘verifyflag’] & 8 != 0: # 公众号/服务号
memberlist.remove(member)
elif member[‘username’] in specialusers: # 特殊账号
memberlist.remove(member)
elif member[‘username’].find(‘@@’) != -1: # 群聊
memberlist.remove(member)
elif member[‘username’] == my[‘username’]: # 自己
memberlist.remove(member)
return memberlist
def createchatroom(usernames):
memberlist = []
for username in usernames:
memberlist.append({‘username’: username})
url = base_uri + ‘/webwxcreatechatroom?pass_ticket=%s&r=%s’ % (pass_ticket, int(time.time()))
params = {
‘baserequest’: baserequest,
‘membercount’: len(memberlist),
‘memberlist’: memberlist,
‘topic’: ”,
}
request = urllib2.request(url=url, data=json.dumps(params))
request.add_header(‘contenttype’, ‘application/json; charset=utf-8’)
response = urllib2.urlopen(request)
data = response.read()
# print data
dic = json.loads(data)
chatroomname = dic[‘chatroomname’]
memberlist = dic[‘memberlist’]
deletedlist = []
for member in memberlist:
if member[‘memberstatus’] == 4: # 被对方删除了
deletedlist.append(member[‘username’])
errmsg = dic[‘baseresponse’][‘errmsg’]
if len(errmsg) > 0:
print errmsg
return (chatroomname, deletedlist)
def deletemember(chatroomname, usernames):
url = base_uri + ‘/webwxupdatechatroom?fun=delmember&pass_ticket=%s’ % (pass_ticket)
params = {
‘baserequest’: baserequest,
‘chatroomname’: chatroomname,
‘delmemberlist’: ‘,’.join(usernames),
}
request = urllib2.request(url=url, data=json.dumps(params))
request.add_header(‘contenttype’, ‘application/json; charset=utf-8’)
response = urllib2.urlopen(request)
data = response.read()
# print data
dic = json.loads(data)
errmsg = dic[‘baseresponse’][‘errmsg’]
if len(errmsg) > 0:
print errmsg
ret = dic[‘baseresponse’][‘ret’]
if ret != 0:
return false
return true
def addmember(chatroomname, usernames):
url = base_uri + ‘/webwxupdatechatroom?fun=addmember&pass_ticket=%s’ % (pass_ticket)
params = {
‘baserequest’: baserequest,
‘chatroomname’: chatroomname,
‘addmemberlist’: ‘,’.join(usernames),
}
request = urllib2.request(url=url, data=json.dumps(params))
request.add_header(‘contenttype’, ‘application/json; charset=utf-8’)
response = urllib2.urlopen(request)
data = response.read()
# print data
dic = json.loads(data)
memberlist = dic[‘memberlist’]
deletedlist = []
for member in memberlist:
if member[‘memberstatus’] == 4: # 被对方删除了
deletedlist.append(member[‘username’])
errmsg = dic[‘baseresponse’][‘errmsg’]
if len(errmsg) > 0:
print errmsg
return deletedlist
def main():
opener = urllib2.build_opener(urllib2.httpcookieprocessor(cookielib.cookiejar()))
urllib2.install_opener(opener)
if getuuid() == false:
print ‘获取uuid失败’
return
showqrimage()
time.sleep(1)
while waitforlogin() != ‘200’:
pass
os.remove(qrimagepath)
if login() == false:
print ‘登录失败’
return
if webwxinit() == false:
print ‘初始化失败’
return
memberlist = webwxgetcontact()
membercount = len(memberlist)
print ‘通讯录共%s位好友’ % membercount
chatroomname = ”
result = []
for i in xrange(0, int(math.ceil(membercount / float(max_group_num)))):
usernames = []
nicknames = []
deletedlist = ”
for j in xrange(0, max_group_num):
if i * max_group_num + j >= membercount:
break
member = memberlist[i * max_group_num + j]
usernames.append(member[‘username’])
nicknames.append(member[‘nickname’].encode(‘utf-8’))
print ‘第%s组…’ % (i + 1)
print ‘, ‘.join(nicknames)
print ‘回车键继续…’
raw_input()
# 新建群组/添加成员
if chatroomname == ”:
(chatroomname, deletedlist) = createchatroom(usernames)
else:
deletedlist = addmember(chatroomname, usernames)
deletedcount = len(deletedlist)
if deletedcount > 0:
result += deletedlist
print ‘找到%s个被删好友’ % deletedcount
# raw_input()
# 删除成员
deletemember(chatroomname, usernames)
# todo 删除群组
resultnames = []
for member in memberlist:
if member[‘username’] in result:
nickname = member[‘nickname’]
if member[‘remarkname’] != ”:
nickname += ‘(%s)’ % member[‘remarkname’]
resultnames.append(nickname.encode(‘utf-8’))
print ‘———- 被删除的好友列表 ———-‘
print ‘\n’.join(resultnames)
print ‘———————————–‘
# windows下编码问题修复
class unicodestreamfilter:
def __init__(self, target):
self.target = target
self.encoding = ‘utf-8’
self.errors = ‘replace’
self.encode_to = self.target.encoding
def write(self, s):
if type(s) == str:
s = s.decode(‘utf-8’)
s = s.encode(self.encode_to, self.errors).decode(self.encode_to)
self.target.write(s)
if sys.stdout.encoding == ‘cp936’:
sys.stdout = unicodestreamfilter(sys.stdout)
if __name__ == ‘__main__’:
print ‘本程序的查询结果可能会引起一些心理上的不适,请小心使用…’
print ‘回车键继续…’
raw_input()
main()
print ‘回车键结束’
raw_input()

以上就是python如何查看微信好友是否删除自己 的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论