11
11
import random
12
12
13
13
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
-
29
14
class WXBot :
30
15
def __init__ (self ):
31
16
self .DEBUG = False
@@ -399,7 +384,7 @@ def extract_msg_content(self, msg_type_id, msg):
399
384
uid = uid [:- 1 ]
400
385
msg_content ['user' ] = {'id' : uid , 'name' : self .get_user_remark_name (uid )}
401
386
if self .DEBUG :
402
- print msg_content ['user' ]
387
+ print msg_content ['user' ][ 'name' ]
403
388
else : # Self, Contact, Special, Public, Unknown
404
389
pass
405
390
@@ -413,24 +398,24 @@ def extract_msg_content(self, msg_type_id, msg):
413
398
msg_content ['data' ] = pos
414
399
msg_content ['detail' ] = data
415
400
if self .DEBUG :
416
- print '[Location] I am at %s ' % pos
401
+ print ' [Location] I am at %s ' % pos
417
402
else :
418
403
msg_content ['type' ] = 0
419
- msg_content ['data' ] = content
404
+ msg_content ['data' ] = content . replace ( u' \u2005 ' , '' )
420
405
if self .DEBUG :
421
- print '[Text] %s' % content
406
+ print ' [Text] %s' % msg_content [ 'data' ]
422
407
elif mtype == 3 :
423
408
msg_content ['type' ] = 3
424
409
msg_content ['data' ] = self .get_msg_img_url (msg_id )
425
410
if self .DEBUG :
426
411
image = self .get_msg_img (msg_id )
427
- print '[Image] %s' % image
412
+ print ' [Image] %s' % image
428
413
elif mtype == 34 :
429
414
msg_content ['type' ] = 4
430
415
msg_content ['data' ] = self .get_voice_url (msg_id )
431
416
if self .DEBUG :
432
417
voice = self .get_voice (msg_id )
433
- print '[Voice] %s' % voice
418
+ print ' [Voice] %s' % voice
434
419
elif mtype == 42 :
435
420
msg_content ['type' ] = 5
436
421
info = msg ['RecommendInfo' ]
@@ -440,18 +425,18 @@ def extract_msg_content(self, msg_type_id, msg):
440
425
'city' : info ['City' ],
441
426
'gender' : ['unknown' , 'male' , 'female' ][info ['Sex' ]]}
442
427
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 ' ----------------------------- '
450
435
elif mtype == 47 :
451
436
msg_content ['type' ] = 6
452
437
msg_content ['data' ] = self .search_content ('cdnurl' , content )
453
438
if self .DEBUG :
454
- print '[Animation] %s' % msg_content ['data' ]
439
+ print ' [Animation] %s' % msg_content ['data' ]
455
440
elif mtype == 49 :
456
441
msg_content ['type' ] = 7
457
442
app_msg_type = ''
@@ -469,34 +454,34 @@ def extract_msg_content(self, msg_type_id, msg):
469
454
'url' : msg ['Url' ],
470
455
'from' : self .search_content ('appname' , content , 'xml' )}
471
456
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 ' -------------------------- '
479
464
480
465
elif mtype == 62 :
481
466
msg_content ['type' ] = 8
482
467
msg_content ['data' ] = content
483
468
if self .DEBUG :
484
- print '[Video] Please check on mobiles'
469
+ print ' [Video] Please check on mobiles'
485
470
elif mtype == 53 :
486
471
msg_content ['type' ] = 9
487
472
msg_content ['data' ] = content
488
473
if self .DEBUG :
489
- print '[Video Call]'
474
+ print ' [Video Call]'
490
475
elif mtype == 10002 :
491
476
msg_content ['type' ] = 10
492
477
msg_content ['data' ] = content
493
478
if self .DEBUG :
494
- print '[Redraw]'
479
+ print ' [Redraw]'
495
480
else :
496
481
msg_content ['type' ] = 99
497
482
msg_content ['data' ] = content
498
483
if self .DEBUG :
499
- print '[Unknown]'
484
+ print ' [Unknown]'
500
485
return msg_content
501
486
502
487
'''
@@ -537,7 +522,9 @@ def handle_msg(self, r):
537
522
msg_type_id = 6
538
523
user ['name' ] = self .get_user_remark_name (user ['id' ])
539
524
else :
540
- pass # Unknown
525
+ user ['name' ] = 'unknown' # Unknown
526
+ if self .DEBUG and msg_type_id != 0 :
527
+ print '[MSG] %s:' % user ['name' ]
541
528
content = self .extract_msg_content (msg_type_id , msg )
542
529
message = {'msg_type_id' : msg_type_id ,
543
530
'msg_id' : msg ['MsgId' ],
@@ -570,11 +557,13 @@ def proc_msg(self):
570
557
def send_msg_by_uid (self , word , dst = 'filehelper' ):
571
558
url = self .base_uri + '/webwxsendmsg?pass_ticket=%s' % self .pass_ticket
572
559
msg_id = str (int (time .time ()* 1000 )) + str (random .random ())[:5 ].replace ('.' , '' )
560
+ if type (word ) == 'str' :
561
+ word = word .decode ('utf-8' )
573
562
params = {
574
563
'BaseRequest' : self .base_request ,
575
564
'Msg' : {
576
565
"Type" : 1 ,
577
- "Content" : make_unicode ( word ) ,
566
+ "Content" : word ,
578
567
"FromUserName" : self .user ['UserName' ],
579
568
"ToUserName" : dst ,
580
569
"LocalID" : msg_id ,
@@ -612,7 +601,8 @@ def send_msg(self, name, word, isfile=False):
612
601
print '[ERROR] This user does not exist .'
613
602
return True
614
603
615
- def search_content (self , key , content , fmat = 'attr' ):
604
+ @staticmethod
605
+ def search_content (key , content , fmat = 'attr' ):
616
606
if fmat == 'attr' :
617
607
pm = re .search (key + '\s?=\s?"([^"<]+)"' , content )
618
608
if pm :
0 commit comments