From dcad5ed99d87c92eee8fada64b66c84846814cfa Mon Sep 17 00:00:00 2001 From: futurewrg Date: Wed, 29 Nov 2017 17:34:51 +0800 Subject: [PATCH] Add support for new avatar storage (#50) * Add support for new avatar storage,modify the README and android-interact.sh * Show the nickname for chatroom, fix the IOError in avatar.py * style change --- README.md | 4 ++-- android-interact.sh | 2 +- wechat/avatar.py | 27 +++++++++++++++++++++------ wechat/render.py | 4 ++++ wechat/static/TP_EMOJI.html | 2 +- wechat/static/TP_IMG.html | 2 +- wechat/static/TP_MSG.html | 2 +- wechat/static/TP_SPEAK.html | 2 +- 8 files changed, 32 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 12d7321..347a336 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ pip install --user --pre pysox If things still don't work, you can try the [password cracker](https://github.com/chg-hou/EnMicroMsg.db-Password-Cracker) to brute-force the password. -+ Copy the WeChat user resource directory `/mnt/sdcard/tencent/MicroMsg/${userid}/{emoji,image2,sfs,video,voice2}` from the phone to the `resource` directory: ++ Copy the WeChat user resource directory `/mnt/sdcard/tencent/MicroMsg/${userid}/{avatar,emoji,image2,sfs,video,voice2}` from the phone to the `resource` directory: + `./android-interact.sh res` + You might need to change `RES_DIR` in the script if the default is incorrect on your phone. + This can take a __very long__ time. Some manual ways to do it faster: @@ -87,7 +87,7 @@ pip install --user --pre pysox base64 -di | tar xzf - ``` - + What you'll need in the end is a `resource` directory with the following subdir: `emoji,image2,sfs,video,voice2`. + + What you'll need in the end is a `resource` directory with the following subdir: `avatar,emoji,image2,sfs,video,voice2`. + (Optional) Download the emoji cache from [here](https://github.com/ppwwyyxx/wechat-dump/releases/download/0.1/emoji.cache.tar.bz2) and put it under `resource/emoji`. This will avoid downloading too many emojis during rendering. diff --git a/android-interact.sh b/android-interact.sh index 97db410..4b287b4 100755 --- a/android-interact.sh +++ b/android-interact.sh @@ -53,7 +53,7 @@ elif [[ $1 == "db" || $1 == "res" ]]; then if [[ $1 == "res" ]]; then echo "Pulling resources... this might take a long time, because adb sucks..." mkdir -p resource; cd resource - for d in image2 voice2 emoji video sfs; do + for d in avatar image2 voice2 emoji video sfs; do mkdir -p $d; cd $d adb pull "$RES_DIR/$chooseUser/$d" cd .. diff --git a/wechat/avatar.py b/wechat/avatar.py index 5147ec5..c4a5acb 100644 --- a/wechat/avatar.py +++ b/wechat/avatar.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: avatar.py -# Date: Thu Jun 18 00:02:07 2015 +0800 +# Date: Wed Nov 29 01:33:56 2017 -0800 # Author: Yuxin Wu from PIL import Image @@ -14,19 +14,27 @@ logger = logging.getLogger(__name__) from common.textutil import ensure_bin_str, md5 + class AvatarReader(object): def __init__(self, avt_dir, avt_db="avatar.index"): self.avt_dir = avt_dir + # new location of avatar, see #50 + self.avt_dir_new = avt_dir[:avt_dir.find('sfs')] + 'avatar' self.avt_db = avt_db - if self.avt_db is None or not os.path.isfile(self.avt_db): + self._use_avt = True + if self.avt_db is not None and os.path.isfile(self.avt_db): + self.ava_use_db = True + elif os.path.isdir(self.avt_dir_new): + self.ava_use_db = False + else: logger.warn( "Avatar database {} not found. Will not use avatar!".format(avt_db)) - self.avt_db = None + self._use_avt = False def get_avatar(self, username): """ username: `username` field in db.rcontact""" - if self.avt_db is None: + if not self._use_avt: return None username = ensure_bin_str(username) @@ -37,8 +45,15 @@ class AvatarReader(object): try: try: - pos, size = self.query_index(filename) - return self.read_img(pos, size) + if self.avt_use_db: + pos, size = self.query_index(filename) + return self.read_img(pos, size) + else: + img_file = os.path.join(self.avt_dir_new, filename) + if os.path.exists(img_file): + return Image.open(img_file) + else: + return None except TypeError: logger.warn("Avatar for {} not found in avatar database.".format(username)) return None diff --git a/wechat/render.py b/wechat/render.py index 7f0f6b7..94722af 100644 --- a/wechat/render.py +++ b/wechat/render.py @@ -95,6 +95,10 @@ class HTMLRender(object): sender = u'you ' + msg.talker if not msg.isSend else 'me' format_dict = {'sender_label': sender, 'time': msg.createTime } + if(not msg.isSend and msg.is_chatroom()): + format_dict['nickname'] = '>\n
'+msg.talker_nickname+'
     
     
-
+
diff --git a/wechat/static/TP_IMG.html b/wechat/static/TP_IMG.html index 3100426..a116219 100644 --- a/wechat/static/TP_IMG.html +++ b/wechat/static/TP_IMG.html @@ -2,7 +2,7 @@
-
+
diff --git a/wechat/static/TP_MSG.html b/wechat/static/TP_MSG.html index 4cbf25b..03bfb5e 100644 --- a/wechat/static/TP_MSG.html +++ b/wechat/static/TP_MSG.html @@ -2,7 +2,7 @@
-
+
{content}
diff --git a/wechat/static/TP_SPEAK.html b/wechat/static/TP_SPEAK.html index 6c903ac..b47e026 100644 --- a/wechat/static/TP_SPEAK.html +++ b/wechat/static/TP_SPEAK.html @@ -2,7 +2,7 @@
-
+
{voice_duration}"