Skip to content

Commit b2fafff

Browse files
committed
Format code and fix \u2005 bug.
1 parent 00d7370 commit b2fafff

File tree

1 file changed

+33
-43
lines changed

1 file changed

+33
-43
lines changed

wxbot.py

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,6 @@
1111
import random
1212

1313

14-
def utf82gbk(string):
15-
return string.decode('utf8').encode('gbk')
16-
17-
18-
def make_unicode(data):
19-
if not data:
20-
return data
21-
result = None
22-
if type(data) == unicode:
23-
result = data
24-
elif type(data) == str:
25-
result = data.decode('utf-8')
26-
return result
27-
28-
2914
class WXBot:
3015
def __init__(self):
3116
self.DEBUG = False
@@ -399,7 +384,7 @@ def extract_msg_content(self, msg_type_id, msg):
399384
uid = uid[:-1]
400385
msg_content['user'] = {'id': uid, 'name': self.get_user_remark_name(uid)}
401386
if self.DEBUG:
402-
print msg_content['user']
387+
print msg_content['user']['name']
403388
else: # Self, Contact, Special, Public, Unknown
404389
pass
405390

@@ -413,24 +398,24 @@ def extract_msg_content(self, msg_type_id, msg):
413398
msg_content['data'] = pos
414399
msg_content['detail'] = data
415400
if self.DEBUG:
416-
print '[Location] I am at %s ' % pos
401+
print ' [Location] I am at %s ' % pos
417402
else:
418403
msg_content['type'] = 0
419-
msg_content['data'] = content
404+
msg_content['data'] = content.replace(u'\u2005', '')
420405
if self.DEBUG:
421-
print '[Text] %s' % content
406+
print ' [Text] %s' % msg_content['data']
422407
elif mtype == 3:
423408
msg_content['type'] = 3
424409
msg_content['data'] = self.get_msg_img_url(msg_id)
425410
if self.DEBUG:
426411
image = self.get_msg_img(msg_id)
427-
print '[Image] %s' % image
412+
print ' [Image] %s' % image
428413
elif mtype == 34:
429414
msg_content['type'] = 4
430415
msg_content['data'] = self.get_voice_url(msg_id)
431416
if self.DEBUG:
432417
voice = self.get_voice(msg_id)
433-
print '[Voice] %s' % voice
418+
print ' [Voice] %s' % voice
434419
elif mtype == 42:
435420
msg_content['type'] = 5
436421
info = msg['RecommendInfo']
@@ -440,18 +425,18 @@ def extract_msg_content(self, msg_type_id, msg):
440425
'city': info['City'],
441426
'gender': ['unknown', 'male', 'female'][info['Sex']]}
442427
if self.DEBUG:
443-
print '[Recommend]'
444-
print '========================='
445-
print '= NickName: %s' % info['NickName']
446-
print '= Alias: %s' % info['Alias']
447-
print '= Local: %s %s' % (info['Province'], info['City'])
448-
print '= Gender: %s' % ['unknown', 'male', 'female'][info['Sex']]
449-
print '========================='
428+
print ' [Recommend]'
429+
print ' -----------------------------'
430+
print ' | NickName: %s' % info['NickName']
431+
print ' | Alias: %s' % info['Alias']
432+
print ' | Local: %s %s' % (info['Province'], info['City'])
433+
print ' | Gender: %s' % ['unknown', 'male', 'female'][info['Sex']]
434+
print ' -----------------------------'
450435
elif mtype == 47:
451436
msg_content['type'] = 6
452437
msg_content['data'] = self.search_content('cdnurl', content)
453438
if self.DEBUG:
454-
print '[Animation] %s' % msg_content['data']
439+
print ' [Animation] %s' % msg_content['data']
455440
elif mtype == 49:
456441
msg_content['type'] = 7
457442
app_msg_type = ''
@@ -469,34 +454,34 @@ def extract_msg_content(self, msg_type_id, msg):
469454
'url': msg['Url'],
470455
'from': self.search_content('appname', content, 'xml')}
471456
if self.DEBUG:
472-
print '[Share] %s' % app_msg_type
473-
print '========================='
474-
print '= title: %s' % msg['FileName']
475-
print '= desc: %s' % self.search_content('des', content, 'xml')
476-
print '= link: %s' % msg['Url']
477-
print '= from: %s' % self.search_content('appname', content, 'xml')
478-
print '========================='
457+
print ' [Share] %s' % app_msg_type
458+
print ' --------------------------'
459+
print ' | title: %s' % msg['FileName']
460+
print ' | desc: %s' % self.search_content('des', content, 'xml')
461+
print ' | link: %s' % msg['Url']
462+
print ' | from: %s' % self.search_content('appname', content, 'xml')
463+
print ' --------------------------'
479464

480465
elif mtype == 62:
481466
msg_content['type'] = 8
482467
msg_content['data'] = content
483468
if self.DEBUG:
484-
print '[Video] Please check on mobiles'
469+
print ' [Video] Please check on mobiles'
485470
elif mtype == 53:
486471
msg_content['type'] = 9
487472
msg_content['data'] = content
488473
if self.DEBUG:
489-
print '[Video Call]'
474+
print ' [Video Call]'
490475
elif mtype == 10002:
491476
msg_content['type'] = 10
492477
msg_content['data'] = content
493478
if self.DEBUG:
494-
print '[Redraw]'
479+
print ' [Redraw]'
495480
else:
496481
msg_content['type'] = 99
497482
msg_content['data'] = content
498483
if self.DEBUG:
499-
print '[Unknown]'
484+
print ' [Unknown]'
500485
return msg_content
501486

502487
'''
@@ -537,7 +522,9 @@ def handle_msg(self, r):
537522
msg_type_id = 6
538523
user['name'] = self.get_user_remark_name(user['id'])
539524
else:
540-
pass # Unknown
525+
user['name'] = 'unknown' # Unknown
526+
if self.DEBUG and msg_type_id != 0:
527+
print '[MSG] %s:' % user['name']
541528
content = self.extract_msg_content(msg_type_id, msg)
542529
message = {'msg_type_id': msg_type_id,
543530
'msg_id': msg['MsgId'],
@@ -570,11 +557,13 @@ def proc_msg(self):
570557
def send_msg_by_uid(self, word, dst='filehelper'):
571558
url = self.base_uri + '/webwxsendmsg?pass_ticket=%s' % self.pass_ticket
572559
msg_id = str(int(time.time()*1000)) + str(random.random())[:5].replace('.', '')
560+
if type(word) == 'str':
561+
word = word.decode('utf-8')
573562
params = {
574563
'BaseRequest': self.base_request,
575564
'Msg': {
576565
"Type": 1,
577-
"Content": make_unicode(word),
566+
"Content": word,
578567
"FromUserName": self.user['UserName'],
579568
"ToUserName": dst,
580569
"LocalID": msg_id,
@@ -612,7 +601,8 @@ def send_msg(self, name, word, isfile=False):
612601
print '[ERROR] This user does not exist .'
613602
return True
614603

615-
def search_content(self, key, content, fmat='attr'):
604+
@staticmethod
605+
def search_content(key, content, fmat='attr'):
616606
if fmat == 'attr':
617607
pm = re.search(key+'\s?=\s?"([^"<]+)"', content)
618608
if pm:

0 commit comments

Comments
 (0)