wxbot.py 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import print_function
  4. import os
  5. import sys
  6. import tempfile
  7. import traceback
  8. import webbrowser
  9. import binascii
  10. import jsonpickle
  11. import pyqrcode
  12. import requests
  13. import mimetypes
  14. import json
  15. import xml.dom.minidom
  16. import urllib
  17. import time
  18. import re
  19. import random
  20. from traceback import format_exc
  21. from requests.exceptions import ConnectionError, ReadTimeout
  22. import sys
  23. if sys.version_info[0] == 3:
  24. from html import parser as HTMLParser
  25. from urllib.parse import urlencode
  26. unicode = str
  27. else:
  28. import HTMLParser
  29. from urllib import urlencode
  30. import logging
  31. log = logging.getLogger('wxbot')
  32. UNKONWN = 'unkonwn'
  33. SUCCESS = '200'
  34. SCANED = '201'
  35. TIMEOUT = '408'
  36. def map_username_batch(user_name):
  37. return {"UserName": user_name, "EncryChatRoomId": ""}
  38. def show_image(file_path):
  39. """
  40. 跨平台显示图片文件
  41. :param file_path: 图片文件路径
  42. """
  43. if sys.version_info >= (3, 3):
  44. from shlex import quote
  45. else:
  46. from pipes import quote
  47. if sys.platform == "darwin":
  48. command = "open -a /Applications/Preview.app %s&" % quote(file_path)
  49. os.system(command)
  50. else:
  51. webbrowser.open(os.path.join(tempfile.gettempdir(), 'wxbot', file_path))
  52. class SafeSession(requests.Session):
  53. def request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None,
  54. timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None,
  55. json=None):
  56. for i in range(3):
  57. try:
  58. return super(SafeSession, self).request(method, url, params, data, headers, cookies, files, auth,
  59. timeout,
  60. allow_redirects, proxies, hooks, stream, verify, cert, json)
  61. except Exception as e:
  62. log.exception('request %s failed' % url)
  63. continue
  64. #重试3次以后再加一次,抛出异常
  65. try:
  66. return super(SafeSession, self).request(method, url, params, data, headers, cookies, files, auth,
  67. timeout,
  68. allow_redirects, proxies, hooks, stream, verify, cert, json)
  69. except Exception as e:
  70. raise e
  71. class WXBot:
  72. """WXBot功能类"""
  73. def __init__(self):
  74. self.DEBUG = False
  75. self.uuid = ''
  76. self.base_uri = ''
  77. self.base_host = ''
  78. self.redirect_uri = ''
  79. self.uin = ''
  80. self.sid = ''
  81. self.skey = ''
  82. self.pass_ticket = ''
  83. self.device_id = 'e' + repr(random.random())[2:17]
  84. self.base_request = {}
  85. self.sync_key_str = ''
  86. self.sync_key = []
  87. self.sync_host = ''
  88. self.batch_count = 50 #一次拉取50个联系人的信息
  89. self.full_user_name_list = [] #直接获取不到通讯录时,获取的username列表
  90. self.wxid_list = [] #获取到的wxid的列表
  91. self.cursor = 0 #拉取联系人信息的游标
  92. self.is_big_contact = False #通讯录人数过多,无法直接获取
  93. #文件缓存目录
  94. self.temp_pwd = os.path.join(tempfile.gettempdir(), 'wxbot')
  95. if not os.path.exists(self.temp_pwd):
  96. os.makedirs(self.temp_pwd)
  97. self.session = SafeSession()
  98. self.session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5'})
  99. self.conf = {'qr': 'png'}
  100. self.my_account = {} # 当前账户
  101. # 所有相关账号: 联系人, 公众号, 群组, 特殊账号
  102. self.member_list = []
  103. # 所有群组的成员, {'group_id1': [member1, member2, ...], ...}
  104. self.group_members = {}
  105. # 所有账户, {'group_member':{'id':{'type':'group_member', 'info':{}}, ...}, 'normal_member':{'id':{}, ...}}
  106. self.account_info = {'group_member': {}, 'normal_member': {}}
  107. self.contact_list = [] # 联系人列表
  108. self.public_list = [] # 公众账号列表
  109. self.group_list = [] # 群聊列表
  110. self.special_list = [] # 特殊账号列表
  111. self.encry_chat_room_id_list = [] # 存储群聊的EncryChatRoomId,获取群内成员头像时需要用到
  112. self.file_index = 0
  113. self.state_file = os.path.join(self.temp_pwd, 'session_state.json')
  114. @staticmethod
  115. def to_unicode(string, encoding='utf-8'):
  116. """
  117. 将字符串转换为Unicode
  118. :param string: 待转换字符串
  119. :param encoding: 字符串解码方式
  120. :return: 转换后的Unicode字符串
  121. """
  122. if isinstance(string, unicode):
  123. return string
  124. elif isinstance(string, str):
  125. return string.decode(encoding)
  126. else:
  127. raise Exception('Unknown Type')
  128. def get_contact(self):
  129. """获取当前账户的所有相关账号(包括联系人、公众号、群聊、特殊账号)"""
  130. if self.is_big_contact:
  131. return False
  132. url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' \
  133. % (self.pass_ticket, self.skey, int(time.time()))
  134. #如果通讯录联系人过多,这里会直接获取失败
  135. try:
  136. r = self.session.post(url, data='{}')
  137. except Exception as e:
  138. self.is_big_contact = True
  139. return False
  140. r.encoding = 'utf-8'
  141. if self.DEBUG:
  142. with open(os.path.join(self.temp_pwd,'contacts.json'), 'wb') as f:
  143. f.write(r.text.encode('utf-8'))
  144. dic = json.loads(r.text)
  145. self.member_list = dic['MemberList']
  146. special_users = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail',
  147. 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
  148. 'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp',
  149. 'blogapp', 'facebookapp', 'masssendapp', 'meishiapp',
  150. 'feedsapp', 'voip', 'blogappweixin', 'weixin', 'brandsessionholder',
  151. 'weixinreminder', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c',
  152. 'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11',
  153. 'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages']
  154. self.contact_list = []
  155. self.public_list = []
  156. self.special_list = []
  157. self.group_list = []
  158. for contact in self.member_list:
  159. if contact['VerifyFlag'] & 8 != 0: # 公众号
  160. self.public_list.append(contact)
  161. self.account_info['normal_member'][contact['UserName']] = {'type': 'public', 'info': contact}
  162. elif contact['UserName'] in special_users: # 特殊账户
  163. self.special_list.append(contact)
  164. self.account_info['normal_member'][contact['UserName']] = {'type': 'special', 'info': contact}
  165. elif contact['UserName'].find('@@') != -1: # 群聊
  166. self.group_list.append(contact)
  167. self.account_info['normal_member'][contact['UserName']] = {'type': 'group', 'info': contact}
  168. elif contact['UserName'] == self.my_account['UserName']: # 自己
  169. self.account_info['normal_member'][contact['UserName']] = {'type': 'self', 'info': contact}
  170. else:
  171. self.contact_list.append(contact)
  172. self.account_info['normal_member'][contact['UserName']] = {'type': 'contact', 'info': contact}
  173. self.batch_get_group_members()
  174. for group in self.group_members:
  175. for member in self.group_members[group]:
  176. if member['UserName'] not in self.account_info:
  177. self.account_info['group_member'][member['UserName']] = \
  178. {'type': 'group_member', 'info': member, 'group': group}
  179. if self.DEBUG:
  180. with open(os.path.join(self.temp_pwd,'contact_list.json'), 'w') as f:
  181. f.write(json.dumps(self.contact_list))
  182. with open(os.path.join(self.temp_pwd,'special_list.json'), 'w') as f:
  183. f.write(json.dumps(self.special_list))
  184. with open(os.path.join(self.temp_pwd,'group_list.json'), 'w') as f:
  185. f.write(json.dumps(self.group_list))
  186. with open(os.path.join(self.temp_pwd,'public_list.json'), 'w') as f:
  187. f.write(json.dumps(self.public_list))
  188. with open(os.path.join(self.temp_pwd,'member_list.json'), 'w') as f:
  189. f.write(json.dumps(self.member_list))
  190. with open(os.path.join(self.temp_pwd,'group_users.json'), 'w') as f:
  191. f.write(json.dumps(self.group_members))
  192. with open(os.path.join(self.temp_pwd,'account_info.json'), 'w') as f:
  193. f.write(json.dumps(self.account_info))
  194. return True
  195. def get_big_contact(self):
  196. total_len = len(self.full_user_name_list)
  197. user_info_list = []
  198. #一次拉取50个联系人的信息,包括所有的群聊,公众号,好友
  199. while self.cursor < total_len:
  200. cur_batch = self.full_user_name_list[self.cursor:(self.cursor+self.batch_count)]
  201. self.cursor += self.batch_count
  202. cur_batch = map(map_username_batch, cur_batch)
  203. user_info_list += self.batch_get_contact(cur_batch)
  204. log.info("Get batch contacts")
  205. self.member_list = user_info_list
  206. special_users = ['newsapp', 'filehelper', 'weibo', 'qqmail',
  207. 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle',
  208. 'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp',
  209. 'blogapp', 'facebookapp', 'masssendapp', 'meishiapp',
  210. 'feedsapp', 'voip', 'blogappweixin', 'weixin', 'brandsessionholder',
  211. 'weixinreminder', 'wxid_novlwrv3lqwv11',
  212. 'officialaccounts',
  213. 'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages', 'notifymessage']
  214. self.contact_list = []
  215. self.public_list = []
  216. self.special_list = []
  217. self.group_list = []
  218. for i, contact in enumerate(self.member_list):
  219. if contact['VerifyFlag'] & 8 != 0: # 公众号
  220. self.public_list.append(contact)
  221. self.account_info['normal_member'][contact['UserName']] = {'type': 'public', 'info': contact}
  222. elif contact['UserName'] in special_users or self.wxid_list[i] in special_users: # 特殊账户
  223. self.special_list.append(contact)
  224. self.account_info['normal_member'][contact['UserName']] = {'type': 'special', 'info': contact}
  225. elif contact['UserName'].find('@@') != -1: # 群聊
  226. self.group_list.append(contact)
  227. self.account_info['normal_member'][contact['UserName']] = {'type': 'group', 'info': contact}
  228. elif contact['UserName'] == self.my_account['UserName']: # 自己
  229. self.account_info['normal_member'][contact['UserName']] = {'type': 'self', 'info': contact}
  230. else:
  231. self.contact_list.append(contact)
  232. self.account_info['normal_member'][contact['UserName']] = {'type': 'contact', 'info': contact}
  233. group_members = {}
  234. encry_chat_room_id = {}
  235. for group in self.group_list:
  236. gid = group['UserName']
  237. members = group['MemberList']
  238. group_members[gid] = members
  239. encry_chat_room_id[gid] = group['EncryChatRoomId']
  240. self.group_members = group_members
  241. self.encry_chat_room_id_list = encry_chat_room_id
  242. for group in self.group_members:
  243. for member in self.group_members[group]:
  244. if member['UserName'] not in self.account_info:
  245. self.account_info['group_member'][member['UserName']] = \
  246. {'type': 'group_member', 'info': member, 'group': group}
  247. if self.DEBUG:
  248. with open(os.path.join(self.temp_pwd,'contact_list.json'), 'w') as f:
  249. f.write(json.dumps(self.contact_list))
  250. with open(os.path.join(self.temp_pwd,'special_list.json'), 'w') as f:
  251. f.write(json.dumps(self.special_list))
  252. with open(os.path.join(self.temp_pwd,'group_list.json'), 'w') as f:
  253. f.write(json.dumps(self.group_list))
  254. with open(os.path.join(self.temp_pwd,'public_list.json'), 'w') as f:
  255. f.write(json.dumps(self.public_list))
  256. with open(os.path.join(self.temp_pwd,'member_list.json'), 'w') as f:
  257. f.write(json.dumps(self.member_list))
  258. with open(os.path.join(self.temp_pwd,'group_users.json'), 'w') as f:
  259. f.write(json.dumps(self.group_members))
  260. with open(os.path.join(self.temp_pwd,'account_info.json'), 'w') as f:
  261. f.write(json.dumps(self.account_info))
  262. log.info('Get %d contacts' % len(self.contact_list))
  263. log.info('Start to process messages .')
  264. return True
  265. def batch_get_contact(self, cur_batch):
  266. """批量获取成员信息"""
  267. url = self.base_uri + '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s' % (int(time.time()), self.pass_ticket)
  268. params = {
  269. 'BaseRequest': self.base_request,
  270. "Count": len(cur_batch),
  271. "List": cur_batch
  272. }
  273. r = self.session.post(url, data=json.dumps(params))
  274. r.encoding = 'utf-8'
  275. dic = json.loads(r.text)
  276. #print dic['ContactList']
  277. return dic['ContactList']
  278. def batch_get_group_members(self):
  279. """批量获取所有群聊成员信息"""
  280. url = self.base_uri + '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s' % (int(time.time()), self.pass_ticket)
  281. params = {
  282. 'BaseRequest': self.base_request,
  283. "Count": len(self.group_list),
  284. "List": [{"UserName": group['UserName'], "EncryChatRoomId": ""} for group in self.group_list]
  285. }
  286. r = self.session.post(url, data=json.dumps(params))
  287. r.encoding = 'utf-8'
  288. dic = json.loads(r.text)
  289. group_members = {}
  290. encry_chat_room_id = {}
  291. for group in dic['ContactList']:
  292. gid = group['UserName']
  293. members = group['MemberList']
  294. group_members[gid] = members
  295. encry_chat_room_id[gid] = group['EncryChatRoomId']
  296. self.group_members = group_members
  297. self.encry_chat_room_id_list = encry_chat_room_id
  298. def get_group_member_name(self, gid, uid):
  299. """
  300. 获取群聊中指定成员的名称信息
  301. :param gid: 群id
  302. :param uid: 群聊成员id
  303. :return: 名称信息,类似 {"display_name": "test_user", "nickname": "test", "remark_name": "for_test" }
  304. """
  305. if gid not in self.group_members:
  306. return None
  307. group = self.group_members[gid]
  308. for member in group:
  309. if member['UserName'] == uid:
  310. names = {}
  311. if 'RemarkName' in member and member['RemarkName']:
  312. names['remark_name'] = member['RemarkName']
  313. if 'NickName' in member and member['NickName']:
  314. names['nickname'] = member['NickName']
  315. if 'DisplayName' in member and member['DisplayName']:
  316. names['display_name'] = member['DisplayName']
  317. return names
  318. return None
  319. def get_contact_info(self, uid):
  320. return self.account_info['normal_member'].get(uid)
  321. def get_group_member_info(self, uid):
  322. return self.account_info['group_member'].get(uid)
  323. def get_contact_name(self, uid):
  324. info = self.get_contact_info(uid)
  325. if info is None:
  326. return None
  327. info = info['info']
  328. name = {}
  329. if 'RemarkName' in info and info['RemarkName']:
  330. name['remark_name'] = info['RemarkName']
  331. if 'NickName' in info and info['NickName']:
  332. name['nickname'] = info['NickName']
  333. if 'DisplayName' in info and info['DisplayName']:
  334. name['display_name'] = info['DisplayName']
  335. if len(name) == 0:
  336. return None
  337. else:
  338. return name
  339. @staticmethod
  340. def get_contact_prefer_name(name):
  341. if name is None:
  342. return None
  343. if 'remark_name' in name:
  344. return name['remark_name']
  345. if 'nickname' in name:
  346. return name['nickname']
  347. if 'display_name' in name:
  348. return name['display_name']
  349. return None
  350. @staticmethod
  351. def get_group_member_prefer_name(name):
  352. if name is None:
  353. return None
  354. if 'remark_name' in name:
  355. return name['remark_name']
  356. if 'display_name' in name:
  357. return name['display_name']
  358. if 'nickname' in name:
  359. return name['nickname']
  360. return None
  361. def get_user_type(self, wx_user_id):
  362. """
  363. 获取特定账号与自己的关系
  364. :param wx_user_id: 账号id:
  365. :return: 与当前账号的关系
  366. """
  367. for account in self.contact_list:
  368. if wx_user_id == account['UserName']:
  369. return 'contact'
  370. for account in self.public_list:
  371. if wx_user_id == account['UserName']:
  372. return 'public'
  373. for account in self.special_list:
  374. if wx_user_id == account['UserName']:
  375. return 'special'
  376. for account in self.group_list:
  377. if wx_user_id == account['UserName']:
  378. return 'group'
  379. for group in self.group_members:
  380. for member in self.group_members[group]:
  381. if member['UserName'] == wx_user_id:
  382. return 'group_member'
  383. return 'unknown'
  384. def is_contact(self, uid):
  385. for account in self.contact_list:
  386. if uid == account['UserName']:
  387. return True
  388. return False
  389. def is_public(self, uid):
  390. for account in self.public_list:
  391. if uid == account['UserName']:
  392. return True
  393. return False
  394. def is_special(self, uid):
  395. for account in self.special_list:
  396. if uid == account['UserName']:
  397. return True
  398. return False
  399. def handle_msg_all(self, msg):
  400. """
  401. 处理所有消息,请子类化后覆盖此函数
  402. msg:
  403. msg_id -> 消息id
  404. msg_type_id -> 消息类型id
  405. user -> 发送消息的账号id
  406. content -> 消息内容
  407. :param msg: 收到的消息
  408. """
  409. pass
  410. @staticmethod
  411. def proc_at_info(msg):
  412. if not msg:
  413. return '', []
  414. segs = msg.split(u'\u2005')
  415. str_msg_all = ''
  416. str_msg = ''
  417. infos = []
  418. if len(segs) > 1:
  419. for i in range(0, len(segs) - 1):
  420. segs[i] += u'\u2005'
  421. pm = re.search(u'@.*\u2005', segs[i]).group()
  422. if pm:
  423. name = pm[1:-1]
  424. string = segs[i].replace(pm, '')
  425. str_msg_all += string + '@' + name + ' '
  426. str_msg += string
  427. if string:
  428. infos.append({'type': 'str', 'value': string})
  429. infos.append({'type': 'at', 'value': name})
  430. else:
  431. infos.append({'type': 'str', 'value': segs[i]})
  432. str_msg_all += segs[i]
  433. str_msg += segs[i]
  434. str_msg_all += segs[-1]
  435. str_msg += segs[-1]
  436. infos.append({'type': 'str', 'value': segs[-1]})
  437. else:
  438. infos.append({'type': 'str', 'value': segs[-1]})
  439. str_msg_all = msg
  440. str_msg = msg
  441. return str_msg_all.replace(u'\u2005', ''), str_msg.replace(u'\u2005', ''), infos
  442. def extract_msg_content(self, msg_type_id, msg):
  443. """
  444. content_type_id:
  445. 0 -> Text
  446. 1 -> Location
  447. 3 -> Image
  448. 4 -> Voice
  449. 5 -> Recommend
  450. 6 -> Animation
  451. 7 -> Share
  452. 8 -> Video
  453. 9 -> VideoCall
  454. 10 -> Redraw
  455. 11 -> Empty
  456. 99 -> Unknown
  457. :param msg_type_id: 消息类型id
  458. :param msg: 消息结构体
  459. :return: 解析的消息
  460. """
  461. mtype = msg['MsgType']
  462. content = HTMLParser.HTMLParser().unescape(msg['Content'])
  463. msg_id = msg['MsgId']
  464. msg_content = {}
  465. if msg_type_id == 0:
  466. return {'type': 11, 'data': ''}
  467. elif msg_type_id == 2: # File Helper
  468. return {'type': 0, 'data': content.replace('<br/>', '\n')}
  469. elif msg_type_id == 3: # 群聊
  470. sp = content.find('<br/>')
  471. uid = content[:sp]
  472. content = content[sp:]
  473. content = content.replace('<br/>', '')
  474. uid = uid[:-1]
  475. name = self.get_contact_prefer_name(self.get_contact_name(uid))
  476. if not name:
  477. name = self.get_group_member_prefer_name(self.get_group_member_name(msg['FromUserName'], uid))
  478. if not name:
  479. name = 'unknown'
  480. msg_content['user'] = {'id': uid, 'name': name}
  481. else: # Self, Contact, Special, Public, Unknown
  482. pass
  483. msg_prefix = (msg_content['user']['name'] + ':') if 'user' in msg_content else ''
  484. if mtype == 1:
  485. if content.find('http://weixin.qq.com/cgi-bin/redirectforward?args=') != -1:
  486. r = self.session.get(content)
  487. r.encoding = 'gbk'
  488. data = r.text
  489. pos = self.search_content('title', data, 'xml')
  490. msg_content['type'] = 1
  491. msg_content['data'] = pos
  492. msg_content['detail'] = data
  493. if self.DEBUG:
  494. log.info(' %s[Location] %s ' % (msg_prefix, pos))
  495. else:
  496. msg_content['type'] = 0
  497. if msg_type_id == 3 or (msg_type_id == 1 and msg['ToUserName'][:2] == '@@'): # Group text message
  498. msg_infos = self.proc_at_info(content)
  499. str_msg_all = msg_infos[0]
  500. str_msg = msg_infos[1]
  501. detail = msg_infos[2]
  502. msg_content['data'] = str_msg_all
  503. msg_content['detail'] = detail
  504. msg_content['desc'] = str_msg
  505. else:
  506. msg_content['data'] = content
  507. if self.DEBUG:
  508. try:
  509. log.info(' %s[Text] %s' % (msg_prefix, msg_content['data']))
  510. except UnicodeEncodeError:
  511. log.info(' %s[Text] (illegal text).' % msg_prefix)
  512. elif mtype == 3:
  513. msg_content['type'] = 3
  514. msg_content['data'] = self.get_msg_img_url(msg_id)
  515. msg_content['img'] = binascii.hexlify(self.session.get(msg_content['data']).content)
  516. if self.DEBUG:
  517. image = self.get_msg_img(msg_id)
  518. log.info(' %s[Image] %s' % (msg_prefix, image))
  519. elif mtype == 34:
  520. msg_content['type'] = 4
  521. msg_content['data'] = self.get_voice_url(msg_id)
  522. msg_content['voice'] = binascii.hexlify(self.session.get(msg_content['data']).content)
  523. if self.DEBUG:
  524. voice = self.get_voice(msg_id)
  525. log.info(' %s[Voice] %s' % (msg_prefix, voice))
  526. elif mtype == 37:
  527. msg_content['type'] = 37
  528. msg_content['data'] = msg['RecommendInfo']
  529. if self.DEBUG:
  530. log.info(' %s[useradd] %s' % (msg_prefix,msg['RecommendInfo']['NickName']))
  531. elif mtype == 42:
  532. msg_content['type'] = 5
  533. info = msg['RecommendInfo']
  534. msg_content['data'] = {'nickname': info['NickName'],
  535. 'alias': info['Alias'],
  536. 'province': info['Province'],
  537. 'city': info['City'],
  538. 'gender': ['unknown', 'male', 'female'][info['Sex']]}
  539. if self.DEBUG:
  540. log.info(' %s[Recommend]' % msg_prefix)
  541. log.info(' -----------------------------')
  542. log.info(' | NickName: %s' % info['NickName'])
  543. log.info(' | Alias: %s' % info['Alias'])
  544. log.info(' | Local: %s %s' % (info['Province'], info['City']))
  545. log.info(' | Gender: %s' % ['unknown', 'male', 'female'][info['Sex']])
  546. log.info(' -----------------------------')
  547. elif mtype == 47:
  548. msg_content['type'] = 6
  549. msg_content['data'] = self.search_content('cdnurl', content)
  550. if self.DEBUG:
  551. log.info(' %s[Animation] %s' % (msg_prefix, msg_content['data']))
  552. elif mtype == 49:
  553. msg_content['type'] = 7
  554. if msg['AppMsgType'] == 3:
  555. app_msg_type = 'music'
  556. elif msg['AppMsgType'] == 5:
  557. app_msg_type = 'link'
  558. elif msg['AppMsgType'] == 7:
  559. app_msg_type = 'weibo'
  560. else:
  561. app_msg_type = 'unknown'
  562. msg_content['data'] = {'type': app_msg_type,
  563. 'title': msg['FileName'],
  564. 'desc': self.search_content('des', content, 'xml'),
  565. 'url': msg['Url'],
  566. 'from': self.search_content('appname', content, 'xml'),
  567. 'content': msg.get('Content') # 有的公众号会发一次性3 4条链接一个大图,如果只url那只能获取第一条,content里面有所有的链接
  568. }
  569. if self.DEBUG:
  570. log.info(' %s[Share] %s' % (msg_prefix, app_msg_type))
  571. log.info(' --------------------------')
  572. log.info(' | title: %s' % msg['FileName'])
  573. log.info(' | desc: %s' % self.search_content('des', content, 'xml'))
  574. log.info(' | link: %s' % msg['Url'])
  575. log.info(' | from: %s' % self.search_content('appname', content, 'xml'))
  576. log.info(' | content: %s' % (msg.get('content')[:20] if msg.get('content') else "unknown"))
  577. log.info(' --------------------------')
  578. elif mtype == 62:
  579. msg_content['type'] = 8
  580. msg_content['data'] = content
  581. if self.DEBUG:
  582. log.info(' %s[Video] Please check on mobiles' % msg_prefix)
  583. elif mtype == 53:
  584. msg_content['type'] = 9
  585. msg_content['data'] = content
  586. if self.DEBUG:
  587. log.info(' %s[Video Call]' % msg_prefix)
  588. elif mtype == 10002:
  589. msg_content['type'] = 10
  590. msg_content['data'] = content
  591. if self.DEBUG:
  592. log.info(' %s[Redraw]' % msg_prefix)
  593. elif mtype == 10000: # unknown, maybe red packet, or group invite
  594. msg_content['type'] = 12
  595. msg_content['data'] = msg['Content']
  596. if self.DEBUG:
  597. log.info(' [Unknown]')
  598. elif mtype == 43:
  599. msg_content['type'] = 13
  600. msg_content['data'] = self.get_video_url(msg_id)
  601. if self.DEBUG:
  602. log.info(' %s[video] %s' % (msg_prefix, msg_content['data']))
  603. else:
  604. msg_content['type'] = 99
  605. msg_content['data'] = content
  606. if self.DEBUG:
  607. log.info(' %s[Unknown]' % msg_prefix)
  608. return msg_content
  609. def handle_msg(self, r):
  610. """
  611. 处理原始微信消息的内部函数
  612. msg_type_id:
  613. 0 -> Init
  614. 1 -> Self
  615. 2 -> FileHelper
  616. 3 -> Group
  617. 4 -> Contact
  618. 5 -> Public
  619. 6 -> Special
  620. 99 -> Unknown
  621. :param r: 原始微信消息
  622. """
  623. for msg in r['AddMsgList']:
  624. user = {'id': msg['FromUserName'], 'name': 'unknown'}
  625. if msg['MsgType'] == 51 and msg['StatusNotifyCode'] == 4: # init message
  626. msg_type_id = 0
  627. user['name'] = 'system'
  628. #会获取所有联系人的username 和 wxid,但是会收到3次这个消息,只取第一次
  629. if self.is_big_contact and len(self.full_user_name_list) == 0:
  630. self.full_user_name_list = msg['StatusNotifyUserName'].split(",")
  631. self.wxid_list = re.search(r"username&gt;(.*?)&lt;/username", msg["Content"]).group(1).split(",")
  632. with open(os.path.join(self.temp_pwd,'UserName.txt'), 'w') as f:
  633. f.write(msg['StatusNotifyUserName'])
  634. with open(os.path.join(self.temp_pwd,'wxid.txt'), 'w') as f:
  635. f.write(json.dumps(self.wxid_list))
  636. log.info('Contact list is too big. Now start to fetch member list .')
  637. self.get_big_contact()
  638. elif msg['MsgType'] == 37: # friend request
  639. msg_type_id = 37
  640. pass
  641. # content = msg['Content']
  642. # username = content[content.index('fromusername='): content.index('encryptusername')]
  643. # username = username[username.index('"') + 1: username.rindex('"')]
  644. # print u'[Friend Request]'
  645. # print u' Nickname:' + msg['RecommendInfo']['NickName']
  646. # print u' 附加消息:'+msg['RecommendInfo']['Content']
  647. # # print u'Ticket:'+msg['RecommendInfo']['Ticket'] # Ticket添加好友时要用
  648. # print u' 微信号:'+username #未设置微信号的 腾讯会自动生成一段微信ID 但是无法通过搜索 搜索到此人
  649. elif msg['FromUserName'] == self.my_account['UserName']: # Self
  650. msg_type_id = 1
  651. user['name'] = 'self'
  652. elif msg['ToUserName'] == 'filehelper': # File Helper
  653. msg_type_id = 2
  654. user['name'] = 'file_helper'
  655. elif msg['FromUserName'][:2] == '@@': # Group
  656. msg_type_id = 3
  657. user['name'] = self.get_contact_prefer_name(self.get_contact_name(user['id']))
  658. elif self.is_contact(msg['FromUserName']): # Contact
  659. msg_type_id = 4
  660. user['name'] = self.get_contact_prefer_name(self.get_contact_name(user['id']))
  661. elif self.is_public(msg['FromUserName']): # Public
  662. msg_type_id = 5
  663. user['name'] = self.get_contact_prefer_name(self.get_contact_name(user['id']))
  664. elif self.is_special(msg['FromUserName']): # Special
  665. msg_type_id = 6
  666. user['name'] = self.get_contact_prefer_name(self.get_contact_name(user['id']))
  667. else:
  668. msg_type_id = 99
  669. user['name'] = 'unknown'
  670. if not user['name']:
  671. user['name'] = 'unknown'
  672. user['name'] = HTMLParser.HTMLParser().unescape(user['name'])
  673. if self.DEBUG and msg_type_id != 0:
  674. log.info(u'[MSG] %s:' % user['name'])
  675. content = self.extract_msg_content(msg_type_id, msg)
  676. message = {'msg_type_id': msg_type_id,
  677. 'msg_id': msg['MsgId'],
  678. 'content': content,
  679. 'to_user_id': msg['ToUserName'],
  680. 'user': user}
  681. self.handle_msg_all(message)
  682. def schedule(self):
  683. """
  684. 做任务型事情的函数,如果需要,可以在子类中覆盖此函数
  685. 此函数在处理消息的间隙被调用,请不要长时间阻塞此函数
  686. """
  687. pass
  688. def proc_msg(self):
  689. self.test_sync_check()
  690. while True:
  691. check_time = time.time()
  692. try:
  693. [retcode, selector] = self.sync_check()
  694. # log.debug('sync_check:', retcode, selector)
  695. if retcode == '1100': # 从微信客户端上登出
  696. break
  697. elif retcode == '1101': # 从其它设备上登了网页微信
  698. break
  699. elif retcode == '0':
  700. if selector == '2': # 有新消息
  701. r = self.sync()
  702. if r is not None:
  703. self.handle_msg(r)
  704. elif selector == '3': # 未知
  705. r = self.sync()
  706. if r is not None:
  707. self.handle_msg(r)
  708. elif selector == '4': # 通讯录更新
  709. r = self.sync()
  710. if r is not None:
  711. self.get_contact()
  712. elif selector == '6': # 可能是红包
  713. r = self.sync()
  714. if r is not None:
  715. self.handle_msg(r)
  716. elif selector == '7': # 在手机上操作了微信
  717. r = self.sync()
  718. if r is not None:
  719. self.handle_msg(r)
  720. elif selector == '0': # 无事件
  721. pass
  722. else:
  723. log.debug('sync_check:', retcode, selector)
  724. r = self.sync()
  725. if r is not None:
  726. self.handle_msg(r)
  727. else:
  728. log.debug('sync_check:', retcode, selector)
  729. time.sleep(10)
  730. self.schedule()
  731. except:
  732. log.exception('Except in proc_msg')
  733. check_time = time.time() - check_time
  734. if check_time < 0.8:
  735. time.sleep(1 - check_time)
  736. def apply_useradd_requests(self,RecommendInfo):
  737. url = self.base_uri + '/webwxverifyuser?r='+str(int(time.time()))+'&lang=zh_CN'
  738. params = {
  739. "BaseRequest": self.base_request,
  740. "Opcode": 3,
  741. "VerifyUserListSize": 1,
  742. "VerifyUserList": [
  743. {
  744. "Value": RecommendInfo['UserName'],
  745. "VerifyUserTicket": RecommendInfo['Ticket'] }
  746. ],
  747. "VerifyContent": "",
  748. "SceneListCount": 1,
  749. "SceneList": [
  750. 33
  751. ],
  752. "skey": self.skey
  753. }
  754. headers = {'content-type': 'application/json; charset=UTF-8'}
  755. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  756. try:
  757. r = self.session.post(url, data=data, headers=headers)
  758. except (ConnectionError, ReadTimeout):
  759. return False
  760. dic = r.json()
  761. return dic['BaseResponse']['Ret'] == 0
  762. def add_groupuser_to_friend_by_uid(self,uid,VerifyContent):
  763. """
  764. 主动向群内人员打招呼,提交添加好友请求
  765. uid-群内人员得uid VerifyContent-好友招呼内容
  766. 慎用此接口!封号后果自负!慎用此接口!封号后果自负!慎用此接口!封号后果自负!
  767. """
  768. if self.is_contact(uid):
  769. return True
  770. url = self.base_uri + '/webwxverifyuser?r='+str(int(time.time()))+'&lang=zh_CN'
  771. params ={
  772. "BaseRequest": self.base_request,
  773. "Opcode": 2,
  774. "VerifyUserListSize": 1,
  775. "VerifyUserList": [
  776. {
  777. "Value": uid,
  778. "VerifyUserTicket": ""
  779. }
  780. ],
  781. "VerifyContent": VerifyContent,
  782. "SceneListCount": 1,
  783. "SceneList": [
  784. 33
  785. ],
  786. "skey": self.skey
  787. }
  788. headers = {'content-type': 'application/json; charset=UTF-8'}
  789. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  790. try:
  791. r = self.session.post(url, data=data, headers=headers)
  792. except (ConnectionError, ReadTimeout):
  793. return False
  794. dic = r.json()
  795. return dic['BaseResponse']['Ret'] == 0
  796. def add_friend_to_group(self,uid,group_name):
  797. """
  798. 将好友加入到群聊中
  799. """
  800. gid = ''
  801. #通过群名获取群id,群没保存到通讯录中的话无法添加哦
  802. for group in self.group_list:
  803. if group['NickName'] == group_name:
  804. gid = group['UserName']
  805. if gid == '':
  806. return False
  807. #通过群id判断uid是否在群中
  808. for user in self.group_members[gid]:
  809. if user['UserName'] == uid:
  810. #已经在群里面了,不用加了
  811. return True
  812. url = self.base_uri + '/webwxupdatechatroom?fun=addmember&pass_ticket=%s' % self.pass_ticket
  813. params ={
  814. "AddMemberList": uid,
  815. "ChatRoomName": gid,
  816. "BaseRequest": self.base_request
  817. }
  818. headers = {'content-type': 'application/json; charset=UTF-8'}
  819. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  820. try:
  821. r = self.session.post(url, data=data, headers=headers)
  822. except (ConnectionError, ReadTimeout):
  823. return False
  824. dic = r.json()
  825. return dic['BaseResponse']['Ret'] == 0
  826. def invite_friend_to_group(self,uid,group_name):
  827. """
  828. 将好友加入到群中。对人数多的群,需要调用此方法。
  829. 拉人时,可以先尝试使用add_friend_to_group方法,当调用失败(Ret=1)时,再尝试调用此方法。
  830. """
  831. gid = ''
  832. # 通过群名获取群id,群没保存到通讯录中的话无法添加哦
  833. for group in self.group_list:
  834. if group['NickName'] == group_name:
  835. gid = group['UserName']
  836. if gid == '':
  837. return False
  838. # 通过群id判断uid是否在群中
  839. for user in self.group_members[gid]:
  840. if user['UserName'] == uid:
  841. # 已经在群里面了,不用加了
  842. return True
  843. url = self.base_uri + '/webwxupdatechatroom?fun=invitemember&pass_ticket=%s' % self.pass_ticket
  844. params = {
  845. "InviteMemberList": uid,
  846. "ChatRoomName": gid,
  847. "BaseRequest": self.base_request
  848. }
  849. headers = {'content-type': 'application/json; charset=UTF-8'}
  850. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  851. try:
  852. r = self.session.post(url, data=data, headers=headers)
  853. except (ConnectionError, ReadTimeout):
  854. return False
  855. dic = r.json()
  856. return dic['BaseResponse']['Ret'] == 0
  857. def delete_user_from_group(self,uname,gid):
  858. """
  859. 将群用户从群中剔除,只有群管理员有权限
  860. """
  861. uid = ""
  862. for user in self.group_members[gid]:
  863. if user['NickName'] == uname:
  864. uid = user['UserName']
  865. if uid == "":
  866. return False
  867. url = self.base_uri + '/webwxupdatechatroom?fun=delmember&pass_ticket=%s' % self.pass_ticket
  868. params ={
  869. "DelMemberList": uid,
  870. "ChatRoomName": gid,
  871. "BaseRequest": self.base_request
  872. }
  873. headers = {'content-type': 'application/json; charset=UTF-8'}
  874. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  875. try:
  876. r = self.session.post(url, data=data, headers=headers)
  877. except (ConnectionError, ReadTimeout):
  878. return False
  879. dic = r.json()
  880. return dic['BaseResponse']['Ret'] == 0
  881. def set_group_name(self,gid,gname):
  882. """
  883. 设置群聊名称
  884. """
  885. url = self.base_uri + '/webwxupdatechatroom?fun=modtopic&pass_ticket=%s' % self.pass_ticket
  886. params ={
  887. "NewTopic": gname,
  888. "ChatRoomName": gid,
  889. "BaseRequest": self.base_request
  890. }
  891. headers = {'content-type': 'application/json; charset=UTF-8'}
  892. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  893. try:
  894. r = self.session.post(url, data=data, headers=headers)
  895. except (ConnectionError, ReadTimeout):
  896. return False
  897. dic = r.json()
  898. return dic['BaseResponse']['Ret'] == 0
  899. def send_msg_by_uid(self, word, dst='filehelper'):
  900. url = self.base_uri + '/webwxsendmsg?pass_ticket=%s' % self.pass_ticket
  901. msg_id = str(int(time.time() * 1000)) + str(random.random())[:5].replace('.', '')
  902. word = self.to_unicode(word)
  903. params = {
  904. 'BaseRequest': self.base_request,
  905. 'Msg': {
  906. "Type": 1,
  907. "Content": word,
  908. "FromUserName": self.my_account['UserName'],
  909. "ToUserName": dst,
  910. "LocalID": msg_id,
  911. "ClientMsgId": msg_id
  912. }
  913. }
  914. headers = {'content-type': 'application/json; charset=UTF-8'}
  915. data = json.dumps(params, ensure_ascii=False).encode('utf8')
  916. try:
  917. r = self.session.post(url, data=data, headers=headers)
  918. except (ConnectionError, ReadTimeout):
  919. return False
  920. dic = r.json()
  921. return dic['BaseResponse']['Ret'] == 0
  922. def upload_media(self, fpath, is_img=False):
  923. if not os.path.exists(fpath):
  924. log.error('File not exists.')
  925. return None
  926. url_1 = 'https://file.'+self.base_host+'/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'
  927. url_2 = 'https://file2.'+self.base_host+'/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'
  928. flen = str(os.path.getsize(fpath))
  929. ftype = mimetypes.guess_type(fpath)[0] or 'application/octet-stream'
  930. files = {
  931. 'id': (None, 'WU_FILE_%s' % str(self.file_index)),
  932. 'name': (None, os.path.basename(fpath)),
  933. 'type': (None, ftype),
  934. 'lastModifiedDate': (None, time.strftime('%m/%d/%Y, %H:%M:%S GMT+0800 (CST)')),
  935. 'size': (None, flen),
  936. 'mediatype': (None, 'pic' if is_img else 'doc'),
  937. 'uploadmediarequest': (None, json.dumps({
  938. 'BaseRequest': self.base_request,
  939. 'ClientMediaId': int(time.time()),
  940. 'TotalLen': flen,
  941. 'StartPos': 0,
  942. 'DataLen': flen,
  943. 'MediaType': 4,
  944. })),
  945. 'webwx_data_ticket': (None, self.session.cookies['webwx_data_ticket']),
  946. 'pass_ticket': (None, self.pass_ticket),
  947. 'filename': (os.path.basename(fpath), open(fpath, 'rb'),ftype.split('/')[1]),
  948. }
  949. self.file_index += 1
  950. try:
  951. r = self.session.post(url_1, files=files)
  952. if json.loads(r.text)['BaseResponse']['Ret'] != 0:
  953. # 当file返回值不为0时则为上传失败,尝试第二服务器上传
  954. r = self.session.post(url_2, files=files)
  955. if json.loads(r.text)['BaseResponse']['Ret'] != 0:
  956. log.error('Upload media failure.')
  957. return None
  958. mid = json.loads(r.text)['MediaId']
  959. return mid
  960. except:
  961. return None
  962. def send_file_msg_by_uid(self, fpath, uid):
  963. mid = self.upload_media(fpath)
  964. if mid is None or not mid:
  965. return False
  966. url = self.base_uri + '/webwxsendappmsg?fun=async&f=json&pass_ticket=' + self.pass_ticket
  967. msg_id = str(int(time.time() * 1000)) + str(random.random())[:5].replace('.', '')
  968. data = {
  969. 'BaseRequest': self.base_request,
  970. 'Msg': {
  971. 'Type': 6,
  972. 'Content': ("<appmsg appid='wxeb7ec651dd0aefa9' sdkver=''><title>%s</title><des></des><action></action><type>6</type><content></content><url></url><lowurl></lowurl><appattach><totallen>%s</totallen><attachid>%s</attachid><fileext>%s</fileext></appattach><extinfo></extinfo></appmsg>" % (os.path.basename(fpath).encode('utf-8'), str(os.path.getsize(fpath)), mid, fpath.split('.')[-1])).encode('utf8'),
  973. 'FromUserName': self.my_account['UserName'],
  974. 'ToUserName': uid,
  975. 'LocalID': msg_id,
  976. 'ClientMsgId': msg_id, }, }
  977. try:
  978. r = self.session.post(url, data=json.dumps(data))
  979. res = json.loads(r.text)
  980. if res['BaseResponse']['Ret'] == 0:
  981. return True
  982. else:
  983. return False
  984. except:
  985. return False
  986. def send_img_msg_by_uid(self, fpath, uid):
  987. mid = self.upload_media(fpath, is_img=True)
  988. if mid is None:
  989. return False
  990. url = self.base_uri + '/webwxsendmsgimg?fun=async&f=json'
  991. data = {
  992. 'BaseRequest': self.base_request,
  993. 'Msg': {
  994. 'Type': 3,
  995. 'MediaId': mid,
  996. 'FromUserName': self.my_account['UserName'],
  997. 'ToUserName': uid,
  998. 'LocalID': str(time.time() * 1e7),
  999. 'ClientMsgId': str(time.time() * 1e7), }, }
  1000. if fpath[-4:] == '.gif':
  1001. url = self.base_uri + '/webwxsendemoticon?fun=sys'
  1002. data['Msg']['Type'] = 47
  1003. data['Msg']['EmojiFlag'] = 2
  1004. try:
  1005. r = self.session.post(url, data=json.dumps(data))
  1006. res = json.loads(r.text)
  1007. if res['BaseResponse']['Ret'] == 0:
  1008. return True
  1009. else:
  1010. return False
  1011. except:
  1012. return False
  1013. def get_user_id(self, name):
  1014. if name == '':
  1015. return None
  1016. name = self.to_unicode(name)
  1017. for contact in self.contact_list:
  1018. if 'RemarkName' in contact and contact['RemarkName'] == name:
  1019. return contact['UserName']
  1020. elif 'NickName' in contact and contact['NickName'] == name:
  1021. return contact['UserName']
  1022. elif 'DisplayName' in contact and contact['DisplayName'] == name:
  1023. return contact['UserName']
  1024. for group in self.group_list:
  1025. if 'RemarkName' in group and group['RemarkName'] == name:
  1026. return group['UserName']
  1027. if 'NickName' in group and group['NickName'] == name:
  1028. return group['UserName']
  1029. if 'DisplayName' in group and group['DisplayName'] == name:
  1030. return group['UserName']
  1031. return ''
  1032. def send_msg(self, name, word, isfile=False):
  1033. uid = self.get_user_id(name)
  1034. if uid is not None:
  1035. if isfile:
  1036. with open(word, 'r') as f:
  1037. result = True
  1038. for line in f.readlines():
  1039. line = line.replace('\n', '')
  1040. log.info('-> ' + name + ': ' + line)
  1041. if self.send_msg_by_uid(line, uid):
  1042. pass
  1043. else:
  1044. result = False
  1045. time.sleep(1)
  1046. return result
  1047. else:
  1048. word = self.to_unicode(word)
  1049. if self.send_msg_by_uid(word, uid):
  1050. return True
  1051. else:
  1052. return False
  1053. else:
  1054. if self.DEBUG:
  1055. log.error('This user does not exist .')
  1056. return True
  1057. @staticmethod
  1058. def search_content(key, content, fmat='attr'):
  1059. if fmat == 'attr':
  1060. pm = re.search(key + '\s?=\s?"([^"<]+)"', content)
  1061. if pm:
  1062. return pm.group(1)
  1063. elif fmat == 'xml':
  1064. pm = re.search('<{0}>([^<]+)</{0}>'.format(key), content)
  1065. if pm:
  1066. return pm.group(1)
  1067. return 'unknown'
  1068. def save_to_file(self):
  1069. with open(os.path.join(self.temp_pwd, 'session_state.json'), 'w') as fp:
  1070. json.dump({
  1071. 'uuid': self.uuid,
  1072. 'base_uri': self.base_uri,
  1073. 'base_host': self.base_host,
  1074. 'redirect_uri': self.redirect_uri,
  1075. 'uin': self.uin,
  1076. 'sid': self.sid,
  1077. 'skey': self.skey,
  1078. 'pass_ticket': self.pass_ticket,
  1079. 'device_id': self.device_id,
  1080. 'base_request': self.base_request,
  1081. 'session': jsonpickle.encode(self.session),
  1082. }, fp)
  1083. def load_from_file(self):
  1084. if not os.path.exists(self.state_file):
  1085. return False
  1086. try:
  1087. with open(self.state_file) as fp:
  1088. state = json.load(fp)
  1089. self.uuid = state['uuid']
  1090. self.base_uri = state['base_uri']
  1091. self.base_host = state['base_host']
  1092. self.redirect_uri = state['redirect_uri']
  1093. self.uin = state['uin']
  1094. self.sid = state['sid']
  1095. self.skey = state['skey']
  1096. self.pass_ticket = state['pass_ticket']
  1097. self.device_id = state['device_id']
  1098. self.base_request = state['base_request']
  1099. self.session = jsonpickle.decode(state['session'])
  1100. except:
  1101. log.exception('Failed to parse %s' % self.state_file)
  1102. return False
  1103. if self.init():
  1104. log.info('Web WeChat init succeed .')
  1105. return True
  1106. else:
  1107. log.info('Web WeChat init failed')
  1108. return False
  1109. def login(self):
  1110. self.get_uuid()
  1111. self.gen_qr_code(os.path.join(self.temp_pwd,'wxqr.png'))
  1112. log.info('Please use WeChat to scan the QR code .')
  1113. result = self.wait4login()
  1114. if result != SUCCESS:
  1115. log.error('Web WeChat login failed. failed code=%s' % (result,))
  1116. return False
  1117. if self._login():
  1118. log.info('Web WeChat login succeed .')
  1119. else:
  1120. log.error('Web WeChat login failed .')
  1121. return False
  1122. if self.init():
  1123. log.info('Web WeChat init succeed .')
  1124. self.save_to_file()
  1125. return True
  1126. else:
  1127. log.error('Web WeChat init failed')
  1128. return False
  1129. def run(self):
  1130. if not self.load_from_file() and not self.login():
  1131. log.error('Both recovered login status and fresh login are failed.')
  1132. return False
  1133. self.status_notify()
  1134. if self.get_contact():
  1135. log.info('Get %d contacts' % len(self.contact_list))
  1136. log.info('Start to process messages .')
  1137. self.proc_msg()
  1138. def get_uuid(self):
  1139. url = 'https://login.weixin.qq.com/jslogin'
  1140. params = {
  1141. 'appid': 'wx782c26e4c19acffb',
  1142. 'fun': 'new',
  1143. 'lang': 'zh_CN',
  1144. '_': int(time.time()) * 1000 + random.randint(1, 999),
  1145. }
  1146. r = self.session.get(url, params=params)
  1147. r.encoding = 'utf-8'
  1148. data = r.text
  1149. regx = r'window.QRLogin.code = (\d+); window.QRLogin.uuid = "(\S+?)"'
  1150. pm = re.search(regx, data)
  1151. if pm:
  1152. code = pm.group(1)
  1153. self.uuid = pm.group(2)
  1154. return code == '200'
  1155. return False
  1156. def gen_qr_code(self, qr_file_path):
  1157. string = 'https://login.weixin.qq.com/l/' + self.uuid
  1158. qr = pyqrcode.create(string)
  1159. if self.conf['qr'] == 'png':
  1160. qr.png(qr_file_path, scale=8)
  1161. show_image(qr_file_path)
  1162. # img = Image.open(qr_file_path)
  1163. # img.show()
  1164. elif self.conf['qr'] == 'tty':
  1165. print(qr.terminal(quiet_zone=1))
  1166. def do_request(self, url):
  1167. r = self.session.get(url)
  1168. r.encoding = 'utf-8'
  1169. data = r.text
  1170. param = re.search(r'window.code=(\d+);', data)
  1171. code = param.group(1)
  1172. return code, data
  1173. def wait4login(self):
  1174. """
  1175. http comet:
  1176. tip=1, 等待用户扫描二维码,
  1177. 201: scaned
  1178. 408: timeout
  1179. tip=0, 等待用户确认登录,
  1180. 200: confirmed
  1181. """
  1182. LOGIN_TEMPLATE = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s'
  1183. tip = 1
  1184. try_later_secs = 1
  1185. MAX_RETRY_TIMES = 10
  1186. code = UNKONWN
  1187. retry_time = MAX_RETRY_TIMES
  1188. while retry_time > 0:
  1189. url = LOGIN_TEMPLATE % (tip, self.uuid, int(time.time()))
  1190. code, data = self.do_request(url)
  1191. if code == SCANED:
  1192. log.info('Please confirm to login .')
  1193. tip = 0
  1194. elif code == SUCCESS: # 确认登录成功
  1195. param = re.search(r'window.redirect_uri="(\S+?)";', data)
  1196. redirect_uri = param.group(1) + '&fun=new'
  1197. self.redirect_uri = redirect_uri
  1198. self.base_uri = redirect_uri[:redirect_uri.rfind('/')]
  1199. temp_host = self.base_uri[8:]
  1200. self.base_host = temp_host[:temp_host.find("/")]
  1201. return code
  1202. elif code == TIMEOUT:
  1203. log.error('WeChat login timeout. retry in %s secs later...' % (try_later_secs,))
  1204. tip = 1 # 重置
  1205. retry_time -= 1
  1206. time.sleep(try_later_secs)
  1207. else:
  1208. log.error('WeChat login exception return_code=%s. retry in %s secs later...' %
  1209. (code, try_later_secs))
  1210. tip = 1
  1211. retry_time -= 1
  1212. time.sleep(try_later_secs)
  1213. return code
  1214. def _login(self):
  1215. if len(self.redirect_uri) < 4:
  1216. log.error('Login failed due to network problem, please try again.')
  1217. return False
  1218. r = self.session.get(self.redirect_uri)
  1219. r.encoding = 'utf-8'
  1220. data = r.text
  1221. doc = xml.dom.minidom.parseString(data)
  1222. root = doc.documentElement
  1223. for node in root.childNodes:
  1224. if node.nodeName == 'skey':
  1225. self.skey = node.childNodes[0].data
  1226. elif node.nodeName == 'wxsid':
  1227. self.sid = node.childNodes[0].data
  1228. elif node.nodeName == 'wxuin':
  1229. self.uin = node.childNodes[0].data
  1230. elif node.nodeName == 'pass_ticket':
  1231. self.pass_ticket = node.childNodes[0].data
  1232. if '' in (self.skey, self.sid, self.uin, self.pass_ticket):
  1233. return False
  1234. self.base_request = {
  1235. 'Uin': self.uin,
  1236. 'Sid': self.sid,
  1237. 'Skey': self.skey,
  1238. 'DeviceID': self.device_id,
  1239. }
  1240. return True
  1241. def init(self):
  1242. url = self.base_uri + '/webwxinit?r=%i&lang=en_US&pass_ticket=%s' % (int(time.time()), self.pass_ticket)
  1243. params = {
  1244. 'BaseRequest': self.base_request
  1245. }
  1246. r = self.session.post(url, data=json.dumps(params))
  1247. r.encoding = 'utf-8'
  1248. dic = json.loads(r.text)
  1249. self.sync_key = dic['SyncKey']
  1250. self.my_account = dic['User']
  1251. self.sync_key_str = '|'.join([str(keyVal['Key']) + '_' + str(keyVal['Val'])
  1252. for keyVal in self.sync_key['List']])
  1253. return dic['BaseResponse']['Ret'] == 0
  1254. def status_notify(self):
  1255. url = self.base_uri + '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s' % self.pass_ticket
  1256. self.base_request['Uin'] = int(self.base_request['Uin'])
  1257. params = {
  1258. 'BaseRequest': self.base_request,
  1259. "Code": 3,
  1260. "FromUserName": self.my_account['UserName'],
  1261. "ToUserName": self.my_account['UserName'],
  1262. "ClientMsgId": int(time.time())
  1263. }
  1264. r = self.session.post(url, data=json.dumps(params))
  1265. r.encoding = 'utf-8'
  1266. dic = json.loads(r.text)
  1267. return dic['BaseResponse']['Ret'] == 0
  1268. def test_sync_check(self):
  1269. for host1 in ['webpush.', 'webpush2.']:
  1270. self.sync_host = host1+self.base_host
  1271. try:
  1272. retcode = self.sync_check()[0]
  1273. except:
  1274. retcode = -1
  1275. if retcode == '0':
  1276. return True
  1277. return False
  1278. def sync_check(self):
  1279. params = {
  1280. 'r': int(time.time()),
  1281. 'sid': self.sid,
  1282. 'uin': self.uin,
  1283. 'skey': self.skey,
  1284. 'deviceid': self.device_id,
  1285. 'synckey': self.sync_key_str,
  1286. '_': int(time.time()),
  1287. }
  1288. url = 'https://' + self.sync_host + '/cgi-bin/mmwebwx-bin/synccheck?' + urlencode(params)
  1289. try:
  1290. r = self.session.get(url, timeout=60)
  1291. r.encoding = 'utf-8'
  1292. data = r.text
  1293. pm = re.search(r'window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}', data)
  1294. retcode = pm.group(1)
  1295. selector = pm.group(2)
  1296. return [retcode, selector]
  1297. except:
  1298. return [-1, -1]
  1299. def sync(self):
  1300. url = self.base_uri + '/webwxsync?sid=%s&skey=%s&lang=en_US&pass_ticket=%s' \
  1301. % (self.sid, self.skey, self.pass_ticket)
  1302. params = {
  1303. 'BaseRequest': self.base_request,
  1304. 'SyncKey': self.sync_key,
  1305. 'rr': ~int(time.time())
  1306. }
  1307. try:
  1308. r = self.session.post(url, data=json.dumps(params), timeout=60)
  1309. r.encoding = 'utf-8'
  1310. dic = json.loads(r.text)
  1311. if dic['BaseResponse']['Ret'] == 0:
  1312. self.sync_key = dic['SyncKey']
  1313. self.sync_key_str = '|'.join([str(keyVal['Key']) + '_' + str(keyVal['Val'])
  1314. for keyVal in self.sync_key['List']])
  1315. return dic
  1316. except:
  1317. return None
  1318. def get_icon(self, uid, gid=None):
  1319. """
  1320. 获取联系人或者群聊成员头像
  1321. :param uid: 联系人id
  1322. :param gid: 群id,如果为非None获取群中成员头像,如果为None则获取联系人头像
  1323. """
  1324. if gid is None:
  1325. url = self.base_uri + '/webwxgeticon?username=%s&skey=%s' % (uid, self.skey)
  1326. else:
  1327. url = self.base_uri + '/webwxgeticon?username=%s&skey=%s&chatroomid=%s' % (
  1328. uid, self.skey, self.encry_chat_room_id_list[gid])
  1329. r = self.session.get(url)
  1330. data = r.content
  1331. fn = 'icon_' + uid + '.jpg'
  1332. with open(os.path.join(self.temp_pwd,fn), 'wb') as f:
  1333. f.write(data)
  1334. return fn
  1335. def get_head_img(self, uid):
  1336. """
  1337. 获取群头像
  1338. :param uid: 群uid
  1339. """
  1340. url = self.base_uri + '/webwxgetheadimg?username=%s&skey=%s' % (uid, self.skey)
  1341. r = self.session.get(url)
  1342. data = r.content
  1343. fn = 'head_' + uid + '.jpg'
  1344. with open(os.path.join(self.temp_pwd,fn), 'wb') as f:
  1345. f.write(data)
  1346. return fn
  1347. def get_msg_img_url(self, msgid):
  1348. return self.base_uri + '/webwxgetmsgimg?MsgID=%s&skey=%s' % (msgid, self.skey)
  1349. def get_msg_img(self, msgid):
  1350. """
  1351. 获取图片消息,下载图片到本地
  1352. :param msgid: 消息id
  1353. :return: 保存的本地图片文件路径
  1354. """
  1355. url = self.base_uri + '/webwxgetmsgimg?MsgID=%s&skey=%s' % (msgid, self.skey)
  1356. r = self.session.get(url)
  1357. data = r.content
  1358. fn = 'img_' + msgid + '.jpg'
  1359. with open(os.path.join(self.temp_pwd,fn), 'wb') as f:
  1360. f.write(data)
  1361. return fn
  1362. def get_voice_url(self, msgid):
  1363. return self.base_uri + '/webwxgetvoice?msgid=%s&skey=%s' % (msgid, self.skey)
  1364. def get_voice(self, msgid):
  1365. """
  1366. 获取语音消息,下载语音到本地
  1367. :param msgid: 语音消息id
  1368. :return: 保存的本地语音文件路径
  1369. """
  1370. url = self.base_uri + '/webwxgetvoice?msgid=%s&skey=%s' % (msgid, self.skey)
  1371. r = self.session.get(url)
  1372. data = r.content
  1373. fn = 'voice_' + msgid + '.mp3'
  1374. with open(os.path.join(self.temp_pwd,fn), 'wb') as f:
  1375. f.write(data)
  1376. return fn
  1377. def get_video_url(self, msgid):
  1378. return self.base_uri + '/webwxgetvideo?msgid=%s&skey=%s' % (msgid, self.skey)
  1379. def get_video(self, msgid):
  1380. """
  1381. 获取视频消息,下载视频到本地
  1382. :param msgid: 视频消息id
  1383. :return: 保存的本地视频文件路径
  1384. """
  1385. url = self.base_uri + '/webwxgetvideo?msgid=%s&skey=%s' % (msgid, self.skey)
  1386. headers = {'Range': 'bytes=0-'}
  1387. r = self.session.get(url, headers=headers)
  1388. data = r.content
  1389. fn = 'video_' + msgid + '.mp4'
  1390. with open(os.path.join(self.temp_pwd,fn), 'wb') as f:
  1391. f.write(data)
  1392. return fn
  1393. def set_remarkname(self,uid,remarkname):#设置联系人的备注名
  1394. url = self.base_uri + '/webwxoplog?lang=zh_CN&pass_ticket=%s' \
  1395. % (self.pass_ticket)
  1396. remarkname = self.to_unicode(remarkname)
  1397. params = {
  1398. 'BaseRequest': self.base_request,
  1399. 'CmdId': 2,
  1400. 'RemarkName': remarkname,
  1401. 'UserName': uid
  1402. }
  1403. try:
  1404. r = self.session.post(url, data=json.dumps(params), timeout=60)
  1405. r.encoding = 'utf-8'
  1406. dic = json.loads(r.text)
  1407. return dic['BaseResponse']['ErrMsg']
  1408. except:
  1409. return None