Again, doc first.
https://docs.python.org/3.6/library/os.html

This module provides a portable way of using operating system dependent functionality. If you just want to read or write a file see open(), if you want to manipulate paths, see the os.path module, and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module, and for high-level file and directory handling see the shutil module.

1
2
3
4
5
6
7
8
import os.path
import time

print('File :', __file__)
print('Access time :', time.ctime(os.path.getatime(__file__)))
print('Modified time:', time.ctime(os.path.getmtime(__file__)))
print('Change time :', time.ctime(os.path.getctime(__file__)))
print('Size :', os.path.getsize(__file__))

1
pip3 install itchat

You could find its Github page:
https://github.com/littlecodersh/itchat
This module has a complete and graceful API for WeChat.

1
2
3
4
import itchat

#login on Mac terminal with white background
itchat.auto_login(enableCmdQR=-2)

itchat API could be found on its core.py
https://github.com/littlecodersh/ItChat/blob/master/itchat/core.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
def search_friends(self, name=None, userName=None,
remarkName=None, nickName=None, wechatAccount=None):
return self.storageClass.search_friends(name, userName,
remarkName, nickName, wechatAccount)

def search_chatrooms(self, name=None, userName=None):
return self.storageClass.search_chatrooms(name, userName)

def send_msg(self, msg='Test Message', toUserName=None):
''' send plain text message
for options
- msg: should be unicode if there's non-ascii words in msg
- toUserName: 'UserName' key of friend dict
it is defined in components/messages.py
'''
raise NotImplementedError()

# or you could use

def send(self, msg, toUserName=None, mediaId=None):
''' wrapped function for all the sending functions
for options
- msg: message starts with different string indicates different type
- list of type string: ['@fil@', '@img@', '@msg@', '@vid@']
- they are for file, image, plain text, video
- if none of them matches, it will be sent like plain text
- toUserName: 'UserName' key of friend dict
- mediaId: if set, uploading will not be repeated
it is defined in components/messages.py
'''
raise NotImplementedError()

For example, if you know the name of one particular chatroom is ‘abc’, then you could:

1
2
3
some_chatroom_username = itchat.search_chatrooms(name='abc')[0]['UserName']
# then you enter the chatroom to say hi
itchat.send('Hello everyone', some_chatroom_username)

I know there are blank pages for previous blogs. I know.

1
import argparse

Docs here:
https://docs.python.org/3/howto/argparse.html

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv.

1
2
3
4
5
6
7
8
9
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')

args = parser.parse_args()
print(args.accumulate(args.integers))

1
2
3
4
5
6
7
8
9
# Handling Unicode: http://stackoverflow.com/a/6633040/305414
import sys
if sys.version < '3':
import codecs
def u(x):
return codecs.unicode_escape_decode(x)[0]
else:
def u(x):
return x

Still search for the documentation for the function unicode_escape_decode() in codecs.

0%