@@ -49,8 +49,11 @@ def get_contact(self):
49
49
r = self .session .post (url , data = '{}' )
50
50
r .encoding = 'utf-8'
51
51
if self .DEBUG :
52
- with open ('contacts.json' , 'w ' ) as f :
52
+ with open ('contacts.json' , 'wb ' ) as f :
53
53
f .write (r .text .encode ('utf-8' ))
54
+ # if self.DEBUG:
55
+ # with open('contacts.json', 'wb') as f:
56
+ # f.write(r.text)
54
57
dic = json .loads (r .text )
55
58
self .member_list = dic ['MemberList' ]
56
59
@@ -282,24 +285,24 @@ def extract_msg_content(self, msg_type_id, msg):
282
285
msg_content ['data' ] = pos
283
286
msg_content ['detail' ] = data
284
287
if self .DEBUG :
285
- print ' %s[Location] %s ' % (msg_prefix , pos )
288
+ print ( ' %s[Location] %s ' % (msg_prefix , pos ) )
286
289
else :
287
290
msg_content ['type' ] = 0
288
291
msg_content ['data' ] = content .replace (u'\u2005 ' , '' )
289
292
if self .DEBUG :
290
- print ' %s[Text] %s' % (msg_prefix , msg_content ['data' ])
293
+ print ( ' %s[Text] %s' % (msg_prefix , msg_content ['data' ]) )
291
294
elif mtype == 3 :
292
295
msg_content ['type' ] = 3
293
296
msg_content ['data' ] = self .get_msg_img_url (msg_id )
294
297
if self .DEBUG :
295
298
image = self .get_msg_img (msg_id )
296
- print ' %s[Image] %s' % (msg_prefix , image )
299
+ print ( ' %s[Image] %s' % (msg_prefix , image ) )
297
300
elif mtype == 34 :
298
301
msg_content ['type' ] = 4
299
302
msg_content ['data' ] = self .get_voice_url (msg_id )
300
303
if self .DEBUG :
301
304
voice = self .get_voice (msg_id )
302
- print ' %s[Voice] %s' % (msg_prefix , voice )
305
+ print ( ' %s[Voice] %s' % (msg_prefix , voice ) )
303
306
elif mtype == 42 :
304
307
msg_content ['type' ] = 5
305
308
info = msg ['RecommendInfo' ]
@@ -309,18 +312,18 @@ def extract_msg_content(self, msg_type_id, msg):
309
312
'city' : info ['City' ],
310
313
'gender' : ['unknown' , 'male' , 'female' ][info ['Sex' ]]}
311
314
if self .DEBUG :
312
- print ' %s[Recommend]' % msg_prefix
313
- print ' -----------------------------'
314
- print ' | NickName: %s' % info ['NickName' ]
315
- print ' | Alias: %s' % info ['Alias' ]
316
- print ' | Local: %s %s' % (info ['Province' ], info ['City' ])
317
- print ' | Gender: %s' % ['unknown' , 'male' , 'female' ][info ['Sex' ]]
318
- print ' -----------------------------'
315
+ print ( ' %s[Recommend]' % msg_prefix )
316
+ print ( ' -----------------------------' )
317
+ print ( ' | NickName: %s' % info ['NickName' ])
318
+ print ( ' | Alias: %s' % info ['Alias' ])
319
+ print ( ' | Local: %s %s' % (info ['Province' ], info ['City' ]) )
320
+ print ( ' | Gender: %s' % ['unknown' , 'male' , 'female' ][info ['Sex' ]])
321
+ print ( ' -----------------------------' )
319
322
elif mtype == 47 :
320
323
msg_content ['type' ] = 6
321
324
msg_content ['data' ] = self .search_content ('cdnurl' , content )
322
325
if self .DEBUG :
323
- print ' %s[Animation] %s' % (msg_prefix , msg_content ['data' ])
326
+ print ( ' %s[Animation] %s' % (msg_prefix , msg_content ['data' ]) )
324
327
elif mtype == 49 :
325
328
msg_content ['type' ] = 7
326
329
app_msg_type = ''
@@ -338,39 +341,39 @@ def extract_msg_content(self, msg_type_id, msg):
338
341
'url' : msg ['Url' ],
339
342
'from' : self .search_content ('appname' , content , 'xml' )}
340
343
if self .DEBUG :
341
- print ' %s[Share] %s' % (msg_prefix , app_msg_type )
342
- print ' --------------------------'
343
- print ' | title: %s' % msg ['FileName' ]
344
- print ' | desc: %s' % self .search_content ('des' , content , 'xml' )
345
- print ' | link: %s' % msg ['Url' ]
346
- print ' | from: %s' % self .search_content ('appname' , content , 'xml' )
347
- print ' --------------------------'
344
+ print ( ' %s[Share] %s' % (msg_prefix , app_msg_type ) )
345
+ print ( ' --------------------------' )
346
+ print ( ' | title: %s' % msg ['FileName' ])
347
+ print ( ' | desc: %s' % self .search_content ('des' , content , 'xml' ) )
348
+ print ( ' | link: %s' % msg ['Url' ])
349
+ print ( ' | from: %s' % self .search_content ('appname' , content , 'xml' ) )
350
+ print ( ' --------------------------' )
348
351
349
352
elif mtype == 62 :
350
353
msg_content ['type' ] = 8
351
354
msg_content ['data' ] = content
352
355
if self .DEBUG :
353
- print ' %s[Video] Please check on mobiles' % msg_prefix
356
+ print ( ' %s[Video] Please check on mobiles' % msg_prefix )
354
357
elif mtype == 53 :
355
358
msg_content ['type' ] = 9
356
359
msg_content ['data' ] = content
357
360
if self .DEBUG :
358
- print ' %s[Video Call]' % msg_prefix
361
+ print ( ' %s[Video Call]' % msg_prefix )
359
362
elif mtype == 10002 :
360
363
msg_content ['type' ] = 10
361
364
msg_content ['data' ] = content
362
365
if self .DEBUG :
363
- print ' %s[Redraw]' % msg_prefix
366
+ print ( ' %s[Redraw]' % msg_prefix )
364
367
elif mtype == 10000 :
365
368
msg_content ['type' ] = 12
366
369
msg_content ['data' ] = msg ['Content' ]
367
370
if self .DEBUG :
368
- print ' [Red Packet]'
371
+ print ( ' [Red Packet]' )
369
372
else :
370
373
msg_content ['type' ] = 99
371
374
msg_content ['data' ] = content
372
375
if self .DEBUG :
373
- print ' %s[Unknown]' % msg_prefix
376
+ print ( ' %s[Unknown]' % msg_prefix )
374
377
return msg_content
375
378
376
379
def handle_msg (self , r ):
@@ -417,7 +420,7 @@ def handle_msg(self, r):
417
420
user ['name' ] = 'unknown'
418
421
419
422
if self .DEBUG and msg_type_id != 0 :
420
- print '[MSG] %s:' % user ['name' ]
423
+ print ( '[MSG] %s:' % user ['name' ])
421
424
content = self .extract_msg_content (msg_type_id , msg )
422
425
message = {'msg_type_id' : msg_type_id ,
423
426
'msg_id' : msg ['MsgId' ],
@@ -505,7 +508,7 @@ def send_msg(self, name, word, isfile=False):
505
508
result = True
506
509
for line in f .readlines ():
507
510
line = line .replace ('\n ' , '' )
508
- print '-> ' + name + ': ' + line
511
+ print ( '-> ' + name + ': ' + line )
509
512
if self .send_msg_by_uid (line , uid ):
510
513
pass
511
514
else :
@@ -519,7 +522,7 @@ def send_msg(self, name, word, isfile=False):
519
522
return False
520
523
else :
521
524
if self .DEBUG :
522
- print '[ERROR] This user does not exist .'
525
+ print ( '[ERROR] This user does not exist .' )
523
526
return True
524
527
525
528
@staticmethod
@@ -537,24 +540,24 @@ def search_content(key, content, fmat='attr'):
537
540
def run (self ):
538
541
self .get_uuid ()
539
542
self .gen_qr_code ('qr.png' )
540
- print '[INFO] Please use WeCaht to scan the QR code .'
543
+ print ( '[INFO] Please use WeCaht to scan the QR code .' )
541
544
self .wait4login (1 )
542
- print '[INFO] Please confirm to login .'
545
+ print ( '[INFO] Please confirm to login .' )
543
546
self .wait4login (0 )
544
547
if self .login ():
545
- print '[INFO] Web WeChat login succeed .'
548
+ print ( '[INFO] Web WeChat login succeed .' )
546
549
else :
547
- print '[ERROR] Web WeChat login failed .'
550
+ print ( '[ERROR] Web WeChat login failed .' )
548
551
return
549
552
if self .init ():
550
- print '[INFO] Web WeChat init succeed .'
553
+ print ( '[INFO] Web WeChat init succeed .' )
551
554
else :
552
- print '[INFO] Web WeChat init failed'
555
+ print ( '[INFO] Web WeChat init failed' )
553
556
return
554
557
self .status_notify ()
555
558
self .get_contact ()
556
- print '[INFO] Get %d contacts' % len (self .contact_list )
557
- print '[INFO] Start to process messages .'
559
+ print ( '[INFO] Get %d contacts' % len (self .contact_list ) )
560
+ print ( '[INFO] Start to process messages .' )
558
561
self .proc_msg ()
559
562
560
563
def get_uuid (self ):
@@ -610,9 +613,9 @@ def wait4login(self, tip):
610
613
self .base_uri = redirect_uri [:redirect_uri .rfind ('/' )]
611
614
return True
612
615
elif code == '408' :
613
- print '[ERROR] WeChat login timeout .'
616
+ print ( '[ERROR] WeChat login timeout .' )
614
617
else :
615
- print '[ERROR] WeChat login exception .'
618
+ print ( '[ERROR] WeChat login exception .' )
616
619
return False
617
620
618
621
def login (self ):
@@ -693,7 +696,7 @@ def sync_check(self):
693
696
'synckey' : self .sync_key_str ,
694
697
'_' : int (time .time ()),
695
698
}
696
- url = 'https://' + self .sync_host + '.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?' + urllib .urlencode (params )
699
+ url = 'https://' + self .sync_host + '.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?' + urllib .parse . urlencode (params )
697
700
try :
698
701
r = self .session .get (url )
699
702
except (ConnectionError , ReadTimeout ):
0 commit comments