Skip to content

Commit

Permalink
Add user search & remarkName setting
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecodersh committed Jul 29, 2016
1 parent 4eeeb97 commit 637d8f8
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 13 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ itchat.auto_login(enableCmdQR = 2)
itchat.auto_login(hotReload = True)
```

### 用户搜索

使用`get_friends`方法可以搜索用户,有四种搜索方式:
1. 仅获取自己的用户信息
2. 获取特定`UserName`的用户信息
3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户
4. 获取备注、微信号、昵称分别等于相应键值的用户

其中三、四项可以一同使用,下面是示例程序:

```python
# 获取自己的用户信息,返回自己的属性字典
itchat.get_friends()
# 获取特定UserName的用户信息
itchat.get_friends(userName = '@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.get_friends(name = 'littlecodersh')
# 获取分别对应相应键值的用户
itchat.get_friends(wechatAccount = 'littlecodersh')
# 三、四项功能可以一同使用
itchat.get_friends(name = 'LittleCoder机器人', wechatAccount = 'littlecodersh')
```

### 附件的下载与发送

itchat的附件下载方法存储在msg的Text键中。
Expand Down
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ By using the following command, you may reload the program without re-scan QRCod
itchat.auto_login(hotReload = True)
*User search*

By using `get_friends`, you have four ways to search a user:

1. Get your own user information
2. Get user information through `UserName`
3. Get user information whose remark name or wechat account or nickname matches name key of the function
4. Get user information whose remark name, wechat account and nickname match what are given to the function

Way 3, 4 can be used together, the following is the demo program:

.. code:: python
# get your own user information
itchat.get_friends()
# get user information of specific username
itchat.get_friends(userName = '@abcdefg1234567')
# get user information of function 3
itchat.get_friends(name = 'littlecodersh')
# get user information of function 4
itchat.get_friends(wechatAccount = 'littlecodersh')
# combination of way 3, 4
itchat.get_friends(name = 'LittleCoder机器人', wechatAccount = 'littlecodersh')
*Download and send attachments*

The attachment download function of itchat is in Text key of msg
Expand Down
23 changes: 23 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ By using the following command, you may reload the program without re-scan QRCod
itchat.auto_login(hotReload = True)
```

### User search

By using `get_friends`, you have four ways to search a user:
1. Get your own user information
2. Get user information through `UserName`
3. Get user information whose remark name or wechat account or nickname matches name key of the function
4. Get user information whose remark name, wechat account and nickname match what are given to the function

Way 3, 4 can be used together, the following is the demo program:

```python
# get your own user information
itchat.get_friends()
# get user information of specific username
itchat.get_friends(userName = '@abcdefg1234567')
# get user information of function 3
itchat.get_friends(name = 'littlecodersh')
# get user information of function 4
itchat.get_friends(wechatAccount = 'littlecodersh')
# combination of way 3, 4
itchat.get_friends(name = 'LittleCoder机器人', wechatAccount = 'littlecodersh')
```

### Download and send attachments

The attachment download function of itchat is in Text key of msg
Expand Down
23 changes: 23 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ itchat.auto_login(enableCmdQR = 2)
itchat.auto_login(hotReload = True)
```

### 用户搜索

使用`get_friends`方法可以搜索用户,有四种搜索方式:
1. 仅获取自己的用户信息
2. 获取特定`UserName`的用户信息
3. 获取备注、微信号、昵称中的任何一项等于`name`键值的用户
4. 获取备注、微信号、昵称分别等于相应键值的用户

其中三、四项可以一同使用,下面是示例程序:

```python
# 获取自己的用户信息,返回自己的属性字典
itchat.get_friends()
# 获取特定UserName的用户信息
itchat.get_friends(userName = '@abcdefg1234567')
# 获取任何一项等于name键值的用户
itchat.get_friends(name = 'littlecodersh')
# 获取分别对应相应键值的用户
itchat.get_friends(wechatAccount = 'littlecodersh')
# 三、四项功能可以一同使用
itchat.get_friends(name = 'LittleCoder机器人', wechatAccount = 'littlecodersh')
```

### 附件的下载与发送

itchat的附件下载方法存储在msg的Text键中。
Expand Down
9 changes: 7 additions & 2 deletions itchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@ def get_chatrooms(update = False): return __client.get_chatrooms(update)
def show_mobile_login(): return __client.show_mobile_login()
def start_receiving(): return __client.start_receiving()
# <<<
# The following method are for reload without re-scan the QRCode
# The following methods are for reload without re-scan the QRCode >>>
def dump_login_status(fileDir = 'itchat.pkl'): return __client.dump_login_status(fileDir)
def load_login_status(fileDir = 'itchat.pkl'): return __client.load_login_status(fileDir)
# <<<
# The following methods are for member dealing >>>
def get_friends(name = None, userName = None, remarkName = None, nickName = None, wechatAccount = None):
return __client.storageClass.get_friends(name, userName, remarkName, nickName, wechatAccount)
def set_alias(userName, alias): return __client.set_alias(userName, alias)
def add_friend(status, userName, ticket, recommendInfo = {}): return __client.add_friend(status, userName, ticket, recommendInfo)
# <<<
# if toUserName is set to None, msg will be sent to yourself
def send_msg(msg = 'Test Message', toUserName = None): return __client.send_msg(msg, toUserName)
def send_file(fileDir, toUserName): return __client.send_file(fileDir, toUserName)
def send_image(fileDir, toUserName): return __client.send_image(fileDir, toUserName)
def add_friend(status, userName, ticket, recommendInfo = {}): return __client.add_friend(status, userName, ticket, recommendInfo)
def create_chatroom(memberList, topic = ''): return __client.create_chatroom(memberList, topic)
def delete_member_from_chatroom(chatRoomUserName, memberList): return __client.delete_member_from_chatroom(chatRoomUserName, memberList)
def add_member_into_chatroom(chatRoomUserName, memberList): return __client.add_member_into_chatroom(chatRoomUserName, memberList)
Expand Down
10 changes: 10 additions & 0 deletions itchat/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ def send_image(self, fileDir, toUserName = None):
'Content-Type': 'application/json;charset=UTF-8', }
r = self.s.post(url, data = json.dumps(payloads, ensure_ascii = False).encode('utf8'), headers = headers)
return True
def set_alias(self, userName, alias):
url = '%s/webwxoplog?lang=%s&pass_ticket=%s'%(
self.loginInfo['url'], 'zh_CN', self.loginInfo['pass_ticket'])
data = {
'UserName' : userName,
'CmdId' : 2,
'RemarkName' : alias,
'BaseRequest' : self.loginInfo['BaseRequest'], }
j = self.s.post(url, json.dumps(data, ensure_ascii = False).encode('utf8')).json()
return j['BaseResponse']['Ret'] == 0
def add_friend(self, status, userName, ticket, recommendInfo = {}):
url = '%s/webwxverifyuser?r=%s&pass_ticket=%s'%(self.loginInfo['url'], int(time.time()), self.loginInfo['pass_ticket'])
payloads = {
Expand Down
41 changes: 30 additions & 11 deletions itchat/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os, time
from . import config
import os, time, copy

class Storage:
def __init__(self):
Expand Down Expand Up @@ -28,12 +27,32 @@ def loads(self, j):
self.groupDict.clear()
for k, v in j.get('groupDict', {}).items(): self.groupDict[k] = v
self.lastInputUserName = j.get('lastInputUserName', None)
def find_username(self, n):
r = []
for member in self.memberList:
if member['NickName'] == n: r.append(member['UserName'])
return r
def find_nickname(self, u):
r = []
for member in self.memberList:
if member['UserName'] == u: return member['NickName']
def get_friends(self, name = None, userName = None, remarkName = None, nickName = None,
wechatAccount = None):
if (name or userName or remarkName or nickName or wechatAccount) is None:
return copy.deepcopy(self.memberList[0]) # my own account
elif userName: # return the only userName match
for m in self.memberList:
if m['UserName'] == userName: return copy.deepcopy(m)
else:
matchDict = {
'RemarkName' : remarkName,
'NickName' : nickName,
'Alias' : wechatAccount, }
for k in ('RemarkName', 'NickName', 'Alias'):
if matchDict[k] is None: del matchDict[k]
if name: # select based on name
contract = []
for m in self.memberList:
if any([m.get(k) == name for k in ('RemarkName', 'NickName', 'Alias')]):
contract.append(m)
else:
contract = self.memberList[:]
if matchDict: # select again based on matchDict
friendList = []
for m in contract:
if all([m.get(k) == v for k, v in matchDict.items()]):
friendList.append(m)
return copy.deepcopy(friendList)
else:
return copy.deepcopy(contract)

0 comments on commit 637d8f8

Please sign in to comment.