add logger
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
|
||||
import logging
|
||||
logging.basicConfig(
|
||||
format='\033[1;32m[%(asctime)s %(lineno)d@%(filename)s:%(name)s]\033[0m'
|
||||
' %(message)s',
|
||||
datefmt='%H:%M:%S', level=logging.INFO)
|
||||
|
||||
+3
-2
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: avatar.py
|
||||
# Date: Sun Nov 23 16:28:00 2014 +0800
|
||||
# Date: Fri Dec 12 22:39:37 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import os
|
||||
@@ -13,7 +13,8 @@ from .utils import ensure_bin_str
|
||||
class AvatarReader(object):
|
||||
def __init__(self, res_dir):
|
||||
self.avt_dir = os.path.join(res_dir, 'avatar')
|
||||
assert os.path.isdir(self.avt_dir), "No such directory {}".format(self.avt_dir)
|
||||
assert os.path.isdir(self.avt_dir), \
|
||||
"No such directory {}".format(self.avt_dir)
|
||||
|
||||
@staticmethod
|
||||
def get_filename(username):
|
||||
|
||||
+9
-6
@@ -1,12 +1,14 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: parser.py
|
||||
# Date: Sun Nov 23 20:44:02 2014 +0800
|
||||
# Date: Fri Dec 12 22:35:56 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import sqlite3
|
||||
from collections import defaultdict
|
||||
import itertools
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from .msg import WeChatMsg
|
||||
from .utils import ensure_unicode
|
||||
@@ -41,7 +43,7 @@ SELECT username,conRemark,nickname FROM rcontact
|
||||
else:
|
||||
self.contacts[username] = ensure_unicode(nickname)
|
||||
|
||||
print "Got {} contacts.".format(len(self.contacts))
|
||||
logger.info("Got {} contacts.".format(len(self.contacts)))
|
||||
|
||||
def _parse_msg(self):
|
||||
msgs_tot_cnt = 0
|
||||
@@ -60,18 +62,19 @@ SELECT {} FROM message
|
||||
for msg in v:
|
||||
msg.talker_name = ensure_unicode(k)
|
||||
msgs_tot_cnt += len(v)
|
||||
print "Got {} messages in total.".format(msgs_tot_cnt)
|
||||
logger.info("Got {} messages in total.".format(msgs_tot_cnt))
|
||||
|
||||
def _parse_userinfo(self):
|
||||
userinfo_q = self.cc.execute(""" SELECT id, value FROM userinfo """)
|
||||
userinfo = dict(userinfo_q)
|
||||
self.username = userinfo[2]
|
||||
print "Your username is: {}".format(self.username)
|
||||
logger.info("Your username is: {}".format(self.username))
|
||||
|
||||
def _parse_imginfo(self):
|
||||
imginfo_q = self.cc.execute("""SELECT msgSvrId, bigImgPath FROM ImgInfo2""")
|
||||
self.imginfo = dict([(k, v) for (k, v) in imginfo_q if not v.startswith('SERVERID://')])
|
||||
print "Got {} big images.".format(len(self.imginfo))
|
||||
self.imginfo = dict([(k, v) for (k, v) in imginfo_q
|
||||
if not v.startswith('SERVERID://')])
|
||||
logger.info("Got {} big images.".format(len(self.imginfo)))
|
||||
|
||||
def _find_msg_by_type(self, msgs=None):
|
||||
ret = []
|
||||
|
||||
+10
-13
@@ -1,12 +1,14 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: render.py
|
||||
# Date: Sun Nov 23 22:57:13 2014 +0800
|
||||
# Date: Fri Dec 12 23:15:31 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import os
|
||||
import base64
|
||||
import eyed3
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
LIB_PATH = os.path.dirname(os.path.abspath(__file__))
|
||||
CSS_FILE = os.path.join(LIB_PATH, 'static/wx.css')
|
||||
HTML_FILE = os.path.join(LIB_PATH, 'static/template.html')
|
||||
@@ -50,7 +52,7 @@ class HTMLRender(object):
|
||||
# TODO is there a library to use?
|
||||
ret = os.system('sox {} {}'.format(amr_fpath, mp3_file))
|
||||
if ret != 0:
|
||||
print "Sox Failed!"
|
||||
logger.warn("Sox Failed!")
|
||||
return ""
|
||||
mp3_string = open(mp3_file, 'rb').read()
|
||||
duration = eyed3.load(mp3_file).info.time_secs
|
||||
@@ -68,18 +70,13 @@ class HTMLRender(object):
|
||||
voice_duration=duration,
|
||||
voice_str=audio_str)
|
||||
elif msg.type == TYPE_IMG:
|
||||
img = ""
|
||||
svrid = msg.msgSvrId
|
||||
imgpath = msg.imgPath
|
||||
#bigimg = self.parser.imginfo.get(svrid)
|
||||
#if bigimg:
|
||||
#img = self.res.get_img(bigimg, smart=False)
|
||||
if len(img) == 0:
|
||||
#print "Big Image Failed: {}".format(bigimg)
|
||||
img = imgpath.split('_')[-1]
|
||||
img = self.res.get_img(img)
|
||||
# imgPath was original THUMBNAIL_DIRPATH://th_xxxxxxxxx
|
||||
imgpath = msg.imgPath.split('_')[-1]
|
||||
bigimgpath = self.parser.imginfo.get(msg.msgSvrId)
|
||||
|
||||
bigimg, smallimg = self.res.get_img([imgpath, bigimgpath])
|
||||
return template.format(sender_label='you' if not msg.isSend else 'me',
|
||||
img_data=img)
|
||||
img_data=smallimg)
|
||||
else:
|
||||
raise
|
||||
except:
|
||||
|
||||
+57
-28
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 -*-
|
||||
# File: res.py
|
||||
# Date: Sun Nov 23 21:08:44 2014 +0800
|
||||
# Date: Fri Dec 12 23:12:06 2014 +0800
|
||||
# Author: Yuxin Wu <[email protected]>
|
||||
|
||||
import glob
|
||||
@@ -9,6 +9,8 @@ import os
|
||||
import Image
|
||||
import cStringIO
|
||||
import base64
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from lib.avatar import AvatarReader
|
||||
|
||||
@@ -21,6 +23,8 @@ class Resource(object):
|
||||
assert os.path.isdir(res_dir), "No such directory: {}".format(res_dir)
|
||||
self.res_dir = res_dir
|
||||
self.img_dir = os.path.join(res_dir, 'image2')
|
||||
assert os.path.isdir(self.img_dir), \
|
||||
"No such directory: {}".format(self.img_dir)
|
||||
self.avt_reader = AvatarReader(self.res_dir)
|
||||
self.init()
|
||||
|
||||
@@ -56,38 +60,63 @@ class Resource(object):
|
||||
jpeg_str = buf.getvalue()
|
||||
return base64.b64encode(jpeg_str)
|
||||
|
||||
def get_img_file(self, fname, smart):
|
||||
""" return base64 string"""
|
||||
dir1, dir2 = fname[:2], fname[2:4]
|
||||
if not smart:
|
||||
filename = os.path.join(self.img_dir, dir1, dir2, fname)
|
||||
if os.path.isfile(filename):
|
||||
return filename
|
||||
else:
|
||||
def get_img_file(self, fnames):
|
||||
""" fnames: a list of filename to search for
|
||||
return (filename, filename) of (big, small) image.
|
||||
could be empty string.
|
||||
"""
|
||||
cands = []
|
||||
for fname in fnames:
|
||||
dir1, dir2 = fname[:2], fname[2:4]
|
||||
dirname = os.path.join(self.img_dir, dir1, dir2)
|
||||
if not os.path.isdir(dirname):
|
||||
print "Directory not found: {}".format(dirname)
|
||||
return ""
|
||||
maxf, maxsize = "", 0
|
||||
logger.warn("Directory not found: {}".format(dirname))
|
||||
continue
|
||||
for f in os.listdir(dirname):
|
||||
if fname in f:
|
||||
full_name = os.path.join(dirname, f)
|
||||
size = os.path.getsize(full_name)
|
||||
if size > maxsize:
|
||||
maxsize = size
|
||||
maxf = full_name
|
||||
if maxsize == 0:
|
||||
return ""
|
||||
else:
|
||||
return maxf
|
||||
return ""
|
||||
if size > 0:
|
||||
cands.append((full_name, size))
|
||||
if not cands:
|
||||
return ("", "")
|
||||
cands = sorted(cands, key=lambda x: x[1])
|
||||
|
||||
def get_img(self, fname, smart=True):
|
||||
img_file = self.get_img_file(fname, smart)
|
||||
if not img_file.endswith('jpg') or not img_file.startswith('th_'):
|
||||
im = Image.open(open(img_file, 'rb'))
|
||||
buf = cStringIO.StringIO()
|
||||
im.save(buf, 'JPEG', quality=JPEG_QUALITY)
|
||||
return base64.b64encode(buf.getvalue())
|
||||
else:
|
||||
def name_is_thumbnail(name):
|
||||
return os.path.basename(name).startswith('th_') \
|
||||
and not name.endswith('hd')
|
||||
if len(cands) == 1:
|
||||
name = cands[0][0]
|
||||
if name_is_thumbnail(name):
|
||||
# thumbnail
|
||||
return ("", name)
|
||||
else:
|
||||
logger.warn("Found big image but not thumbnail: {}".format(fname))
|
||||
return (name, "")
|
||||
big, small = cands[-1], cands[0]
|
||||
if not name_is_thumbnail(small[0]):
|
||||
return (big[0], "")
|
||||
return (big[0], small[0])
|
||||
|
||||
def get_img(self, fnames):
|
||||
""" return two base64 jpg string"""
|
||||
big_file, small_file = self.get_img_file(fnames)
|
||||
|
||||
def get_jpg_b64(img_file):
|
||||
if not big_file:
|
||||
return None
|
||||
if not img_file.endswith('jpg'):
|
||||
# possibly not jpg
|
||||
im = Image.open(open(img_file, 'rb'))
|
||||
buf = cStringIO.StringIO()
|
||||
im.save(buf, 'JPEG', quality=JPEG_QUALITY)
|
||||
return base64.b64encode(buf.getvalue())
|
||||
return Resource.get_file_b64(img_file)
|
||||
big_file = get_jpg_b64(big_file)
|
||||
small_file = get_jpg_b64(small_file)
|
||||
|
||||
if big_file and not small_file:
|
||||
# TODO resize big to a thumbnail
|
||||
pass
|
||||
return (big_file, small_file)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user