Pārlūkot izejas kodu

add internal emoji

ppwwyyxx 11 gadi atpakaļ
vecāks
revīzija
937fa8b88f
3 mainītis faili ar 31 papildinājumiem un 9 dzēšanām
  1. 12 1
      lib/parser.py
  2. 9 6
      lib/render.py
  3. 10 2
      lib/res.py

+ 12 - 1
lib/parser.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python2
 # -*- coding: UTF-8 -*-
 # File: parser.py
-# Date: Sat Dec 20 19:40:31 2014 +0800
+# Date: Mon Dec 22 09:45:35 2014 +0800
 # Author: Yuxin Wu <[email protected]>
 
 import sqlite3
@@ -31,6 +31,7 @@ class WeChatDBParser(object):
         self.contacts = {}
         self.msgs_by_talker = defaultdict(list)
         self.emojis = {}
+        self.internal_emojis = {}
         self.parse()
 
     def _parse_contact(self):
@@ -95,6 +96,16 @@ SELECT {} FROM message
             md5, desc, group = row
             self.emojis[md5] = (group, desc)
 
+        NEEDED_EMOJI_CATALOG = [49, 50]
+        emojiinfo_q = self.cc.execute(
+""" SELECT md5, catalog, name FROM EmojiInfo WHERE name <> ''""")
+        for row in emojiinfo_q:
+            md5, catalog, name = row
+            if catalog not in NEEDED_EMOJI_CATALOG:
+                continue
+            self.internal_emojis[md5] = name
+
+
     def parse(self):
         self._parse_userinfo()
         self._parse_contact()

+ 9 - 6
lib/render.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python2
 # -*- coding: UTF-8 -*-
 # File: render.py
-# Date: Sun Dec 21 23:48:29 2014 +0800
+# Date: Mon Dec 22 09:49:12 2014 +0800
 # Author: Yuxin Wu <[email protected]>
 
 import os
@@ -100,12 +100,15 @@ class HTMLRender(object):
                                    big_img=bigimg)
         elif msg.type == TYPE_EMOJI:
             imgpath = msg.imgPath
-
-            if imgpath in self.parser.emojis:
-                group, _ = self.parser.emojis[imgpath]
+            if imgpath in self.parser.internal_emojis:
+                emoji_img, format = self.res.get_internal_emoji(self.parser.internal_emojis[imgpath])
             else:
-                group = None
-            emoji_img, format = self.res.get_emoji(imgpath, group)
+                if imgpath in self.parser.emojis:
+                    group, _ = self.parser.emojis[imgpath]
+                else:
+                    group = None
+                emoji_img, format = self.res.get_emoji(imgpath, group)
+            assert emoji_img
             return template.format(sender_label=sender,
                                   emoji_format=format,
                                   emoji_img=emoji_img)

+ 10 - 2
lib/res.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python2
 # -*- coding: UTF-8 -*-
 # File: res.py
-# Date: Sun Dec 21 23:49:46 2014 +0800
+# Date: Mon Dec 22 09:48:16 2014 +0800
 # Author: Yuxin Wu <[email protected]>
 
 import glob
@@ -18,6 +18,8 @@ import eyed3
 
 from lib.avatar import AvatarReader
 
+LIB_PATH = os.path.dirname(os.path.abspath(__file__))
+INTERNAL_EMOJI_DIR = os.path.join(LIB_PATH, 'static', 'internal_emoji')
 VOICE_DIRNAME = 'voice2'
 IMG_DIRNAME = 'image2'
 EMOJI_DIRNAME = 'emoji'
@@ -157,7 +159,13 @@ class Resource(object):
             candidates = [k for k in candidates if not re.match('.*_[0-9]+$', k)]
             # only one file is the gif in need, others are frames and cover
             assert len(candidates) == 1
-        print candidates, md5
+        if not candidates:
+            return None, None
         fname = candidates[0]
         return Resource.get_file_b64(fname), imghdr.what(fname)
 
+    def get_internal_emoji(self, fname):
+        f = os.path.join(INTERNAL_EMOJI_DIR, fname)
+        return Resource.get_file_b64(fname), imghdr.what(fname)
+
+