diff --git a/README.md b/README.md index 8030882..c628c14 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ ### How to use: #### Install Dependencies: -+ python-numpy + python-PIL ++ [PyQuery](https://pypi.python.org/pypi/pyquery/1.2.1) + [eyed3](http://eyed3.nicfit.net/) + sox + python-csscompressor(optional) @@ -38,7 +38,7 @@ ### TODO + Parse uncommon message types -+ Show name of emoji in text ++ Add date/time in html ++ Fix smiley spacing in html ++ Show name of emoji in text output + Search by uid/username.. -+ Fix emoji sizes.. -+ Sometimes beautiful soup failed on links.. diff --git a/lib/msg.py b/lib/msg.py index 7b0fa27..29146ed 100644 --- a/lib/msg.py +++ b/lib/msg.py @@ -1,11 +1,11 @@ #!/usr/bin/env python2 # -*- coding: UTF-8 -*- # File: msg.py -# Date: Sun Dec 21 21:23:27 2014 +0800 +# Date: Sun Dec 21 21:50:50 2014 +0800 # Author: Yuxin Wu from datetime import datetime -from bs4 import BeautifulSoup +from pyquery import PyQuery from .utils import ensure_bin_str, ensure_unicode TYPE_MSG = 1 @@ -41,30 +41,24 @@ class WeChatMsg(object): self.content = u"" def msg_str(self): - # TODO: fix more types if self.type == TYPE_LOCATION: - soup = BeautifulSoup(self.content) - loc = soup.find('location') + pq = PyQuery(self.content) + loc = pq('location').attr label = loc['label'] - ret = label try: poiname = loc['poiname'] if poiname: - ret = poiname + label = poiname except: pass - return ret + " ({},{})".format(loc['x'], loc['y']) + return label + " ({},{})".format(loc['x'], loc['y']) elif self.type == TYPE_VOIP: return "REQUEST VIDEO CHAT" elif self.type == TYPE_LINK: - soup = BeautifulSoup(self.content) - url = soup.find('url').text + pq = PyQuery(self.content) + url = pq('url').text() if not url: - title = soup.find('title').text - # TODO sometimes fail to parse title - if not title: - print self.content - from IPython import embed; embed() + title = pq('title').text() assert title, \ u"No title or url found in TYPE_LINK: {}".format(self.content) return u"FILE:{}".format(title) @@ -72,11 +66,13 @@ class WeChatMsg(object): elif self.type == TYPE_VIDEO: return "VIDEO" elif self.type == TYPE_NAMECARD: - soup = BeautifulSoup(self.content) - msg = soup.find('msg').attrs - name = msg.get('nickname', '') + pq = PyQuery(self.content) + msg = pq('msg').attr + name = msg['nickname'] if not name: - name = msg.get('alias', '') + name = msg['alias'] + if not name: + name = "" return u"NAMECARD: {}".format(name) elif self.type == TYPE_EMOJI: # TODO add emoji name @@ -102,8 +98,8 @@ class WeChatMsg(object): def get_emoji_product_id(self): assert self.type == TYPE_EMOJI, "Wrong call to get_emoji_product_id()!" - soup = BeautifulSoup(self.content) - emoji = soup.find('emoji') - if emoji is None: + pq = PyQuery(self.content) + emoji = pq('emoji') + if not emoji: return None - return emoji.get('productid', None) + return emoji.attrs['productid']