[
  {
    "path": ".gitignore",
    "content": "# Created by .ignore support plugin (hsz.mobi)\n### Python template\n# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nenv/\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\n*.egg-info/\n.installed.cfg\n*.egg\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*,cover\n.hypothesis/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\ntarget/\n\n# IPython Notebook\n.ipynb_checkpoints\n\n# pyenv\n.python-version\n\n# celery beat schedule file\ncelerybeat-schedule\n\n# dotenv\n.env\n\n# virtualenv\nvenv/\nENV/\n\n# Spyder project settings\n.spyderproject\n\n# Rope project settings\n.ropeproject\n\n# pycharm\n.idea\n\n*.pyc\nwxbot_demo_py3/saved/\nwxbot_project_py2.7/tmp_data/\nwxbot_project_py2.7/wechat/wechat_js_backup/\nwxbot_project_py2.7/config/wechat.conf\nwxbot_project_py2.7/wiki/\n"
  },
  {
    "path": "LICENSE",
    "content": "                                 Apache License\n                           Version 2.0, January 2004\n                        http://www.apache.org/licenses/\n\n   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION\n\n   1. Definitions.\n\n      \"License\" shall mean the terms and conditions for use, reproduction,\n      and distribution as defined by Sections 1 through 9 of this document.\n\n      \"Licensor\" shall mean the copyright owner or entity authorized by\n      the copyright owner that is granting the License.\n\n      \"Legal Entity\" shall mean the union of the acting entity and all\n      other entities that control, are controlled by, or are under common\n      control with that entity. For the purposes of this definition,\n      \"control\" means (i) the power, direct or indirect, to cause the\n      direction or management of such entity, whether by contract or\n      otherwise, or (ii) ownership of fifty percent (50%) or more of the\n      outstanding shares, or (iii) beneficial ownership of such entity.\n\n      \"You\" (or \"Your\") shall mean an individual or Legal Entity\n      exercising permissions granted by this License.\n\n      \"Source\" form shall mean the preferred form for making modifications,\n      including but not limited to software source code, documentation\n      source, and configuration files.\n\n      \"Object\" form shall mean any form resulting from mechanical\n      transformation or translation of a Source form, including but\n      not limited to compiled object code, generated documentation,\n      and conversions to other media types.\n\n      \"Work\" shall mean the work of authorship, whether in Source or\n      Object form, made available under the License, as indicated by a\n      copyright notice that is included in or attached to the work\n      (an example is provided in the Appendix below).\n\n      \"Derivative Works\" shall mean any work, whether in Source or Object\n      form, that is based on (or derived from) the Work and for which the\n      editorial revisions, annotations, elaborations, or other modifications\n      represent, as a whole, an original work of authorship. For the purposes\n      of this License, Derivative Works shall not include works that remain\n      separable from, or merely link (or bind by name) to the interfaces of,\n      the Work and Derivative Works thereof.\n\n      \"Contribution\" shall mean any work of authorship, including\n      the original version of the Work and any modifications or additions\n      to that Work or Derivative Works thereof, that is intentionally\n      submitted to Licensor for inclusion in the Work by the copyright owner\n      or by an individual or Legal Entity authorized to submit on behalf of\n      the copyright owner. For the purposes of this definition, \"submitted\"\n      means any form of electronic, verbal, or written communication sent\n      to the Licensor or its representatives, including but not limited to\n      communication on electronic mailing lists, source code control systems,\n      and issue tracking systems that are managed by, or on behalf of, the\n      Licensor for the purpose of discussing and improving the Work, but\n      excluding communication that is conspicuously marked or otherwise\n      designated in writing by the copyright owner as \"Not a Contribution.\"\n\n      \"Contributor\" shall mean Licensor and any individual or Legal Entity\n      on behalf of whom a Contribution has been received by Licensor and\n      subsequently incorporated within the Work.\n\n   2. Grant of Copyright License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      copyright license to reproduce, prepare Derivative Works of,\n      publicly display, publicly perform, sublicense, and distribute the\n      Work and such Derivative Works in Source or Object form.\n\n   3. Grant of Patent License. Subject to the terms and conditions of\n      this License, each Contributor hereby grants to You a perpetual,\n      worldwide, non-exclusive, no-charge, royalty-free, irrevocable\n      (except as stated in this section) patent license to make, have made,\n      use, offer to sell, sell, import, and otherwise transfer the Work,\n      where such license applies only to those patent claims licensable\n      by such Contributor that are necessarily infringed by their\n      Contribution(s) alone or by combination of their Contribution(s)\n      with the Work to which such Contribution(s) was submitted. If You\n      institute patent litigation against any entity (including a\n      cross-claim or counterclaim in a lawsuit) alleging that the Work\n      or a Contribution incorporated within the Work constitutes direct\n      or contributory patent infringement, then any patent licenses\n      granted to You under this License for that Work shall terminate\n      as of the date such litigation is filed.\n\n   4. Redistribution. You may reproduce and distribute copies of the\n      Work or Derivative Works thereof in any medium, with or without\n      modifications, and in Source or Object form, provided that You\n      meet the following conditions:\n\n      (a) You must give any other recipients of the Work or\n          Derivative Works a copy of this License; and\n\n      (b) You must cause any modified files to carry prominent notices\n          stating that You changed the files; and\n\n      (c) You must retain, in the Source form of any Derivative Works\n          that You distribute, all copyright, patent, trademark, and\n          attribution notices from the Source form of the Work,\n          excluding those notices that do not pertain to any part of\n          the Derivative Works; and\n\n      (d) If the Work includes a \"NOTICE\" text file as part of its\n          distribution, then any Derivative Works that You distribute must\n          include a readable copy of the attribution notices contained\n          within such NOTICE file, excluding those notices that do not\n          pertain to any part of the Derivative Works, in at least one\n          of the following places: within a NOTICE text file distributed\n          as part of the Derivative Works; within the Source form or\n          documentation, if provided along with the Derivative Works; or,\n          within a display generated by the Derivative Works, if and\n          wherever such third-party notices normally appear. The contents\n          of the NOTICE file are for informational purposes only and\n          do not modify the License. You may add Your own attribution\n          notices within Derivative Works that You distribute, alongside\n          or as an addendum to the NOTICE text from the Work, provided\n          that such additional attribution notices cannot be construed\n          as modifying the License.\n\n      You may add Your own copyright statement to Your modifications and\n      may provide additional or different license terms and conditions\n      for use, reproduction, or distribution of Your modifications, or\n      for any such Derivative Works as a whole, provided Your use,\n      reproduction, and distribution of the Work otherwise complies with\n      the conditions stated in this License.\n\n   5. Submission of Contributions. Unless You explicitly state otherwise,\n      any Contribution intentionally submitted for inclusion in the Work\n      by You to the Licensor shall be under the terms and conditions of\n      this License, without any additional terms or conditions.\n      Notwithstanding the above, nothing herein shall supersede or modify\n      the terms of any separate license agreement you may have executed\n      with Licensor regarding such Contributions.\n\n   6. Trademarks. This License does not grant permission to use the trade\n      names, trademarks, service marks, or product names of the Licensor,\n      except as required for reasonable and customary use in describing the\n      origin of the Work and reproducing the content of the NOTICE file.\n\n   7. Disclaimer of Warranty. Unless required by applicable law or\n      agreed to in writing, Licensor provides the Work (and each\n      Contributor provides its Contributions) on an \"AS IS\" BASIS,\n      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\n      implied, including, without limitation, any warranties or conditions\n      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A\n      PARTICULAR PURPOSE. You are solely responsible for determining the\n      appropriateness of using or redistributing the Work and assume any\n      risks associated with Your exercise of permissions under this License.\n\n   8. Limitation of Liability. In no event and under no legal theory,\n      whether in tort (including negligence), contract, or otherwise,\n      unless required by applicable law (such as deliberate and grossly\n      negligent acts) or agreed to in writing, shall any Contributor be\n      liable to You for damages, including any direct, indirect, special,\n      incidental, or consequential damages of any character arising as a\n      result of this License or out of the use or inability to use the\n      Work (including but not limited to damages for loss of goodwill,\n      work stoppage, computer failure or malfunction, or any and all\n      other commercial damages or losses), even if such Contributor\n      has been advised of the possibility of such damages.\n\n   9. Accepting Warranty or Additional Liability. While redistributing\n      the Work or Derivative Works thereof, You may choose to offer,\n      and charge a fee for, acceptance of support, warranty, indemnity,\n      or other liability obligations and/or rights consistent with this\n      License. However, in accepting such obligations, You may act only\n      on Your own behalf and on Your sole responsibility, not on behalf\n      of any other Contributor, and only if You agree to indemnify,\n      defend, and hold each Contributor harmless for any liability\n      incurred by, or claims asserted against, such Contributor by reason\n      of your accepting any such warranty or additional liability.\n\n   END OF TERMS AND CONDITIONS\n\n   APPENDIX: How to apply the Apache License to your work.\n\n      To apply the Apache License to your work, attach the following\n      boilerplate notice, with the fields enclosed by brackets \"{}\"\n      replaced with your own identifying information. (Don't include\n      the brackets!)  The text should be enclosed in the appropriate\n      comment syntax for the file format. We also recommend that a\n      file or class name and description of purpose be included on the\n      same \"printed page\" as the copyright notice for easier\n      identification within third-party archives.\n\n   Copyright {yyyy} {name of copyright owner}\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n"
  },
  {
    "path": "README.md",
    "content": "# WeixinBot [![star this repo](http://github-svg-buttons.herokuapp.com/star.svg?user=Urinx&repo=WeixinBot&style=flat&background=1081C1)](http://github.com/Urinx/WeixinBot) [![fork this repo](http://github-svg-buttons.herokuapp.com/fork.svg?user=Urinx&repo=WeixinBot&style=flat&background=1081C1)](http://github.com/Urinx/WeixinBot/fork) ![python](https://img.shields.io/badge/python-2.7%20&%203.6-ff69b4.svg)\n\n网页版微信API，包含终端版微信及微信机器人\n\n## Contents\n* [Demo](#Demo)\n* [Web Weixin Pipeline](#Web-Weixin-Pipeline)\n* [Web Weixin API](#Web-Weixin-API)\n* [Discussion Group](#Discussion-Group)\n* [Recent Update](#Recent-Update)\n\n## <a name=\"Demo\">Demo</a>\n为了确保能正常运行示例脚本，请安装所需的第三方包。\n\n```\npip install -r requirements.txt\n```\n\n注：下面演示的图片与功能可能不是最新的，具体请看源码。\n\n<div align=center>\n<img src=\"imgs/1.png\" width=\"500\" height=\"550\"/>\n</div>\n\n按照操作指示在手机微信上扫描二维码然后登录，你可以选择是否开启自动回复模式。\n\n![2](imgs/2.png)\n\n开启自动回复模式后，如果接收到的是文字消息就会自动回复，包括群消息。\n\n![3](imgs/3.png)\n\n名片，链接，动画表情和地址位置消息。\n\n![4](imgs/4.png)\n\n![5](imgs/5.png)\n\n网页版上有的功能目前基本上都能支持。\n\n## <a name=\"Web-Weixin-Pipeline\">Web Weixin Pipeline</a>\n\n```\n       +--------------+     +---------------+   +---------------+\n       |              |     |               |   |               |\n       |   Get UUID   |     |  Get Contact  |   | Status Notify |\n       |              |     |               |   |               |\n       +-------+------+     +-------^-------+   +-------^-------+\n               |                    |                   |\n               |                    +-------+  +--------+\n               |                            |  |\n       +-------v------+               +-----+--+------+      +--------------+\n       |              |               |               |      |              |\n       |  Get QRCode  |               |  Weixin Init  +------>  Sync Check  <----+\n       |              |               |               |      |              |    |\n       +-------+------+               +-------^-------+      +-------+------+    |\n               |                              |                      |           |\n               |                              |                      +-----------+\n               |                              |                      |\n       +-------v------+               +-------+--------+     +-------v-------+\n       |              | Confirm Login |                |     |               |\n+------>    Login     +---------------> New Login Page |     |  Weixin Sync  |\n|      |              |               |                |     |               |\n|      +------+-------+               +----------------+     +---------------+\n|             |\n|QRCode Scaned|\n+-------------+\n```\n\n\n## <a name=\"Web-Weixin-API\">Web Weixin API</a>\n\n### 登录\n\n| API | 获取 UUID |\n| --- | --------- |\n| url | https://login.weixin.qq.com/jslogin |\n| method | POST |\n| data | URL Encode |\n| params | **appid**: `应用ID` <br> **fun**: new `应用类型` <br> **lang**: zh\\_CN `语言` <br> **_**: `时间戳` |\n\n返回数据(String):\n```\nwindow.QRLogin.code = 200; window.QRLogin.uuid = \"xxx\"\n```\n> 注：这里的appid就是在微信开放平台注册的应用的AppID。网页版微信有两个AppID，早期的是`wx782c26e4c19acffb`，在微信客户端上显示为应用名称为`Web微信`；现在用的是`wxeb7ec651dd0aefa9`，显示名称为`微信网页版`。\n\n<div align=center>\n<img src=\"imgs/8.jpg\" width=\"320\" height=\"211\"/>\n</div>\n\n<br>\n\n| API | 绑定登陆（webwxpushloginurl） |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxpushloginurl |\n| method | GET |\n| params | **uin**: xxx |\n\n返回数据(String):\n```\n{'msg': 'all ok', 'uuid': 'xxx', 'ret': '0'}\n\n通过这种方式可以省掉扫二维码这步操作，更加方便\n```\n<br>\n\n| API | 生成二维码 |\n| --- | --------- |\n| url | https://login.weixin.qq.com/l/ `uuid` |\n| method | GET |\n<br>\n\n| API | 二维码扫描登录 |\n| --- | --------- |\n| url | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login |\n| method | GET |\n| params | **tip**: 1 `未扫描` 0 `已扫描` <br> **uuid**: xxx <br> **_**: `时间戳` |\n\n返回数据(String):\n```\nwindow.code=xxx;\n\nxxx:\n\t408 登陆超时\n\t201 扫描成功\n\t200 确认登录\n\n当返回200时，还会有\nwindow.redirect_uri=\"https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=xxx&uuid=xxx&lang=xxx&scan=xxx\";\n```\n<br>\n\n| API | webwxnewloginpage |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage |\n| method | GET |\n| params | **ticket**: xxx <br> **uuid**: xxx <br> **lang**: zh_CN `语言` <br> **scan**: xxx <br> **fun**: new |\n\n返回数据(XML):\n```\n<error>\n\t<ret>0</ret>\n\t<message>OK</message>\n\t<skey>xxx</skey>\n\t<wxsid>xxx</wxsid>\n\t<wxuin>xxx</wxuin>\n\t<pass_ticket>xxx</pass_ticket>\n\t<isgrayscale>1</isgrayscale>\n</error>\n```\n<br>\n\n### 微信初始化\n\n| API | webwxinit |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?pass_ticket=xxx&skey=xxx&r=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\tUin: xxx, <br>\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sid: xxx, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\tSkey: xxx, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DeviceID: xxx, <br> &nbsp;&nbsp;&nbsp;&nbsp; } <br> } |\n\n返回数据(JSON):\n```\n{\n\t\"BaseResponse\": {\n\t\t\"Ret\": 0,\n\t\t\"ErrMsg\": \"\"\n\t},\n\t\"Count\": 11,\n\t\"ContactList\": [...],\n\t\"SyncKey\": {\n\t\t\"Count\": 4,\n\t\t\"List\": [\n\t\t\t{\n\t\t\t\t\"Key\": 1,\n\t\t\t\t\"Val\": 635705559\n\t\t\t},\n\t\t\t...\n\t\t]\n\t},\n\t\"User\": {\n\t\t\"Uin\": xxx,\n\t\t\"UserName\": xxx,\n\t\t\"NickName\": xxx,\n\t\t\"HeadImgUrl\": xxx,\n\t\t\"RemarkName\": \"\",\n\t\t\"PYInitial\": \"\",\n\t\t\"PYQuanPin\": \"\",\n\t\t\"RemarkPYInitial\": \"\",\n\t\t\"RemarkPYQuanPin\": \"\",\n\t\t\"HideInputBarFlag\": 0,\n\t\t\"StarFriend\": 0,\n\t\t\"Sex\": 1,\n\t\t\"Signature\": \"Apt-get install B\",\n\t\t\"AppAccountFlag\": 0,\n\t\t\"VerifyFlag\": 0,\n\t\t\"ContactFlag\": 0,\n\t\t\"WebWxPluginSwitch\": 0,\n\t\t\"HeadImgFlag\": 1,\n\t\t\"SnsFlag\": 17\n\t},\n\t\"ChatSet\": xxx,\n\t\"SKey\": xxx,\n\t\"ClientVersion\": 369297683,\n\t\"SystemTime\": 1453124908,\n\t\"GrayScale\": 1,\n\t\"InviteStartCount\": 40,\n\t\"MPSubscribeMsgCount\": 2,\n\t\"MPSubscribeMsgList\": [...],\n\t\"ClickReportInterval\": 600000\n}\n```\n<br>\n\n| API | webwxstatusnotify |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?lang=zh_CN&pass_ticket=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; Code: 3, <br> &nbsp;&nbsp;&nbsp;&nbsp; FromUserName: `自己ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp; ToUserName: `自己ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp; ClientMsgId: `时间戳` <br> } |\n\n返回数据(JSON):\n```\n{\n\t\"BaseResponse\": {\n\t\t\"Ret\": 0,\n\t\t\"ErrMsg\": \"\"\n\t},\n\t...\n}\n```\n<br>\n\n### 获取联系人信息\n\n| API | webwxgetcontact |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin//webwxgetcontact?pass_ticket=xxx&skey=xxx&r=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n\n返回数据(JSON):\n```\n{\n\t\"BaseResponse\": {\n\t\t\"Ret\": 0,\n\t\t\"ErrMsg\": \"\"\n\t},\n\t\"MemberCount\": 334,\n\t\"MemberList\": [\n\t\t{\n\t\t\t\"Uin\": 0,\n\t\t\t\"UserName\": xxx,\n\t\t\t\"NickName\": \"Urinx\",\n\t\t\t\"HeadImgUrl\": xxx,\n\t\t\t\"ContactFlag\": 3,\n\t\t\t\"MemberCount\": 0,\n\t\t\t\"MemberList\": [],\n\t\t\t\"RemarkName\": \"\",\n\t\t\t\"HideInputBarFlag\": 0,\n\t\t\t\"Sex\": 0,\n\t\t\t\"Signature\": \"你好，我们是地球三体组织。在这里，你将感受到不一样的思维模式，以及颠覆常规的世界观。而我们的目标，就是以三体人的智慧，引领人类未来科学技术500年。\",\n\t\t\t\"VerifyFlag\": 8,\n\t\t\t\"OwnerUin\": 0,\n\t\t\t\"PYInitial\": \"URINX\",\n\t\t\t\"PYQuanPin\": \"Urinx\",\n\t\t\t\"RemarkPYInitial\": \"\",\n\t\t\t\"RemarkPYQuanPin\": \"\",\n\t\t\t\"StarFriend\": 0,\n\t\t\t\"AppAccountFlag\": 0,\n\t\t\t\"Statues\": 0,\n\t\t\t\"AttrStatus\": 0,\n\t\t\t\"Province\": \"\",\n\t\t\t\"City\": \"\",\n\t\t\t\"Alias\": \"Urinxs\",\n\t\t\t\"SnsFlag\": 0,\n\t\t\t\"UniFriend\": 0,\n\t\t\t\"DisplayName\": \"\",\n\t\t\t\"ChatRoomId\": 0,\n\t\t\t\"KeyWord\": \"gh_\",\n\t\t\t\"EncryChatRoomId\": \"\"\n\t\t},\n\t\t...\n\t],\n\t\"Seq\": 0\n}\n```\n<br>\n\n| API | webwxbatchgetcontact |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=xxx&pass_ticket=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; Count: `群数量`, <br> &nbsp;&nbsp;&nbsp;&nbsp; List: [ <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { UserName: `群ID`, EncryChatRoomId: \"\" }, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ... <br> &nbsp;&nbsp;&nbsp;&nbsp; ], <br> } |\n\n返回数据(JSON)同上\n<br><br>\n\n### 同步刷新\n\n| API | synccheck |\n| --- | --------- |\n| protocol | https |\n| host | webpush.weixin.qq.com <br> webpush.wx2.qq.com <br> webpush.wx8.qq.com <br> webpush.wx.qq.com <br> webpush.web2.wechat.com <br> webpush.web.wechat.com |\n| path | /cgi-bin/mmwebwx-bin/synccheck |\n| method | GET |\n| data | URL Encode |\n| params | **r**: `时间戳` <br> **sid**: xxx <br> **uin**: xxx <br> **skey**: xxx <br> **deviceid**: xxx <br> **synckey**: xxx <br> **_**: `时间戳` |\n\n返回数据(String):\n```\nwindow.synccheck={retcode:\"xxx\",selector:\"xxx\"}\n\nretcode:\n\t0 正常\n\t1100 失败/登出微信\nselector:\n\t0 正常\n\t2 新的消息\n\t7 进入/离开聊天界面\n```\n<br>\n\n| API | webwxsync |\n| --- | --------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=xxx&skey=xxx&pass_ticket=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; SyncKey: xxx, <br> &nbsp;&nbsp;&nbsp;&nbsp; rr: `时间戳取反` <br> } |\n\n返回数据(JSON):\n```\n{\n\t'BaseResponse': {'ErrMsg': '', 'Ret': 0},\n\t'SyncKey': {\n\t\t'Count': 7,\n\t\t'List': [\n\t\t\t{'Val': 636214192, 'Key': 1},\n\t\t\t...\n\t\t]\n\t},\n\t'ContinueFlag': 0,\n\t'AddMsgCount': 1,\n\t'AddMsgList': [\n\t\t{\n\t\t\t'FromUserName': '',\n\t\t\t'PlayLength': 0,\n\t\t\t'RecommendInfo': {...},\n\t\t\t'Content': \"\", \n\t\t\t'StatusNotifyUserName': '',\n\t\t\t'StatusNotifyCode': 5,\n\t\t\t'Status': 3,\n\t\t\t'VoiceLength': 0,\n\t\t\t'ToUserName': '',\n\t\t\t'ForwardFlag': 0,\n\t\t\t'AppMsgType': 0,\n\t\t\t'AppInfo': {'Type': 0, 'AppID': ''},\n\t\t\t'Url': '',\n\t\t\t'ImgStatus': 1,\n\t\t\t'MsgType': 51,\n\t\t\t'ImgHeight': 0,\n\t\t\t'MediaId': '', \n\t\t\t'FileName': '',\n\t\t\t'FileSize': '',\n\t\t\t...\n\t\t},\n\t\t...\n\t],\n\t'ModChatRoomMemberCount': 0,\n\t'ModContactList': [],\n\t'DelContactList': [],\n\t'ModChatRoomMemberList': [],\n\t'DelContactCount': 0,\n\t...\n}\n```\n<br>\n\n### 消息接口\n\n| API | webwxsendmsg |\n| --- | ------------ |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?pass_ticket=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; Msg: { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type: 1 `文字消息`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Content: `要发送的消息`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FromUserName: `自己ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ToUserName: `好友ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LocalID: `与clientMsgId相同`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClientMsgId: `时间戳左移4位随后补上4位随机数` <br> &nbsp;&nbsp;&nbsp;&nbsp; } <br> } |\n\n返回数据(JSON):\n```\n{\n\t\"BaseResponse\": {\n\t\t\"Ret\": 0,\n\t\t\"ErrMsg\": \"\"\n\t},\n\t...\n}\n```\n\n| API | webwxrevokemsg |\n| --- | ------------ |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; SvrMsgId: msg_id, <br> &nbsp;&nbsp;&nbsp;&nbsp; ToUserName: user_id, <br> &nbsp;&nbsp;&nbsp;&nbsp; ClientMsgId: local_msg_id <br>  } |\n\n返回数据(JSON):\n```\n{\n\t\"BaseResponse\": {\n\t\t\"Ret\": 0,\n\t\t\"ErrMsg\": \"\"\n\t}\n}\n```\n\n#### 发送表情\n\n| API | webwxsendmsgemotion |\n| --- | ------------ |\n| url | https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&f=json&pass_ticket=xxx |\n| method | POST |\n| data | JSON |\n| header | ContentType: application/json; charset=UTF-8 |\n| params | { <br> &nbsp;&nbsp;&nbsp;&nbsp; BaseRequest: { Uin: xxx, Sid: xxx, Skey: xxx, DeviceID: xxx }, <br> &nbsp;&nbsp;&nbsp;&nbsp; Msg: { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type: 47 `emoji消息`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EmojiFlag: 2, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MediaId: `表情上传后的媒体ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FromUserName: `自己ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ToUserName: `好友ID`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LocalID: `与clientMsgId相同`, <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ClientMsgId: `时间戳左移4位随后补上4位随机数` <br> &nbsp;&nbsp;&nbsp;&nbsp; } <br> } |\n\n<br>\n\n### 图片接口\n\n| API | webwxgeticon |\n| --- | ------------ |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgeticon |\n| method | GET |\n| params | **seq**: `数字，可为空` <br> **username**: `ID` <br> **skey**: xxx |\n<br>\n\n| API | webwxgetheadimg |\n| --- | --------------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetheadimg |\n| method | GET |\n| params | **seq**: `数字，可为空` <br> **username**: `群ID` <br> **skey**: xxx |\n<br>\n\n| API | webwxgetmsgimg |\n| --- | --------------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg |\n| method | GET |\n| params | **MsgID**: `消息ID` <br> **type**: slave `略缩图` or `为空时加载原图` <br> **skey**: xxx |\n<br>\n\n### 多媒体接口\n\n| API | webwxgetvideo |\n| --- | --------------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo |\n| method | GET |\n| params | **msgid**: `消息ID` <br> **skey**: xxx |\n<br>\n\n| API | webwxgetvoice |\n| --- | --------------- |\n| url | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice |\n| method | GET |\n| params | **msgid**: `消息ID` <br> **skey**: xxx |\n<br>\n\n### 账号类型\n\n| 类型 | 说明 |\n| :--: | --- |\n| 个人账号 | 以`@`开头，例如：`@xxx` |\n| 群聊 | 以`@@`开头，例如：`@@xxx` |\n| 公众号/服务号 | 以`@`开头，但其`VerifyFlag` & 8 != 0 <br><br> `VerifyFlag`: <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般个人公众号/服务号：8 <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 一般企业的服务号：24 <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 微信官方账号`微信团队`：56 |\n| 特殊账号 | 像文件传输助手之类的账号，有特殊的ID，目前已知的有：<br> `filehelper`, `newsapp`, `fmessage`, `weibo`, `qqmail`, `tmessage`, `qmessage`, `qqsync`, `floatbottle`, `lbsapp`, `shakeapp`, `medianote`, `qqfriend`, `readerapp`, `blogapp`, `facebookapp`, `masssendapp`, `meishiapp`, `feedsapp`, `voip`, `blogappweixin`, `weixin`, `brandsessionholder`, `weixinreminder`, `officialaccounts`, `notification_messages`, `wxitil`, `userexperience_alarm`, `notification_messages` |\n<br>\n\n### 消息类型\n\n消息一般格式：\n```\n{\n\t\"FromUserName\": \"\",\n\t\"ToUserName\": \"\",\n\t\"Content\": \"\",\n\t\"StatusNotifyUserName\": \"\",\n\t\"ImgWidth\": 0,\n\t\"PlayLength\": 0,\n\t\"RecommendInfo\": {...},\n\t\"StatusNotifyCode\": 4,\n\t\"NewMsgId\": \"\",\n\t\"Status\": 3,\n\t\"VoiceLength\": 0,\n\t\"ForwardFlag\": 0,\n\t\"AppMsgType\": 0,\n\t\"Ticket\": \"\",\n\t\"AppInfo\": {...},\n\t\"Url\": \"\",\n\t\"ImgStatus\": 1,\n\t\"MsgType\": 1,\n\t\"ImgHeight\": 0,\n\t\"MediaId\": \"\",\n\t\"MsgId\": \"\",\n\t\"FileName\": \"\",\n\t\"HasProductId\": 0,\n\t\"FileSize\": \"\",\n\t\"CreateTime\": 1454602196,\n\t\"SubMsgType\": 0\n}\n```\n<br>\n\n| MsgType | 说明 |\n| ------- | --- |\n| 1  | 文本消息 |\n| 3  | 图片消息 |\n| 34 | 语音消息 |\n| 37 | 好友确认消息 |\n| 40 | POSSIBLEFRIEND_MSG |\n| 42 | 共享名片 |\n| 43 | 视频消息 |\n| 47 | 动画表情 |\n| 48 | 位置消息 |\n| 49 | 分享链接 |\n| 50 | VOIPMSG |\n| 51 | 微信初始化消息 |\n| 52 | VOIPNOTIFY |\n| 53 | VOIPINVITE |\n| 62 | 小视频 |\n| 9999 | SYSNOTICE |\n| 10000 | 系统消息 |\n| 10002 | 撤回消息 |\n<br>\n\n**微信初始化消息**\n```html\nMsgType: 51\nFromUserName: 自己ID\nToUserName: 自己ID\nStatusNotifyUserName: 最近联系的联系人ID\nContent:\n\t<msg>\n\t    <op id='4'>\n\t        <username>\n\t        \t// 最近联系的联系人\n\t            filehelper,xxx@chatroom,wxid_xxx,xxx,...\n\t        </username>\n\t        <unreadchatlist>\n\t            <chat>\n\t                <username>\n\t                \t// 朋友圈\n\t                    MomentsUnreadMsgStatus\n\t                </username>\n\t                <lastreadtime>\n\t                    1454502365\n\t                </lastreadtime>\n\t            </chat>\n\t        </unreadchatlist>\n\t        <unreadfunctionlist>\n\t        \t// 未读的功能账号消息，群发助手，漂流瓶等\n\t        </unreadfunctionlist>\n\t    </op>\n\t</msg>\n```\n\n**文本消息**\n```\nMsgType: 1\nFromUserName: 发送方ID\nToUserName: 接收方ID\nContent: 消息内容\n```\n\n**图片消息**\n```html\nMsgType: 3\nFromUserName: 发送方ID\nToUserName: 接收方ID\nMsgId: 用于获取图片\nContent:\n\t<msg>\n\t\t<img length=\"6503\" hdlength=\"0\" />\n\t\t<commenturl></commenturl>\n\t</msg>\n```\n\n**小视频消息**\n```html\nMsgType: 62\nFromUserName: 发送方ID\nToUserName: 接收方ID\nMsgId: 用于获取小视频\nContent:\n\t<msg>\n\t\t<img length=\"6503\" hdlength=\"0\" />\n\t\t<commenturl></commenturl>\n\t</msg>\n```\n\n**地理位置消息**\n```\nMsgType: 1\nFromUserName: 发送方ID\nToUserName: 接收方ID\nContent: http://weixin.qq.com/cgi-bin/redirectforward?args=xxx\n// 属于文本消息，只不过内容是一个跳转到地图的链接\n```\n\n**名片消息**\n```js\nMsgType: 42\nFromUserName: 发送方ID\nToUserName: 接收方ID\nContent:\n\t<?xml version=\"1.0\"?>\n\t<msg bigheadimgurl=\"\" smallheadimgurl=\"\" username=\"\" nickname=\"\"  shortpy=\"\" alias=\"\" imagestatus=\"3\" scene=\"17\" province=\"\" city=\"\" sign=\"\" sex=\"1\" certflag=\"0\" certinfo=\"\" brandIconUrl=\"\" brandHomeUrl=\"\" brandSubscriptConfigUrl=\"\" brandFlags=\"0\" regionCode=\"\" />\n\nRecommendInfo:\n\t{\n\t\t\"UserName\": \"xxx\", // ID\n\t\t\"Province\": \"xxx\", \n\t\t\"City\": \"xxx\", \n\t\t\"Scene\": 17, \n\t\t\"QQNum\": 0, \n\t\t\"Content\": \"\", \n\t\t\"Alias\": \"xxx\", // 微信号\n\t\t\"OpCode\": 0, \n\t\t\"Signature\": \"\", \n\t\t\"Ticket\": \"\", \n\t\t\"Sex\": 0, // 1:男, 2:女\n\t\t\"NickName\": \"xxx\", // 昵称\n\t\t\"AttrStatus\": 4293221, \n\t\t\"VerifyFlag\": 0\n\t}\n```\n\n**语音消息**\n```html\nMsgType: 34\nFromUserName: 发送方ID\nToUserName: 接收方ID\nMsgId: 用于获取语音\nContent:\n\t<msg>\n\t\t<voicemsg endflag=\"1\" cancelflag=\"0\" forwardflag=\"0\" voiceformat=\"4\" voicelength=\"1580\" length=\"2026\" bufid=\"216825389722501519\" clientmsgid=\"49efec63a9774a65a932a4e5fcd4e923filehelper174_1454602489\" fromusername=\"\" />\n\t</msg>\n```\n\n**动画表情**\n```html\nMsgType: 47\nFromUserName: 发送方ID\nToUserName: 接收方ID\nContent:\n\t<msg>\n\t\t<emoji fromusername = \"\" tousername = \"\" type=\"2\" idbuffer=\"media:0_0\" md5=\"e68363487d8f0519c4e1047de403b2e7\" len = \"86235\" productid=\"com.tencent.xin.emoticon.bilibili\" androidmd5=\"e68363487d8f0519c4e1047de403b2e7\" androidlen=\"86235\" s60v3md5 = \"e68363487d8f0519c4e1047de403b2e7\" s60v3len=\"86235\" s60v5md5 = \"e68363487d8f0519c4e1047de403b2e7\" s60v5len=\"86235\" cdnurl = \"http://emoji.qpic.cn/wx_emoji/eFygWtxcoMF8M0oCCsksMA0gplXAFQNpiaqsmOicbXl1OC4Tyx18SGsQ/\" designerid = \"\" thumburl = \"http://mmbiz.qpic.cn/mmemoticon/dx4Y70y9XctRJf6tKsy7FwWosxd4DAtItSfhKS0Czr56A70p8U5O8g/0\" encrypturl = \"http://emoji.qpic.cn/wx_emoji/UyYVK8GMlq5VnJ56a4GkKHAiaC266Y0me0KtW6JN2FAZcXiaFKccRevA/\" aeskey= \"a911cc2ec96ddb781b5ca85d24143642\" ></emoji> \n\t\t<gameext type=\"0\" content=\"0\" ></gameext>\n\t</msg>\n```\n\n**普通链接或应用分享消息**\n```html\nMsgType: 49\nAppMsgType: 5\nFromUserName: 发送方ID\nToUserName: 接收方ID\nUrl: 链接地址\nFileName: 链接标题\nContent:\n\t<msg>\n\t\t<appmsg appid=\"\"  sdkver=\"0\">\n\t\t\t<title></title>\n\t\t\t<des></des>\n\t\t\t<type>5</type>\n\t\t\t<content></content>\n\t\t\t<url></url>\n\t\t\t<thumburl></thumburl>\n\t\t\t...\n\t\t</appmsg>\n\t\t<appinfo>\n\t\t\t<version></version>\n\t\t\t<appname></appname>\n\t\t</appinfo>\n\t</msg>\n```\n\n**音乐链接消息**\n```html\nMsgType: 49\nAppMsgType: 3\nFromUserName: 发送方ID\nToUserName: 接收方ID\nUrl: 链接地址\nFileName: 音乐名\n\nAppInfo: // 分享链接的应用\n\t{\n\t\tType: 0, \n\t\tAppID: wx485a97c844086dc9\n\t}\n\nContent:\n\t<msg>\n\t\t<appmsg appid=\"wx485a97c844086dc9\"  sdkver=\"0\">\n\t\t\t<title></title>\n\t\t\t<des></des>\n\t\t\t<action></action>\n\t\t\t<type>3</type>\n\t\t\t<showtype>0</showtype>\n\t\t\t<mediatagname></mediatagname>\n\t\t\t<messageext></messageext>\n\t\t\t<messageaction></messageaction>\n\t\t\t<content></content>\n\t\t\t<contentattr>0</contentattr>\n\t\t\t<url></url>\n\t\t\t<lowurl></lowurl>\n\t\t\t<dataurl>\n\t\t\t\thttp://ws.stream.qqmusic.qq.com/C100003i9hMt1bgui0.m4a?vkey=6867EF99F3684&amp;guid=ffffffffc104ea2964a111cf3ff3edaf&amp;fromtag=46\n\t\t\t</dataurl>\n\t\t\t<lowdataurl>\n\t\t\t\thttp://ws.stream.qqmusic.qq.com/C100003i9hMt1bgui0.m4a?vkey=6867EF99F3684&amp;guid=ffffffffc104ea2964a111cf3ff3edaf&amp;fromtag=46\n\t\t\t</lowdataurl>\n\t\t\t<appattach>\n\t\t\t\t<totallen>0</totallen>\n\t\t\t\t<attachid></attachid>\n\t\t\t\t<emoticonmd5></emoticonmd5>\n\t\t\t\t<fileext></fileext>\n\t\t\t</appattach>\n\t\t\t<extinfo></extinfo>\n\t\t\t<sourceusername></sourceusername>\n\t\t\t<sourcedisplayname></sourcedisplayname>\n\t\t\t<commenturl></commenturl>\n\t\t\t<thumburl>\n\t\t\t\thttp://imgcache.qq.com/music/photo/album/63/180_albumpic_143163_0.jpg\n\t\t\t</thumburl>\n\t\t\t<md5></md5>\n\t\t</appmsg>\n\t\t<fromusername></fromusername>\n\t\t<scene>0</scene>\n\t\t<appinfo>\n\t\t\t<version>29</version>\n\t\t\t<appname>摇一摇搜歌</appname>\n\t\t</appinfo>\n\t\t<commenturl></commenturl>\n\t</msg>\n```\n\n**群消息**\n```\nMsgType: 1\nFromUserName: @@xxx\nToUserName: @xxx\nContent:\n\t@xxx:<br/>xxx\n```\n\n**红包消息**\n```\nMsgType: 49\nAppMsgType: 2001\nFromUserName: 发送方ID\nToUserName: 接收方ID\nContent: 未知\n```\n注：根据网页版的代码可以看到未来可能支持查看红包消息，但目前走的是系统消息，见下。\n\n**系统消息**\n```\nMsgType: 10000\nFromUserName: 发送方ID\nToUserName: 自己ID\nContent:\n\t\"你已添加了 xxx ，现在可以开始聊天了。\"\n\t\"如果陌生人主动添加你为朋友，请谨慎核实对方身份。\"\n\t\"收到红包，请在手机上查看\"\n```\n\n\n## <a name=\"Discussion-Group\">Discussion Group</a>\n如果你希望和 WeixinBot 的其他开发者交流，或者有什么问题和建议，欢迎大家加入微信群【Youth fed the dog】一起讨论。扫描下面的二维码添加机器人为好友，并回复【Aidog】获取入群链接。\n\n<div align=center>\n<img src=\"imgs/groupQrcode.jpg\" width=\"220\" height=\"220\" alt=\"join us\"/>\n</div>\n\n注：这个不是群的二维码，是机器人拉你入群，记得回复机器人【Aidog】哦~ （secret code: Aidog）\n\n## <a name=\"Recent-Update\">Recent Update</a>\n\n- association_login\n\t目前网页版微信已经可以脱离扫码，但是依然需要在客户端进行确认登录。\n"
  },
  {
    "path": "wxbot_demo_py3/requirements.txt",
    "content": "colorama\ncoloredlogs\nhumanfriendly\nlxml\nqrcode\nrequests\nsix\nrequests_toolbelt\npyqrcode\ncertifi\n"
  },
  {
    "path": "wxbot_demo_py3/weixin.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\nimport qrcode\nfrom pyqrcode import QRCode\nimport urllib.request, urllib.parse, urllib.error\nimport urllib.request, urllib.error, urllib.parse\nimport http.cookiejar\nimport requests\nimport xml.dom.minidom\nimport json\nimport time\nimport ssl\nimport re\nimport sys\nimport os\nimport subprocess\nimport random\nimport multiprocessing\nimport platform\nimport logging\nimport http.client\nfrom collections import defaultdict\nfrom urllib.parse import urlparse\nfrom lxml import html\nfrom socket import timeout as timeout_error\n#import pdb\n\n# for media upload\nimport mimetypes\nfrom requests_toolbelt.multipart.encoder import MultipartEncoder\n\n\ndef catchKeyboardInterrupt(fn):\n    def wrapper(*args):\n        try:\n            return fn(*args)\n        except KeyboardInterrupt:\n            print('\\n[*] 强制退出程序')\n            logging.debug('[*] 强制退出程序')\n    return wrapper\n\n\ndef _decode_list(data):\n    rv = []\n    for item in data:\n        if isinstance(item, str):\n            item = item.encode('utf-8')\n        elif isinstance(item, list):\n            item = _decode_list(item)\n        elif isinstance(item, dict):\n            item = _decode_dict(item)\n        rv.append(item)\n    return rv\n\n\ndef _decode_dict(data):\n    rv = {}\n    for key, value in data.items():\n        if isinstance(key, str):\n            key = key.encode('utf-8')\n        if isinstance(value, str):\n            value = value.encode('utf-8')\n        elif isinstance(value, list):\n            value = _decode_list(value)\n        elif isinstance(value, dict):\n            value = _decode_dict(value)\n        rv[key] = value\n    return rv\n\n\nclass WebWeixin(object):\n\n    def __str__(self):\n        description = \\\n            \"=========================\\n\" + \\\n            \"[#] Web Weixin\\n\" + \\\n            \"[#] Debug Mode: \" + str(self.DEBUG) + \"\\n\" + \\\n            \"[#] Uuid: \" + self.uuid + \"\\n\" + \\\n            \"[#] Uin: \" + str(self.uin) + \"\\n\" + \\\n            \"[#] Sid: \" + self.sid + \"\\n\" + \\\n            \"[#] Skey: \" + self.skey + \"\\n\" + \\\n            \"[#] DeviceId: \" + self.deviceId + \"\\n\" + \\\n            \"[#] PassTicket: \" + self.pass_ticket + \"\\n\" + \\\n            \"=========================\"\n        return description\n\n    def __init__(self):\n        self.DEBUG = False\n        self.commandLineQRCode = False\n        self.uuid = ''\n        self.base_uri = ''\n        self.redirect_uri = ''\n        self.uin = ''\n        self.sid = ''\n        self.skey = ''\n        self.pass_ticket = ''\n        self.deviceId = 'e' + repr(random.random())[2:17]\n        self.BaseRequest = {}\n        self.synckey = ''\n        self.SyncKey = []\n        self.User = []\n        self.MemberList = []\n        self.ContactList = []  # 好友\n        self.GroupList = []  # 群\n        self.GroupMemeberList = []  # 群友\n        self.PublicUsersList = []  # 公众号／服务号\n        self.SpecialUsersList = []  # 特殊账号\n        self.autoReplyMode = False\n        self.syncHost = ''\n        self.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36'\n        self.interactive = False\n        self.autoOpen = False\n        self.saveFolder = os.path.join(os.getcwd(), 'saved')\n        self.saveSubFolders = {'webwxgeticon': 'icons', 'webwxgetheadimg': 'headimgs', 'webwxgetmsgimg': 'msgimgs',\n                               'webwxgetvideo': 'videos', 'webwxgetvoice': 'voices', '_showQRCodeImg': 'qrcodes'}\n        self.appid = 'wx782c26e4c19acffb'\n        self.lang = 'zh_CN'\n        self.lastCheckTs = time.time()\n        self.memberCount = 0\n        self.SpecialUsers = ['newsapp', 'fmessage', 'filehelper', 'weibo', 'qqmail', 'fmessage', 'tmessage', 'qmessage', 'qqsync', 'floatbottle', 'lbsapp', 'shakeapp', 'medianote', 'qqfriend', 'readerapp', 'blogapp', 'facebookapp', 'masssendapp', 'meishiapp', 'feedsapp',\n                             'voip', 'blogappweixin', 'weixin', 'brandsessionholder', 'weixinreminder', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c', 'officialaccounts', 'notification_messages', 'wxid_novlwrv3lqwv11', 'gh_22b87fa7cb3c', 'wxitil', 'userexperience_alarm', 'notification_messages']\n        self.TimeOut = 20  # 同步最短时间间隔（单位：秒）\n        self.media_count = -1\n\n        self.cookie = http.cookiejar.CookieJar()\n        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.cookie))\n        opener.addheaders = [('User-agent', self.user_agent)]\n        urllib.request.install_opener(opener)\n\n    def loadConfig(self, config):\n        if config['DEBUG']:\n            self.DEBUG = config['DEBUG']\n        if config['autoReplyMode']:\n            self.autoReplyMode = config['autoReplyMode']\n        if config['user_agent']:\n            self.user_agent = config['user_agent']\n        if config['interactive']:\n            self.interactive = config['interactive']\n        if config['autoOpen']:\n            self.autoOpen = config['autoOpen']\n\n    def getUUID(self):\n        url = 'https://login.weixin.qq.com/jslogin'\n        params = {\n            'appid': self.appid,\n            'fun': 'new',\n            'lang': self.lang,\n            '_': int(time.time()),\n        }\n        #r = requests.get(url=url, params=params)\n        #r.encoding = 'utf-8'\n        #data = r.text\n        data = self._post(url, params, False).decode(\"utf-8\")\n        if data == '':\n            return False\n        regx = r'window.QRLogin.code = (\\d+); window.QRLogin.uuid = \"(\\S+?)\"'\n        pm = re.search(regx, data)\n        if pm:\n            code = pm.group(1)\n            self.uuid = pm.group(2)\n            return code == '200'\n        return False\n\n    def genQRCode(self):\n        #return self._showQRCodeImg()\n        if sys.platform.startswith('win'):\n            self._showQRCodeImg('win')\n        elif sys.platform.find('darwin') >= 0:\n            self._showQRCodeImg('macos')\n        else:\n            self._str2qr('https://login.weixin.qq.com/l/' + self.uuid)\n\n    def _showQRCodeImg(self, str):\n        if self.commandLineQRCode:\n            qrCode = QRCode('https://login.weixin.qq.com/l/' + self.uuid)\n            self._showCommandLineQRCode(qrCode.text(1))\n        else:\n            url = 'https://login.weixin.qq.com/qrcode/' + self.uuid\n            params = {\n                't': 'webwx',\n                '_': int(time.time())\n            }\n\n            data = self._post(url, params, False)\n            if data == '':\n                return\n            QRCODE_PATH = self._saveFile('qrcode.jpg', data, '_showQRCodeImg')\n            if str == 'win':\n                os.startfile(QRCODE_PATH)\n            elif str == 'macos':\n                subprocess.call([\"open\", QRCODE_PATH])\n            else:\n                return\n\n    def _showCommandLineQRCode(self, qr_data, enableCmdQR=2):\n        try:\n            b = u'\\u2588'\n            sys.stdout.write(b + '\\r')\n            sys.stdout.flush()\n        except UnicodeEncodeError:\n            white = 'MM'\n        else:\n            white = b\n        black = '  '\n        blockCount = int(enableCmdQR)\n        if abs(blockCount) == 0:\n            blockCount = 1\n        white *= abs(blockCount)\n        if blockCount < 0:\n            white, black = black, white\n        sys.stdout.write(' ' * 50 + '\\r')\n        sys.stdout.flush()\n        qr = qr_data.replace('0', white).replace('1', black)\n        sys.stdout.write(qr)\n        sys.stdout.flush()\n\n    def waitForLogin(self, tip=1):\n        time.sleep(tip)\n        url = 'https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?tip=%s&uuid=%s&_=%s' % (\n            tip, self.uuid, int(time.time()))\n        data = self._get(url)\n        if data == '':\n            return False\n        pm = re.search(r\"window.code=(\\d+);\", data)\n        code = pm.group(1)\n\n        if code == '201':\n            return True\n        elif code == '200':\n            pm = re.search(r'window.redirect_uri=\"(\\S+?)\";', data)\n            r_uri = pm.group(1) + '&fun=new'\n            self.redirect_uri = r_uri\n            self.base_uri = r_uri[:r_uri.rfind('/')]\n            return True\n        elif code == '408':\n            self._echo('[登陆超时] \\n')\n        else:\n            self._echo('[登陆异常] \\n')\n        return False\n\n    def login(self):\n        data = self._get(self.redirect_uri)\n        if data == '':\n            return False\n        doc = xml.dom.minidom.parseString(data)\n        root = doc.documentElement\n\n        for node in root.childNodes:\n            if node.nodeName == 'skey':\n                self.skey = node.childNodes[0].data\n            elif node.nodeName == 'wxsid':\n                self.sid = node.childNodes[0].data\n            elif node.nodeName == 'wxuin':\n                self.uin = node.childNodes[0].data\n            elif node.nodeName == 'pass_ticket':\n                self.pass_ticket = node.childNodes[0].data\n\n        if '' in (self.skey, self.sid, self.uin, self.pass_ticket):\n            return False\n\n        self.BaseRequest = {\n            'Uin': int(self.uin),\n            'Sid': self.sid,\n            'Skey': self.skey,\n            'DeviceID': self.deviceId,\n        }\n        return True\n\n    def webwxinit(self):\n        url = self.base_uri + '/webwxinit?pass_ticket=%s&skey=%s&r=%s' % (\n            self.pass_ticket, self.skey, int(time.time()))\n        params = {\n            'BaseRequest': self.BaseRequest\n        }\n        dic = self._post(url, params)\n        if dic == '':\n            return False\n        self.SyncKey = dic['SyncKey']\n        self.User = dic['User']\n        # synckey for synccheck\n        self.synckey = '|'.join(\n            [str(keyVal['Key']) + '_' + str(keyVal['Val']) for keyVal in self.SyncKey['List']])\n\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxstatusnotify(self):\n        url = self.base_uri + \\\n            '/webwxstatusnotify?lang=zh_CN&pass_ticket=%s' % (self.pass_ticket)\n        params = {\n            'BaseRequest': self.BaseRequest,\n            \"Code\": 3,\n            \"FromUserName\": self.User['UserName'],\n            \"ToUserName\": self.User['UserName'],\n            \"ClientMsgId\": int(time.time())\n        }\n        dic = self._post(url, params)\n        if dic == '':\n            return False\n\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxgetcontact(self):\n        SpecialUsers = self.SpecialUsers\n        url = self.base_uri + '/webwxgetcontact?pass_ticket=%s&skey=%s&r=%s' % (\n            self.pass_ticket, self.skey, int(time.time()))\n        dic = self._post(url, {})\n        if dic == '':\n            return False\n\n        self.MemberCount = dic['MemberCount']\n        self.MemberList = dic['MemberList']\n        ContactList = self.MemberList[:]\n        GroupList = self.GroupList[:]\n        PublicUsersList = self.PublicUsersList[:]\n        SpecialUsersList = self.SpecialUsersList[:]\n\n        for i in range(len(ContactList) - 1, -1, -1):\n            Contact = ContactList[i]\n            if Contact['VerifyFlag'] & 8 != 0:  # 公众号/服务号\n                ContactList.remove(Contact)\n                self.PublicUsersList.append(Contact)\n            elif Contact['UserName'] in SpecialUsers:  # 特殊账号\n                ContactList.remove(Contact)\n                self.SpecialUsersList.append(Contact)\n            elif '@@' in Contact['UserName']:  # 群聊\n                ContactList.remove(Contact)\n                self.GroupList.append(Contact)\n            elif Contact['UserName'] == self.User['UserName']:  # 自己\n                ContactList.remove(Contact)\n        self.ContactList = ContactList\n\n        return True\n\n    def webwxbatchgetcontact(self):\n        url = self.base_uri + \\\n            '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s' % (\n                int(time.time()), self.pass_ticket)\n        params = {\n            'BaseRequest': self.BaseRequest,\n            \"Count\": len(self.GroupList),\n            \"List\": [{\"UserName\": g['UserName'], \"EncryChatRoomId\":\"\"} for g in self.GroupList]\n        }\n        dic = self._post(url, params)\n        if dic == '':\n            return False\n\n        # blabla ...\n        ContactList = dic['ContactList']\n        ContactCount = dic['Count']\n        self.GroupList = ContactList\n\n        for i in range(len(ContactList) - 1, -1, -1):\n            Contact = ContactList[i]\n            MemberList = Contact['MemberList']\n            for member in MemberList:\n                self.GroupMemeberList.append(member)\n        return True\n\n    def getNameById(self, id):\n        url = self.base_uri + \\\n            '/webwxbatchgetcontact?type=ex&r=%s&pass_ticket=%s' % (\n                int(time.time()), self.pass_ticket)\n        params = {\n            'BaseRequest': self.BaseRequest,\n            \"Count\": 1,\n            \"List\": [{\"UserName\": id, \"EncryChatRoomId\": \"\"}]\n        }\n        dic = self._post(url, params)\n        if dic == '':\n            return None\n\n        # blabla ...\n        return dic['ContactList']\n\n    def testsynccheck(self):\n        SyncHost = ['wx2.qq.com',\n                    'webpush.wx2.qq.com',\n                    'wx8.qq.com',\n                    'webpush.wx8.qq.com',\n                    'qq.com',\n                    'webpush.wx.qq.com',\n                    'web2.wechat.com',\n                    'webpush.web2.wechat.com',\n                    'wechat.com',\n                    'webpush.web.wechat.com',\n                    'webpush.weixin.qq.com',\n                    'webpush.wechat.com',\n                    'webpush1.wechat.com',\n                    'webpush2.wechat.com',\n                    'webpush.wx.qq.com',\n                    'webpush2.wx.qq.com']\n        for host in SyncHost:\n            self.syncHost = host\n            [retcode, selector] = self.synccheck()\n            if retcode == '0':\n                return True\n        return False\n\n    def synccheck(self):\n        params = {\n            'r': int(time.time()),\n            'sid': self.sid,\n            'uin': self.uin,\n            'skey': self.skey,\n            'deviceid': self.deviceId,\n            'synckey': self.synckey,\n            '_': int(time.time()),\n        }\n        url = 'https://' + self.syncHost + '/cgi-bin/mmwebwx-bin/synccheck?' + urllib.parse.urlencode(params)\n        data = self._get(url, timeout=5)\n        if data == '':\n            return [-1,-1]\n\n        pm = re.search(\n            r'window.synccheck={retcode:\"(\\d+)\",selector:\"(\\d+)\"}', data)\n        retcode = pm.group(1)\n        selector = pm.group(2)\n        return [retcode, selector]\n\n    def webwxsync(self):\n        url = self.base_uri + \\\n            '/webwxsync?sid=%s&skey=%s&pass_ticket=%s' % (\n                self.sid, self.skey, self.pass_ticket)\n        params = {\n            'BaseRequest': self.BaseRequest,\n            'SyncKey': self.SyncKey,\n            'rr': ~int(time.time())\n        }\n        dic = self._post(url, params)\n        if dic == '':\n            return None\n        if self.DEBUG:\n            print(json.dumps(dic, indent=4))\n            (json.dumps(dic, indent=4))\n\n        if dic['BaseResponse']['Ret'] == 0:\n            self.SyncKey = dic['SyncKey']\n            self.synckey = '|'.join(\n                [str(keyVal['Key']) + '_' + str(keyVal['Val']) for keyVal in self.SyncKey['List']])\n        return dic\n\n    def webwxsendmsg(self, word, to='filehelper'):\n        url = self.base_uri + \\\n            '/webwxsendmsg?pass_ticket=%s' % (self.pass_ticket)\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        params = {\n            'BaseRequest': self.BaseRequest,\n            'Msg': {\n                \"Type\": 1,\n                \"Content\": self._transcoding(word),\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": to,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        headers = {'content-type': 'application/json; charset=UTF-8'}\n        data = json.dumps(params, ensure_ascii=False).encode('utf8')\n        r = requests.post(url, data=data, headers=headers)\n        dic = r.json()\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxuploadmedia(self, image_name):\n        url = 'https://file2.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'\n        # 计数器\n        self.media_count = self.media_count + 1\n        # 文件名\n        file_name = image_name\n        # MIME格式\n        # mime_type = application/pdf, image/jpeg, image/png, etc.\n        mime_type = mimetypes.guess_type(image_name, strict=False)[0]\n        # 微信识别的文档格式，微信服务器应该只支持两种类型的格式。pic和doc\n        # pic格式，直接显示。doc格式则显示为文件。\n        media_type = 'pic' if mime_type.split('/')[0] == 'image' else 'doc'\n        # 上一次修改日期\n        lastModifieDate = 'Thu Mar 17 2016 00:55:10 GMT+0800 (CST)'\n        # 文件大小\n        file_size = os.path.getsize(file_name)\n        # PassTicket\n        pass_ticket = self.pass_ticket\n        # clientMediaId\n        client_media_id = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        # webwx_data_ticket\n        webwx_data_ticket = ''\n        for item in self.cookie:\n            if item.name == 'webwx_data_ticket':\n                webwx_data_ticket = item.value\n                break\n        if (webwx_data_ticket == ''):\n            return \"None Fuck Cookie\"\n\n        uploadmediarequest = json.dumps({\n            \"BaseRequest\": self.BaseRequest,\n            \"ClientMediaId\": client_media_id,\n            \"TotalLen\": file_size,\n            \"StartPos\": 0,\n            \"DataLen\": file_size,\n            \"MediaType\": 4\n        }, ensure_ascii=False).encode('utf8')\n\n        multipart_encoder = MultipartEncoder(\n            fields={\n                'id': 'WU_FILE_' + str(self.media_count),\n                'name': file_name,\n                'type': mime_type,\n                'lastModifieDate': lastModifieDate,\n                'size': str(file_size),\n                'mediatype': media_type,\n                'uploadmediarequest': uploadmediarequest,\n                'webwx_data_ticket': webwx_data_ticket,\n                'pass_ticket': pass_ticket,\n                'filename': (file_name, open(file_name, 'rb'), mime_type.split('/')[1])\n            },\n            boundary='-----------------------------1575017231431605357584454111'\n        )\n\n        headers = {\n            'Host': 'file2.wx.qq.com',\n            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:42.0) Gecko/20100101 Firefox/42.0',\n            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',\n            'Accept-Language': 'en-US,en;q=0.5',\n            'Accept-Encoding': 'gzip, deflate',\n            'Referer': 'https://wx2.qq.com/',\n            'Content-Type': multipart_encoder.content_type,\n            'Origin': 'https://wx2.qq.com',\n            'Connection': 'keep-alive',\n            'Pragma': 'no-cache',\n            'Cache-Control': 'no-cache'\n        }\n\n        r = requests.post(url, data=multipart_encoder, headers=headers)\n        response_json = r.json()\n        if response_json['BaseResponse']['Ret'] == 0:\n            return response_json\n        return None\n\n    def webwxsendmsgimg(self, user_id, media_id):\n        url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json&pass_ticket=%s' % self.pass_ticket\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        data_json = {\n            \"BaseRequest\": self.BaseRequest,\n            \"Msg\": {\n                \"Type\": 3,\n                \"MediaId\": media_id,\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": user_id,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        headers = {'content-type': 'application/json; charset=UTF-8'}\n        data = json.dumps(data_json, ensure_ascii=False).encode('utf8')\n        r = requests.post(url, data=data, headers=headers)\n        dic = r.json()\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxsendmsgemotion(self, user_id, media_id):\n        url = 'https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendemoticon?fun=sys&f=json&pass_ticket=%s' % self.pass_ticket\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        data_json = {\n            \"BaseRequest\": self.BaseRequest,\n            \"Msg\": {\n                \"Type\": 47,\n                \"EmojiFlag\": 2,\n                \"MediaId\": media_id,\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": user_id,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        headers = {'content-type': 'application/json; charset=UTF-8'}\n        data = json.dumps(data_json, ensure_ascii=False).encode('utf8')\n        r = requests.post(url, data=data, headers=headers)\n        dic = r.json()\n        if self.DEBUG:\n            print(json.dumps(dic, indent=4))\n            logging.debug(json.dumps(dic, indent=4))\n        return dic['BaseResponse']['Ret'] == 0\n\n    def _saveFile(self, filename, data, api=None):\n        fn = filename\n        if self.saveSubFolders[api]:\n            dirName = os.path.join(self.saveFolder, self.saveSubFolders[api])\n            if not os.path.exists(dirName):\n                os.makedirs(dirName)\n            fn = os.path.join(dirName, filename)\n            logging.debug('Saved file: %s' % fn)\n            with open(fn, 'wb') as f:\n                f.write(data)\n                f.close()\n        return fn\n\n    def webwxgeticon(self, id):\n        url = self.base_uri + \\\n            '/webwxgeticon?username=%s&skey=%s' % (id, self.skey)\n        data = self._get(url)\n        if data == '':\n            return ''\n        fn = 'img_' + id + '.jpg'\n        return self._saveFile(fn, data, 'webwxgeticon')\n\n    def webwxgetheadimg(self, id):\n        url = self.base_uri + \\\n            '/webwxgetheadimg?username=%s&skey=%s' % (id, self.skey)\n        data = self._get(url)\n        if data == '':\n            return ''\n        fn = 'img_' + id + '.jpg'\n        return self._saveFile(fn, data, 'webwxgetheadimg')\n\n    def webwxgetmsgimg(self, msgid):\n        url = self.base_uri + \\\n            '/webwxgetmsgimg?MsgID=%s&skey=%s' % (msgid, self.skey)\n        data = self._get(url)\n        if data == '':\n            return ''\n        fn = 'img_' + msgid + '.jpg'\n        return self._saveFile(fn, data, 'webwxgetmsgimg')\n\n    # Not work now for weixin haven't support this API\n    def webwxgetvideo(self, msgid):\n        url = self.base_uri + \\\n            '/webwxgetvideo?msgid=%s&skey=%s' % (msgid, self.skey)\n        data = self._get(url, api='webwxgetvideo')\n        if data == '':\n            return ''\n        fn = 'video_' + msgid + '.mp4'\n        return self._saveFile(fn, data, 'webwxgetvideo')\n\n    def webwxgetvoice(self, msgid):\n        url = self.base_uri + \\\n            '/webwxgetvoice?msgid=%s&skey=%s' % (msgid, self.skey)\n        data = self._get(url, api='webwxgetvoice')\n        if data == '':\n            return ''\n        fn = 'voice_' + msgid + '.mp3'\n        return self._saveFile(fn, data, 'webwxgetvoice')\n\n    def getGroupName(self, id):\n        name = '未知群'\n        for member in self.GroupList:\n            if member['UserName'] == id:\n                name = member['NickName']\n        if name == '未知群':\n            # 现有群里面查不到\n            GroupList = self.getNameById(id)\n            for group in GroupList:\n                self.GroupList.append(group)\n                if group['UserName'] == id:\n                    name = group['NickName']\n                    MemberList = group['MemberList']\n                    for member in MemberList:\n                        self.GroupMemeberList.append(member)\n        return name\n\n    def getUserRemarkName(self, id):\n        name = '未知群' if id[:2] == '@@' else '陌生人'\n        if id == self.User['UserName']:\n            return self.User['NickName']  # 自己\n\n        if id[:2] == '@@':\n            # 群\n            name = self.getGroupName(id)\n        else:\n            # 特殊账号\n            for member in self.SpecialUsersList:\n                if member['UserName'] == id:\n                    name = member['RemarkName'] if member[\n                        'RemarkName'] else member['NickName']\n\n            # 公众号或服务号\n            for member in self.PublicUsersList:\n                if member['UserName'] == id:\n                    name = member['RemarkName'] if member[\n                        'RemarkName'] else member['NickName']\n\n            # 直接联系人\n            for member in self.ContactList:\n                if member['UserName'] == id:\n                    name = member['RemarkName'] if member[\n                        'RemarkName'] else member['NickName']\n            # 群友\n            for member in self.GroupMemeberList:\n                if member['UserName'] == id:\n                    name = member['DisplayName'] if member[\n                        'DisplayName'] else member['NickName']\n\n        if name == '未知群' or name == '陌生人':\n            logging.debug(id)\n        return name\n\n    def getUSerID(self, name):\n        for member in self.MemberList:\n            if name == member['RemarkName'] or name == member['NickName']:\n                return member['UserName']\n        return None\n\n    def _showMsg(self, message):\n\n        srcName = None\n        dstName = None\n        groupName = None\n        content = None\n\n        msg = message\n        logging.debug(msg)\n\n        if msg['raw_msg']:\n            srcName = self.getUserRemarkName(msg['raw_msg']['FromUserName'])\n            dstName = self.getUserRemarkName(msg['raw_msg']['ToUserName'])\n            content = msg['raw_msg']['Content'].replace(\n                '&lt;', '<').replace('&gt;', '>')\n            message_id = msg['raw_msg']['MsgId']\n\n            if content.find('http://weixin.qq.com/cgi-bin/redirectforward?args=') != -1:\n                # 地理位置消息\n                data = self._get(content)\n                if data == '':\n                    return\n                data.decode('gbk').encode('utf-8')\n                pos = self._searchContent('title', data, 'xml')\n                temp = self._get(content)\n                if temp == '':\n                    return\n                tree = html.fromstring(temp)\n                url = tree.xpath('//html/body/div/img')[0].attrib['src']\n\n                for item in urlparse(url).query.split('&'):\n                    if item.split('=')[0] == 'center':\n                        loc = item.split('=')[-1:]\n\n                content = '%s 发送了一个 位置消息 - 我在 [%s](%s) @ %s]' % (\n                    srcName, pos, url, loc)\n\n            if msg['raw_msg']['ToUserName'] == 'filehelper':\n                # 文件传输助手\n                dstName = '文件传输助手'\n\n            if msg['raw_msg']['FromUserName'][:2] == '@@':\n                # 接收到来自群的消息\n                if \":<br/>\" in content:\n                    [people, content] = content.split(':<br/>', 1)\n                    groupName = srcName\n                    srcName = self.getUserRemarkName(people)\n                    dstName = 'GROUP'\n                else:\n                    groupName = srcName\n                    srcName = 'SYSTEM'\n            elif msg['raw_msg']['ToUserName'][:2] == '@@':\n                # 自己发给群的消息\n                groupName = dstName\n                dstName = 'GROUP'\n\n            # 收到了红包\n            if content == '收到红包，请在手机上查看':\n                msg['message'] = content\n\n            # 指定了消息内容\n            if 'message' in list(msg.keys()):\n                content = msg['message']\n\n        if groupName != None:\n            print('%s |%s| %s -> %s: %s' % (message_id, groupName.strip(), srcName.strip(), dstName.strip(), content.replace('<br/>', '\\n')))\n            logging.info('%s |%s| %s -> %s: %s' % (message_id, groupName.strip(),\n                                                   srcName.strip(), dstName.strip(), content.replace('<br/>', '\\n')))\n        else:\n            print('%s %s -> %s: %s' % (message_id, srcName.strip(), dstName.strip(), content.replace('<br/>', '\\n')))\n            logging.info('%s %s -> %s: %s' % (message_id, srcName.strip(),\n                                              dstName.strip(), content.replace('<br/>', '\\n')))\n\n    def handleMsg(self, r):\n        for msg in r['AddMsgList']:\n            print('[*] 你有新的消息，请注意查收')\n            logging.debug('[*] 你有新的消息，请注意查收')\n\n            if self.DEBUG:\n                fn = 'msg' + str(int(random.random() * 1000)) + '.json'\n                with open(fn, 'w') as f:\n                    f.write(json.dumps(msg))\n                print('[*] 该消息已储存到文件: ' + fn)\n                logging.debug('[*] 该消息已储存到文件: %s' % (fn))\n\n            msgType = msg['MsgType']\n            name = self.getUserRemarkName(msg['FromUserName'])\n            content = msg['Content'].replace('&lt;', '<').replace('&gt;', '>')\n            msgid = msg['MsgId']\n\n            if msgType == 1:\n                raw_msg = {'raw_msg': msg}\n                self._showMsg(raw_msg)\n#自己加的代码-------------------------------------------#\n                #if self.autoReplyRevokeMode:\n                #    store\n#自己加的代码-------------------------------------------#\n                if self.autoReplyMode:\n                    ans = self._xiaodoubi(content) + '\\n[微信机器人自动回复]'\n                    if self.webwxsendmsg(ans, msg['FromUserName']):\n                        print('自动回复: ' + ans)\n                        logging.info('自动回复: ' + ans)\n                    else:\n                        print('自动回复失败')\n                        logging.info('自动回复失败')\n            elif msgType == 3:\n                image = self.webwxgetmsgimg(msgid)\n                raw_msg = {'raw_msg': msg,\n                           'message': '%s 发送了一张图片: %s' % (name, image)}\n                self._showMsg(raw_msg)\n                self._safe_open(image)\n            elif msgType == 34:\n                voice = self.webwxgetvoice(msgid)\n                raw_msg = {'raw_msg': msg,\n                           'message': '%s 发了一段语音: %s' % (name, voice)}\n                self._showMsg(raw_msg)\n                self._safe_open(voice)\n            elif msgType == 42:\n                info = msg['RecommendInfo']\n                print('%s 发送了一张名片:' % name)\n                print('=========================')\n                print('= 昵称: %s' % info['NickName'])\n                print('= 微信号: %s' % info['Alias'])\n                print('= 地区: %s %s' % (info['Province'], info['City']))\n                print('= 性别: %s' % ['未知', '男', '女'][info['Sex']])\n                print('=========================')\n                raw_msg = {'raw_msg': msg, 'message': '%s 发送了一张名片: %s' % (\n                    name.strip(), json.dumps(info))}\n                self._showMsg(raw_msg)\n            elif msgType == 47:\n                url = self._searchContent('cdnurl', content)\n                raw_msg = {'raw_msg': msg,\n                           'message': '%s 发了一个动画表情，点击下面链接查看: %s' % (name, url)}\n                self._showMsg(raw_msg)\n                self._safe_open(url)\n            elif msgType == 49:\n                appMsgType = defaultdict(lambda: \"\")\n                appMsgType.update({5: '链接', 3: '音乐', 7: '微博'})\n                print('%s 分享了一个%s:' % (name, appMsgType[msg['AppMsgType']]))\n                print('=========================')\n                print('= 标题: %s' % msg['FileName'])\n                print('= 描述: %s' % self._searchContent('des', content, 'xml'))\n                print('= 链接: %s' % msg['Url'])\n                print('= 来自: %s' % self._searchContent('appname', content, 'xml'))\n                print('=========================')\n                card = {\n                    'title': msg['FileName'],\n                    'description': self._searchContent('des', content, 'xml'),\n                    'url': msg['Url'],\n                    'appname': self._searchContent('appname', content, 'xml')\n                }\n                raw_msg = {'raw_msg': msg, 'message': '%s 分享了一个%s: %s' % (\n                    name, appMsgType[msg['AppMsgType']], json.dumps(card))}\n                self._showMsg(raw_msg)\n            elif msgType == 51:\n                raw_msg = {'raw_msg': msg, 'message': '[*] 成功获取联系人信息'}\n                self._showMsg(raw_msg)\n            elif msgType == 62:\n                video = self.webwxgetvideo(msgid)\n                raw_msg = {'raw_msg': msg,\n                           'message': '%s 发了一段小视频: %s' % (name, video)}\n                self._showMsg(raw_msg)\n                self._safe_open(video)\n            elif msgType == 10002:\n                raw_msg = {'raw_msg': msg, 'message': '%s 撤回了一条消息' % name}\n                self._showMsg(raw_msg)\n            else:\n                logging.debug('[*] 该消息类型为: %d，可能是表情，图片, 链接或红包: %s' %\n                              (msg['MsgType'], json.dumps(msg)))\n                raw_msg = {\n                    'raw_msg': msg, 'message': '[*] 该消息类型为: %d，可能是表情，图片, 链接或红包' % msg['MsgType']}\n                self._showMsg(raw_msg)\n\n    def listenMsgMode(self):\n        print('[*] 进入消息监听模式 ... 成功')\n        logging.debug('[*] 进入消息监听模式 ... 成功')\n        self._run('[*] 进行同步线路测试 ... ', self.testsynccheck)\n        playWeChat = 0\n        redEnvelope = 0\n        while True:\n            self.lastCheckTs = time.time()\n            [retcode, selector] = self.synccheck()\n            if self.DEBUG:\n                print('retcode: %s, selector: %s' % (retcode, selector))\n            logging.debug('retcode: %s, selector: %s' % (retcode, selector))\n            if retcode == '1100':\n                print('[*] 你在手机上登出了微信，债见')\n                logging.debug('[*] 你在手机上登出了微信，债见')\n                break\n            if retcode == '1101':\n                print('[*] 你在其他地方登录了 WEB 版微信，债见')\n                logging.debug('[*] 你在其他地方登录了 WEB 版微信，债见')\n                break\n            elif retcode == '0':\n                if selector == '2':\n                    r = self.webwxsync()\n                    if r is not None:\n                        self.handleMsg(r)\n                elif selector == '6':\n                    # TODO\n                    redEnvelope += 1\n                    print('[*] 收到疑似红包消息 %d 次' % redEnvelope)\n                    logging.debug('[*] 收到疑似红包消息 %d 次' % redEnvelope)\n                elif selector == '7':\n                    playWeChat += 1\n                    print('[*] 你在手机上玩微信被我发现了 %d 次' % playWeChat)\n                    logging.debug('[*] 你在手机上玩微信被我发现了 %d 次' % playWeChat)\n                    r = self.webwxsync()\n                elif selector == '0':\n                    time.sleep(1)\n            if (time.time() - self.lastCheckTs) <= 20:\n                time.sleep(time.time() - self.lastCheckTs)\n\n    def sendMsg(self, name, word, isfile=False):\n        id = self.getUSerID(name)\n        if id:\n            if isfile:\n                with open(word, 'r') as f:\n                    for line in f.readlines():\n                        line = line.replace('\\n', '')\n                        self._echo('-> ' + name + ': ' + line)\n                        if self.webwxsendmsg(line, id):\n                            print(' [成功]')\n                        else:\n                            print(' [失败]')\n                        time.sleep(1)\n            else:\n                if self.webwxsendmsg(word, id):\n                    print('[*] 消息发送成功')\n                    logging.debug('[*] 消息发送成功')\n                else:\n                    print('[*] 消息发送失败')\n                    logging.debug('[*] 消息发送失败')\n        else:\n            print('[*] 此用户不存在')\n            logging.debug('[*] 此用户不存在')\n\n    def sendMsgToAll(self, word):\n        for contact in self.ContactList:\n            name = contact['RemarkName'] if contact[\n                'RemarkName'] else contact['NickName']\n            id = contact['UserName']\n            self._echo('-> ' + name + ': ' + word)\n            if self.webwxsendmsg(word, id):\n                print(' [成功]')\n            else:\n                print(' [失败]')\n            time.sleep(1)\n\n    def sendImg(self, name, file_name):\n        response = self.webwxuploadmedia(file_name)\n        media_id = \"\"\n        if response is not None:\n            media_id = response['MediaId']\n        user_id = self.getUSerID(name)\n        response = self.webwxsendmsgimg(user_id, media_id)\n\n    def sendEmotion(self, name, file_name):\n        response = self.webwxuploadmedia(file_name)\n        media_id = \"\"\n        if response is not None:\n            media_id = response['MediaId']\n        user_id = self.getUSerID(name)\n        response = self.webwxsendmsgemotion(user_id, media_id)\n\n    @catchKeyboardInterrupt\n    def start(self):\n        self._echo('[*] 微信网页版 ... 开动')\n        print()\n        logging.debug('[*] 微信网页版 ... 开动')\n        while True:\n            self._run('[*] 正在获取 uuid ... ', self.getUUID)\n            self._echo('[*] 正在获取二维码 ... 成功')\n            print()\n            logging.debug('[*] 微信网页版 ... 开动')\n            self.genQRCode()\n            print('[*] 请使用微信扫描二维码以登录 ... ')\n            if not self.waitForLogin():\n                continue\n                print('[*] 请在手机上点击确认以登录 ... ')\n            if not self.waitForLogin(0):\n                continue\n            break\n\n        self._run('[*] 正在登录 ... ', self.login)\n        self._run('[*] 微信初始化 ... ', self.webwxinit)\n        self._run('[*] 开启状态通知 ... ', self.webwxstatusnotify)\n        self._run('[*] 获取联系人 ... ', self.webwxgetcontact)\n        self._echo('[*] 应有 %s 个联系人，读取到联系人 %d 个' %\n                   (self.MemberCount, len(self.MemberList)))\n        print()\n        self._echo('[*] 共有 %d 个群 | %d 个直接联系人 | %d 个特殊账号 ｜ %d 公众号或服务号' % (len(self.GroupList),\n                                                                         len(self.ContactList), len(self.SpecialUsersList), len(self.PublicUsersList)))\n        print()\n        self._run('[*] 获取群 ... ', self.webwxbatchgetcontact)\n        logging.debug('[*] 微信网页版 ... 开动')\n        if self.DEBUG:\n            print(self)\n        logging.debug(self)\n\n        if self.interactive and input('[*] 是否开启自动回复模式(y/n): ') == 'y':\n            self.autoReplyMode = True\n            print('[*] 自动回复模式 ... 开启')\n            logging.debug('[*] 自动回复模式 ... 开启')\n        else:\n            print('[*] 自动回复模式 ... 关闭')\n            logging.debug('[*] 自动回复模式 ... 关闭')\n\n        if sys.platform.startswith('win'):\n            import _thread\n            _thread.start_new_thread(self.listenMsgMode())\n        else:\n            listenProcess = multiprocessing.Process(target=self.listenMsgMode)\n            listenProcess.start()\n\n        while True:\n            text = input('')\n            if text == 'quit':\n                listenProcess.terminate()\n                print('[*] 退出微信')\n                logging.debug('[*] 退出微信')\n                exit()\n            elif text[:2] == '->':\n                [name, word] = text[2:].split(':')\n                if name == 'all':\n                    self.sendMsgToAll(word)\n                else:\n                    self.sendMsg(name, word)\n            elif text[:3] == 'm->':\n                [name, file] = text[3:].split(':')\n                self.sendMsg(name, file, True)\n            elif text[:3] == 'f->':\n                print('发送文件')\n                logging.debug('发送文件')\n            elif text[:3] == 'i->':\n                print('发送图片')\n                [name, file_name] = text[3:].split(':')\n                self.sendImg(name, file_name)\n                logging.debug('发送图片')\n            elif text[:3] == 'e->':\n                print('发送表情')\n                [name, file_name] = text[3:].split(':')\n                self.sendEmotion(name, file_name)\n                logging.debug('发送表情')\n\n    def _safe_open(self, path):\n        if self.autoOpen:\n            if platform.system() == \"Linux\":\n                os.system(\"xdg-open %s &\" % path)\n            else:\n                os.system('open %s &' % path)\n\n    def _run(self, str, func, *args):\n        self._echo(str)\n        if func(*args):\n            print('成功')\n            logging.debug('%s... 成功' % (str))\n        else:\n            print('失败\\n[*] 退出程序')\n            logging.debug('%s... 失败' % (str))\n            logging.debug('[*] 退出程序')\n            exit()\n\n    def _echo(self, str):\n        sys.stdout.write(str)\n        sys.stdout.flush()\n\n    def _printQR(self, mat):\n        for i in mat:\n            BLACK = '\\033[40m  \\033[0m'\n            WHITE = '\\033[47m  \\033[0m'\n            print(''.join([BLACK if j else WHITE for j in i]))\n\n    def _str2qr(self, str):\n        print(str)\n        qr = qrcode.QRCode()\n        qr.border = 1\n        qr.add_data(str)\n        qr.make()\n        # img = qr.make_image()\n        # img.save(\"qrcode.png\")\n        #mat = qr.get_matrix()\n        #self._printQR(mat)  # qr.print_tty() or qr.print_ascii()\n        qr.print_ascii(invert=True)\n\n    def _transcoding(self, data):\n        if not data:\n            return data\n        result = None\n        if type(data) == str:\n            result = data\n        elif type(data) == str:\n            result = data.decode('utf-8')\n        return result\n\n    def _get(self, url: object, api: object = None, timeout: object = None) -> object:\n        request = urllib.request.Request(url=url)\n        request.add_header('Referer', 'https://wx.qq.com/')\n        if api == 'webwxgetvoice':\n            request.add_header('Range', 'bytes=0-')\n        if api == 'webwxgetvideo':\n            request.add_header('Range', 'bytes=0-')\n        try:\n            response = urllib.request.urlopen(request, timeout=timeout) if timeout else urllib.request.urlopen(request)\n            if api == 'webwxgetvoice' or api == 'webwxgetvideo':\n                data = response.read()\n            else:\n                data = response.read().decode('utf-8')\n            logging.debug(url)\n            return data\n        except urllib.error.HTTPError as e:\n            logging.error('HTTPError = ' + str(e.code))\n        except urllib.error.URLError as e:\n            logging.error('URLError = ' + str(e.reason))\n        except http.client.HTTPException as e:\n            logging.error('HTTPException')\n        except timeout_error as e:\n            pass\n        except ssl.CertificateError as e:\n            pass\n        except Exception:\n            import traceback\n            logging.error('generic exception: ' + traceback.format_exc())\n        return ''\n\n    def _post(self, url: object, params: object, jsonfmt: object = True) -> object:\n        if jsonfmt:\n            data = (json.dumps(params)).encode()\n            \n            request = urllib.request.Request(url=url, data=data)\n            request.add_header(\n                'ContentType', 'application/json; charset=UTF-8')\n        else:\n            request = urllib.request.Request(url=url, data=urllib.parse.urlencode(params).encode(encoding='utf-8'))\n\n\n        try:\n            response = urllib.request.urlopen(request)\n            data = response.read()\n            if jsonfmt:\n                return json.loads(data.decode('utf-8') )#object_hook=_decode_dict)\n            return data\n        except urllib.error.HTTPError as e:\n            logging.error('HTTPError = ' + str(e.code))\n        except urllib.error.URLError as e:\n            logging.error('URLError = ' + str(e.reason))\n        except http.client.HTTPException as e:\n            logging.error('HTTPException')\n        except Exception:\n            import traceback\n            logging.error('generic exception: ' + traceback.format_exc())\n\n        return ''\n\n    def _xiaodoubi(self, word):\n        url = 'http://www.xiaodoubi.com/bot/chat.php'\n        try:\n            r = requests.post(url, data={'chat': word})\n            return r.content\n        except:\n            return \"让我一个人静静 T_T...\"\n\n    def _simsimi(self, word):\n        key = ''\n        url = 'http://sandbox.api.simsimi.com/request.p?key=%s&lc=ch&ft=0.0&text=%s' % (\n            key, word)\n        r = requests.get(url)\n        ans = r.json()\n        if ans['result'] == '100':\n            return ans['response']\n        else:\n            return '你在说什么，风太大听不清列'\n\n    def _searchContent(self, key, content, fmat='attr'):\n        if fmat == 'attr':\n            pm = re.search(key + '\\s?=\\s?\"([^\"<]+)\"', content)\n            if pm:\n                return pm.group(1)\n        elif fmat == 'xml':\n            pm = re.search('<{0}>([^<]+)</{0}>'.format(key), content)\n            if not pm:\n                pm = re.search(\n                    '<{0}><\\!\\[CDATA\\[(.*?)\\]\\]></{0}>'.format(key), content)\n            if pm:\n                return pm.group(1)\n        return '未知'\n\n\nclass UnicodeStreamFilter:\n\n    def __init__(self, target):\n        self.target = target\n        self.encoding = 'utf-8'\n        self.errors = 'replace'\n        self.encode_to = self.target.encoding\n\n    def write(self, s):\n        if type(s) == str:\n            s = s.encode().decode('utf-8')\n        s = s.encode(self.encode_to, self.errors).decode(self.encode_to)\n        self.target.write(s)\n\n    def flush(self):\n        self.target.flush()\n\nif sys.stdout.encoding == 'cp936':\n    sys.stdout = UnicodeStreamFilter(sys.stdout)\n\n\nif __name__ == '__main__':\n    logger = logging.getLogger(__name__)\n    if not sys.platform.startswith('win'):\n        import coloredlogs\n        coloredlogs.install(level='DEBUG')\n\n    webwx = WebWeixin()\n    webwx.start()\n"
  },
  {
    "path": "wxbot_project_py2.7/README.md",
    "content": "# wxbot_project_py2.7\n\n目录结构:\n```bash\n.\n├── README.md\n├── config\n│   ├── __init__.py\n│   ├── config_manager.py\n│   ├── constant.py\n│   ├── log.py\n│   ├── requirements.txt\n│   └── wechat.conf.bak\n├── db\n│   ├── __init__.py\n│   ├── mysql_db.py\n│   └── sqlite_db.py\n├── docker\n│   ├── Dockerfile\n│   └── README.md\n├── flask_templates\n│   ├── index.html\n│   └── upload.html\n├── wechat\n│   ├── __init__.py\n│   ├── utils.py\n│   ├── wechat_apis.py\n│   └── wechat.py\n├── weixin_bot.py\n└── wx_handler\n    ├── __init__.py\n    ├── bot.py\n    ├── sendgrid_mail.py\n    └── wechat_msg_processor.py\n```\n"
  },
  {
    "path": "wxbot_project_py2.7/config/__init__.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\nfrom config_manager import ConfigManager\nfrom constant import Constant\nfrom log import Log"
  },
  {
    "path": "wxbot_project_py2.7/config/config_manager.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom constant import Constant\n#---------------------------------------------------\nimport ConfigParser\nimport os\n#===================================================\n\n\nclass ConfigManager(object):\n\n    def __init__(self):\n        self.config = Constant.WECHAT_CONFIG_FILE\n        self.cp = ConfigParser.ConfigParser()\n        self.cp.read(self.config)\n\n        data_dir = self.get('setting', 'prefix')\n        upload_dir = self.getpath('uploaddir')\n        if not os.path.exists(data_dir):\n            os.makedirs(data_dir)\n        if not os.path.exists(upload_dir):\n            os.makedirs(upload_dir)\n\n    def get(self, section, option):\n        return self.cp.get(section, option)\n\n    def set(self, section, option, value):\n        self.cp.set(section, option, value)\n        self.cp.write(open(self.config, 'w'))\n\n    def getpath(self, dir):\n        prefix = self.get('setting', 'prefix')\n        return prefix + self.get('setting', dir)\n\n    def setup_database(self):\n        path = self.get('setting', 'prefix')\n        conf = [\n            path + self.get('setting', 'uploaddir'),\n            path + self.get('setting', 'datadir'),\n            path + self.get('setting', 'logdir'),\n        ]\n        return conf\n\n    def set_wechat_config(self, conf):\n        for [key, value] in conf.items():\n            self.cp.set('wechat', key, value)\n        self.cp.write(open(self.config, 'w'))\n\n    def get_wechat_config(self):\n        uin = self.cp.get('wechat', 'uin')\n        last_login = self.cp.get('wechat', 'last_login')\n        conf = [\n            self.cp.get('wechat', 'uuid'),\n            self.cp.get('wechat', 'redirect_uri'),\n            int(uin if uin else 0),\n            self.cp.get('wechat', 'sid'),\n            self.cp.get('wechat', 'skey'),\n            self.cp.get('wechat', 'pass_ticket'),\n            self.cp.get('wechat', 'synckey'),\n            self.cp.get('wechat', 'device_id'),\n            float(last_login if last_login else 0),\n        ]\n        return conf\n\n    def get_wechat_media_dir(self):\n        prefix = self.get('setting', 'prefix')\n        path = prefix + self.cp.get('setting', 'mediapath')\n        return {\n            'webwxgeticon': path + '/icons',\n            'webwxgetheadimg': path + '/headimgs',\n            'webwxgetmsgimg': path + '/msgimgs',\n            'webwxgetvideo': path + '/videos',\n            'webwxgetvoice': path + '/voices',\n            '_showQRCodeImg': path + '/qrcodes',\n        }\n\n    def get_pickle_files(self):\n        prefix = self.get('setting', 'prefix')\n        return {\n            'User': prefix + self.get('setting', 'contact_user'),\n            'MemberList': prefix + self.get('setting', 'contact_member_list'),\n            'GroupList': prefix + self.get('setting', 'contact_group_list'),\n            'GroupMemeberList': prefix + self.get('setting', 'contact_group_memeber_list'),\n            'SpecialUsersList': prefix + self.get('setting', 'contact_special_users_list'),\n        }\n\n    def get_cookie(self):\n        prefix = self.get('setting', 'prefix')\n        path = prefix + self.get('setting', 'cookie')\n        basedir = os.path.dirname(path)\n        if not os.path.exists(basedir):\n            os.makedirs(basedir)\n        return path\n\n    def mysql(self):\n        mysql = {\n            'host': self.get('mysql', 'host'),\n            'port': self.cp.getint('mysql', 'port'),\n            'user': self.get('mysql', 'user'),\n            'passwd': self.get('mysql', 'passwd'),\n            'database': self.get('mysql', 'database'),\n        }\n        return mysql\n"
  },
  {
    "path": "wxbot_project_py2.7/config/constant.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\nimport time\n\nclass Constant(object):\n    \"\"\"\n    @brief      All used constants are listed here\n    \"\"\"\n\n    WECHAT_CONFIG_FILE = 'config/wechat.conf'\n    LOGGING_LOGGER_NAME = 'WeChat'\n\n    QRCODE_BLACK = '\\033[40m  \\033[0m'\n    QRCODE_WHITE = '\\033[47m  \\033[0m'\n\n    HTTP_HEADER_USERAGENT = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36')]\n    HTTP_HEADER_CONTENTTYPE = ['ContentType', 'application/json; charset=UTF-8']\n    HTTP_HEADER_CONNECTION = ['Connection', 'keep-alive']\n    HTTP_HEADER_REFERER = ['Referer', 'https://wx.qq.com/']\n    HTTP_HEADER_RANGE = ['Range', 'bytes=0-']\n\n    REGEX_EMOJI = r'<span class=\"emoji emoji(\\w+)\"></span>'\n    \n    SERVER_LOG_FORMAT = '%(asctime)s - %(pathname)s:%(lineno)d - %(name)s - %(levelname)s - %(message)s'\n    SERVER_UPLOAD_ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])\n    SERVER_PAGE_UPLOAD = 'upload.html'\n    SERVER_PAGE_INDEX = 'index.html'\n\n    RUN_RESULT_SUCCESS = '成功 %ds\\n'\n    RUN_RESULT_FAIL = '失败\\n[*] 退出程序\\n'\n    MAIN_RESTART = '[*] wait for restart'\n    LOG_MSG_FILE = 'WeChat-Msgs-%Y-%m-%d.json'\n    LOG_MSG_GROUP_LIST_FILE = 'group_list.json'\n    LOG_MSG_QUIT = '\\n[*] Force quit.\\n'\n    LOG_MSG_FAIL = '失败\\n'\n    LOG_MSG_SUCCESS = '成功\\n'\n    LOG_MSG_START = '[*] 微信网页版 ... 开动\\n'\n    LOG_MSG_RECOVER = '[*] 从配置文件中恢复 ... '\n    LOG_MSG_RECOVER_CONTACT = '[*] 从文件中恢复联系人数据 ... '\n    LOG_MSG_TRY_INIT = '[*] 尝试初始化 ... '\n    LOG_MSG_ASSOCIATION_LOGIN = '[*] 通过关联登录 ... '\n    LOG_MSG_GET_UUID = '[*] 正在获取 uuid ... '\n    LOG_MSG_GET_QRCODE = '[*] 正在获取二维码 ... 成功\\n'\n    LOG_MSG_SCAN_QRCODE = '[*] 请使用微信扫描二维码以登录 ... \\n'\n    LOG_MSG_CONFIRM_LOGIN = '[*] 请在手机上点击确认以登录 ... \\n'\n    LOG_MSG_WAIT_LOGIN_ERR1 = '[登陆超时] \\n'\n    LOG_MSG_WAIT_LOGIN_ERR2 = '[登陆异常] \\n'\n    LOG_MSG_LOGIN = '[*] 正在登录 ... '\n    LOG_MSG_INIT = '[*] 微信初始化 ... '\n    LOG_MSG_STATUS_NOTIFY = '[*] 开启状态通知 ... '\n    LOG_MSG_GET_CONTACT = '[*] 获取联系人 ... '\n    LOG_MSG_CONTACT_COUNT = '[*] 应有 %s 个联系人，读取到联系人 %d 个\\n'\n    LOG_MSG_OTHER_CONTACT_COUNT = '[*] 共有 %d 个群 | %d 个直接联系人 | %d 个特殊账号 ｜ %d 公众号或服务号\\n'\n    LOG_MSG_GET_GROUP_MEMBER = '[*] 拉取群聊成员 ... '\n    LOG_MSG_SNAPSHOT = '[*] 保存配置 ... '\n    LOG_MSG_LOGOUT = '[*] 你在手机上登出了微信\\n'\n    LOG_MSG_LOGIN_OTHERWHERE = '[*] 你在其他地方登录了 WEB 版微信\\n'\n    LOG_MSG_QUIT_ON_PHONE = '[*] 你在手机上主动退出了\\n'\n    LOG_MSG_RUNTIME = '[*] Total run: %s\\n'\n    LOG_MSG_KILL_PROCESS = 'kill %d'\n    LOG_MSG_NEW_MSG = '>>> %d 条新消息\\n'\n    LOG_MSG_LOCATION = '[位置] %s'\n    LOG_MSG_PICTURE = '[图片] %s'\n    LOG_MSG_VOICE = '[语音] %s'\n    LOG_MSG_RECALL = '撤回了一条消息'\n    LOG_MSG_ADD_FRIEND = '%s 请求添加你为好友'\n    LOG_MSG_UNKNOWN_MSG = '[*] 该消息类型为: %d，内容: %s'\n    LOG_MSG_VIDEO = '[小视频] %s'\n    LOG_MSG_NOTIFY_PHONE = '[*] 提示手机网页版微信登录状态\\n'\n    LOG_MSG_EMOTION = '[表情] %s'\n    LOG_MSG_NAME_CARD = (\n        '[名片]\\n'\n        '=========================\\n'\n        '= 昵称: %s\\n'\n        '= 微信号: %s\\n'\n        '= 地区: %s %s\\n'\n        '= 性别: %s\\n'\n        '========================='\n    )\n    LOG_MSG_SEX_OPTION = ['未知', '男', '女']\n    LOG_MSG_APP_LINK = (\n        '[%s]\\n'\n        '=========================\\n'\n        '= 标题: %s\\n'\n        '= 描述: %s\\n'\n        '= 链接: %s\\n'\n        '= 来自: %s\\n'\n        '========================='\n    )\n    LOG_MSG_APP_LINK_TYPE = {5: '链接', 3: '音乐', 7: '微博'}\n    LOG_MSG_APP_IMG = (\n        '[图片]\\n'\n        '=========================\\n'\n        '= 文件: %s\\n'\n        '= 来自: %s\\n'\n        '========================='\n    )\n    LOG_MSG_SYSTEM = '系统消息'\n    LOG_MSG_UNKNOWN_NAME = '未知_'\n    LOG_MSG_UNKNOWN_GROUP_NAME = '未知群_'\n\n    TABLE_GROUP_MSG_LOG = 'WeChatRoomMessage'\n    TABLE_GROUP_MSG_LOG_COL = \"\"\"\n        MsgID text,\n        RoomOwnerID text,\n        RoomName text,\n        UserCount text,\n        FromUserName text,\n        ToUserName text,\n        AttrStatus text,\n        DisplayName text,\n        Name text, \n        MsgType text,\n        FaceMsg text,\n        TextMsg text,\n        ImageMsg text,\n        VideoMsg text,\n        SoundMsg text,\n        LinkMsg text,\n        NameCardMsg text,\n        LocationMsg text,\n        RecallMsgID text,\n        SysMsg text,\n        MsgTime text,\n        MsgTimestamp text\n    \"\"\"\n\n    @staticmethod\n    def TABLE_GROUP_LIST():\n        return 'WeChatRoom_' + time.strftime('%Y%m%d', time.localtime())\n    \n    TABLE_GROUP_LIST_COL = \"\"\"\n        RoomName text,\n        RoomID text,\n        RoomOwnerID text,\n        UserCount text,\n        RoomIcon text\n    \"\"\"\n\n    @staticmethod\n    def TABLE_GROUP_USER_LIST():\n        return 'WeChatRoomMember_' + time.strftime('%Y%m%d', time.localtime())\n\n    TABLE_GROUP_USER_LIST_COL = \"\"\"\n        RoomID text,\n        MemberID text,\n        MemberNickName text,\n        MemberDisplayName text,\n        MemberAttrStatus text\n    \"\"\"\n    TABLE_RECORD_ENTER_GROUP = 'WeChatEnterGroupRecord'\n    TABLE_RECORD_ENTER_GROUP_COL = \"\"\"\n        MsgID text,\n        RoomName text,\n        FromUserName text,\n        ToUserName text,\n        Name text,\n        EnterTime text\n    \"\"\"\n    TABLE_RECORD_RENAME_GROUP = 'WeChatRenameGroupRecord'\n    TABLE_RECORD_RENAME_GROUP_COL = \"\"\"\n        MsgID text,\n        FromName text,\n        ToName text,\n        ModifyPeople text,\n        ModifyTime text\n    \"\"\"\n\n    API_APPID = 'wx782c26e4c19acffb'\n    API_WXAPPID = 'wx299208e619de7026' # Weibo\n                # 'wxeb7ec651dd0aefa9' # Weixin\n    API_LANG = 'zh_CN'\n    API_USER_AGENT = (\n        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) '\n        'AppleWebKit/537.36 (KHTML, like Gecko) '\n        'Chrome/48.0.2564.109 Safari/537.36'\n    )\n    API_SPECIAL_USER = [\n        'newsapp', 'filehelper', 'weibo', 'qqmail',\n        'fmessage', 'tmessage', 'qmessage', 'qqsync',\n        'floatbottle', 'lbsapp', 'shakeapp', 'medianote',\n        'qqfriend', 'readerapp', 'blogapp', 'facebookapp',\n        'masssendapp', 'meishiapp', 'feedsapp', 'voip',\n        'blogappweixin', 'brandsessionholder', 'weixin',\n        'weixinreminder', 'officialaccounts', 'wxitil',\n        'notification_messages', 'wxid_novlwrv3lqwv11',\n        'gh_22b87fa7cb3c', 'userexperience_alarm',\n    ]\n\n    EMOTICON = [\n        '[Smile]', '[Grimace]', '[Drool]', '[Scowl]', '[CoolGuy]', '[Sob]', '[Shy]',\n        '[Silent]', '[Sleep]', '[Cry]', '[Awkward]', '[Angry]', '[Tongue]', '[Grin]',\n        '[Surprise]', '[Frown]', '[Ruthless]', '[Blush]', '[Scream]', '[Puke]',\n        '[Chuckle]', '[Joyful]', '[Slight]', '[Smug]', '[Hungry]', '[Drowsy]', '[Panic]',\n        '[Sweat]', '[Laugh]', '[Commando]', '[Determined]', '[Scold]', '[Shocked]', '[Shhh]',\n        '[Dizzy]', '[Tormented]', '[Toasted]', '[Skull]', '[Hammer]', '[Wave]',\n        '[Relief]', '[DigNose]', '[Clap]', '[Shame]', '[Trick]',' [Bah！L]','[Bah！R]',\n        '[Yawn]', '[Lookdown]', '[Wronged]', '[Puling]', '[Sly]', '[Kiss]', '[Uh-oh]',\n        '[Whimper]', '[Cleaver]', '[Melon]', '[Beer]', '[Basketball]', '[PingPong]',\n        '[Coffee]', '[Rice]', '[Pig]', '[Rose]', '[Wilt]', '[Lip]', '[Heart]',\n        '[BrokenHeart]', '[Cake]', '[Lightning]', '[Bomb]', '[Dagger]', '[Soccer]', '[Ladybug]',\n        '[Poop]', '[Moon]', '[Sun]', '[Gift]', '[Hug]', '[Strong]',\n        '[Weak]', '[Shake]', '[Victory]', '[Admire]', '[Beckon]', '[Fist]', '[Pinky]',\n        '[Love]', '[No]', '[OK]', '[InLove]', '[Blowkiss]', '[Waddle]', '[Tremble]',\n        '[Aaagh!]', '[Twirl]', '[Kotow]', '[Lookback]', '[Jump]', '[Give-in]',\n        u'\\U0001f604', u'\\U0001f637', u'\\U0001f639', u'\\U0001f61d', u'\\U0001f632', u'\\U0001f633',\n        u'\\U0001f631', u'\\U0001f64d', u'\\U0001f609', u'\\U0001f60c', u'\\U0001f612', u'\\U0001f47f',\n        u'\\U0001f47b', u'\\U0001f49d', u'\\U0001f64f', u'\\U0001f4aa', u'\\U0001f4b5', u'\\U0001f382',\n        u'\\U0001f388', u'\\U0001f4e6',\n    ]\n    BOT_ZHIHU_URL_LATEST = 'http://news-at.zhihu.com/api/4/news/latest'\n    BOT_ZHIHU_URL_DAILY = 'http://daily.zhihu.com/story/'\n    BOT_TULING_API_KEY = '55e7f30895a0a10535984bae5ad294d1'\n    BOT_TULING_API_URL = 'http://www.tuling123.com/openapi/api?key=%s&info=%s&userid=%s'\n    BOT_TULING_BOT_REPLY = u'麻烦说的清楚一点，我听不懂你在说什么'\n"
  },
  {
    "path": "wxbot_project_py2.7/config/log.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom constant import Constant\nfrom config import ConfigManager\n#---------------------------------------------------\nimport logging\nimport logging.config\n#===================================================\n\ncm = ConfigManager()\n\nlogging.config.fileConfig(Constant.WECHAT_CONFIG_FILE)\n# create logger\nLog = logging.getLogger(Constant.LOGGING_LOGGER_NAME)\n\n# 'application' code\n# Log.debug('debug message')\n# Log.info('info message')\n# Log.warn('warn message')\n# Log.error('error message')\n# Log.critical('critical message')\n"
  },
  {
    "path": "wxbot_project_py2.7/config/requirements.txt",
    "content": "qrcode\nflask\nrequests\nrequests_toolbelt\npymysql\nsendgrid\n"
  },
  {
    "path": "wxbot_project_py2.7/config/wechat.conf.bak",
    "content": "[wechat]\nhost = wx.qq.com\nuuid = \nredirect_uri = \nuin = \nsid = \nskey = \npass_ticket = \ndevice_id = \nlast_login = \n\n[setting]\nprefix = tmp_data/\ndatabase = WeChat.db\ndatadir = Data/infos/\nlogdir = Logs\nmediapath = Data\nuploaddir = Data/upload\nqrcodedir = Data/qrcode\nserver_port = 8080\ncookie = Cookie/WeChat.cookie\ncontact_user = Pickle/User.pkl\ncontact_member_list = Pickle/MemberList.pkl\ncontact_group_list = Pickle/GroupList.pkl\ncontact_group_memeber_list = Pickle/GroupMemeberList.pkl\ncontact_special_users_list = Pickle/SpecialUsersList.pkl\nserver_mode = False\nserver_log_file = server.log\nlog_mode = False\n\n[mysql]\nhost = localhost\nport = 3306\nuser = root\npasswd = root\ndatabase = wechat\n\n[sendgrid]\napi_key = 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\nfrom_email = wxbot@wechat.com\nto_email = xxx@example.com\n\n[loggers]\nkeys = root,WeChat\n\n[handlers]\nkeys = consoleHandler,fileHandler\n\n[formatters]\nkeys = simpleFormatter\n\n[logger_root]\nlevel = DEBUG\nhandlers = consoleHandler\n\n[logger_WeChat]\nlevel = DEBUG\nhandlers = fileHandler\nqualname = WeChat\npropagate = 0\n\n[handler_consoleHandler]\nclass = StreamHandler\nlevel = DEBUG\nformatter = simpleFormatter\nargs = (sys.stdout,)\n\n[handler_fileHandler]\nclass = FileHandler\nlevel = DEBUG\nformatter = simpleFormatter\nargs = ('tmp_data/wechat.log',)\n\n[formatter_simpleFormatter]\nformat = %(asctime)s - %(name)s - %(levelname)s - %(message)s\ndatefmt = \n\n"
  },
  {
    "path": "wxbot_project_py2.7/db/__init__.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\nfrom sqlite_db import SqliteDB\nfrom mysql_db import MysqlDB"
  },
  {
    "path": "wxbot_project_py2.7/db/mysql_db.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom config import Log\n#---------------------------------------------------\nimport pymysql\nimport threading\nimport traceback\n#===================================================\n\ndef array_join(arr, c):\n    t = ''\n    for a in arr:\n        t += \"'%s'\" % str(a).replace(\"'\",\"\\\\\\'\") + c\n    return t[:-len(c)]\n\nclass MysqlDB(object):\n    \"\"\"\n    修改服务器上的配置文件/etc/my.cnf，在对应位置添加以下设置:\n    [client]\n    default-character-set = utf8mb4\n\n    [mysql]\n    default-character-set = utf8mb4\n\n    [mysqld]\n    character-set-client-handshake = FALSE\n    character-set-server = utf8mb4\n    collation-server = utf8mb4_unicode_ci\n    init_connect='SET NAMES utf8mb4'\n    \"\"\"\n\n    def __init__(self, conf):\n        self.conf = conf\n        config = {\n            'host': conf['host'],\n            'port': conf['port'],\n            'user': conf['user'],\n            'passwd': conf['passwd'],\n            'charset':'utf8mb4', # 支持1-4个字节字符\n            'cursorclass': pymysql.cursors.DictCursor\n        }\n        self.conn = pymysql.connect(**config)\n        self.conn.autocommit(1)\n        # for thread-save\n        self.lock = threading.Lock()\n\n        self.create_db(conf['database'])\n        self.conn.select_db(conf['database'])\n\n        # cache table cols\n        self.table_cols = {}\n        for t in self.show_tables():\n            self.table_cols[t] = self.get_table_column_name(t)\n\n    def show_database(self):\n        c = self.conn.cursor()\n        sql = 'SHOW DATABASES'\n        Log.debug('DB -> %s' % sql)\n        c.execute(sql)\n        return [r['Database'] for r in c.fetchall()]\n\n    def show_tables(self):\n        c = self.conn.cursor()\n        sql = 'SHOW TABLES'\n        Log.debug('DB -> %s' % sql)\n        c.execute(sql)\n        return [r['Tables_in_'+self.conf['database']] for r in c.fetchall()]\n\n    def create_db(self, db_name):\n        \"\"\"\n        @brief      Creates a database\n        @param      db_name  String\n        \"\"\"\n        if self.conf['database'] not in self.show_database():\n            sql = 'CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' % db_name\n            Log.debug('DB -> %s' % sql)\n            self.execute(sql)\n\n    def create_table(self, table, cols):\n        \"\"\"\n        @brief      Creates a table in database\n        @param      table  String\n        @param      cols   String, the cols in table\n        \"\"\"\n        if table not in self.table_cols:\n            sql = 'CREATE TABLE IF NOT EXISTS %s(id int primary key auto_increment, %s) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci' % (table, cols)\n            Log.debug('DB -> %s' % sql)\n            self.execute(sql)\n            self.table_cols[table] = ['id'] + [c.strip().split(' ')[0] for c in cols.split(',')]\n\n    def delete_table(self, table):\n        \"\"\"\n        @brief      Delete a table in database\n        @param      table  String\n        \"\"\"\n        if table in self.table_cols:\n            sql = \"DROP TABLE IF EXISTS %s\" % table\n            Log.debug('DB -> %s' % sql)\n            self.execute(sql)\n            self.table_cols.pop(table)\n\n    def insert(self, table, value):\n        \"\"\"\n        @brief      Insert a row in table\n        @param      table  String\n        @param      value  Tuple\n        \"\"\"\n        col_name = self.table_cols[table][1:]\n        sql = \"INSERT INTO %s(%s) VALUES (%s)\" % (table, str(','.join(col_name)), array_join(value, ','))\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql)\n\n    def insertmany(self, table, values):\n        \"\"\"\n        @brief      Insert many rows in table\n        @param      table  String\n        @param      values  Array of tuple\n        \"\"\"\n        col_name = self.table_cols[table][1:]\n        sql = 'INSERT INTO %s(%s) VALUES (%s)' % (table, ','.join(col_name), ','.join(['%s'] * len(values[0])))\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql, values)\n\n    def select(self, table, field='', condition=''):\n        \"\"\"\n        @brief      select all result from table\n        @param      table  String\n        @param      field  String\n        @param      condition  String\n        @return     result  Tuple\n        \"\"\"\n        sql = \"SELECT * FROM %s\" % table\n        if field and condition:\n            sql += \" WHERE %s='%s'\" % (field, condition)\n        Log.debug('DB -> %s' % sql)\n        return self.execute(sql)\n\n    def get_table_column_name(self, table):\n        \"\"\"\n        @brief      select all result from table\n        @param      table  String\n        @return     result  Array\n        \"\"\"\n        c = self.conn.cursor()\n        c.execute(\"SELECT * FROM %s\" % table)\n        names = list(map(lambda x: x[0], c.description))\n        return names\n\n    def execute(self, sql, values=None):\n        \"\"\"\n        @brief      execute sql commands, return result if it has\n        @param      sql  String\n        @param      value  Tuple\n        @return     result  Array\n        \"\"\"\n        c = self.conn.cursor()\n        self.lock.acquire()\n        hasReturn = sql.lstrip().upper().startswith(\"SELECT\")\n\n        result = []\n        try:\n            if values:\n                c.executemany(sql, values)\n            else:\n                c.execute(sql)\n\n            if hasReturn:\n                result = c.fetchall()\n\n        except Exception, e:\n            Log.error(traceback.format_exc())\n            self.conn.rollback()\n        finally:\n            self.lock.release()\n\n        if hasReturn:\n            return result\n\n    def delete(self, table, field='', condition=''):\n        \"\"\"\n        @brief      execute sql commands, return result if it has\n        @param      table  String\n        @param      field  String\n        @param      condition  String\n        \"\"\"\n        sql = \"DELETE FROM %s WHERE %s=%s\" % (table, field, condition)\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql)\n\n    def close(self):\n        \"\"\"\n        @brief      close connection to database\n        \"\"\"\n        Log.debug('DB -> close')\n        # 关闭数据库连接\n        self.conn.close()\n\n        "
  },
  {
    "path": "wxbot_project_py2.7/db/sqlite_db.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom config import Log\n#---------------------------------------------------\nimport sqlite3\nimport threading\nimport traceback\n#===================================================\n\n\ndef _dict_factory(cursor, row):\n    aDict = {}\n    for iField, field in enumerate (cursor.description):\n        aDict [field [0]] = row [iField]\n    return aDict\n\n\nclass SqliteDB(object):\n\n    def __init__(self, db_file):\n        self.db_file = db_file\n        # self.conn = sqlite3.connect(db_file, check_same_thread=False)\n        # use 8-bit strings instead of unicode string\n        self.conn.text_factory = str\n        # not return a tuple but a dict with column name as key\n        self.conn.row_factory = _dict_factory\n        # for thread-save\n        self.lock = threading.Lock()\n\n    def set_conn(self):\n        self._conn = sqlite3.connect(self.db_file,check_same_thread=False)\n\n    @property\n    def conn(self):\n        try:\n            self._conn.execute('select 1;')\n            # check out conn\n        except (sqlite3.ProgrammingError,AttributeError):\n            # Cannot operate on a closed database\n            self.set_conn()\n\n        finally:\n            return self._conn\n\n\n    def create_table(self, table, cols):\n        \"\"\"\n        @brief      Creates a table in database\n        @param      table  String\n        @param      cols   String, the cols in table\n        \"\"\"\n        sql = \"CREATE TABLE if not exists %s (%s);\" % (table, cols)\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql)\n\n    def delete_table(self, table):\n        \"\"\"\n        @brief      Delete a table in database\n        @param      table  String\n        \"\"\"\n        sql = \"DROP TABLE if exists %s;\" % table\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql)\n\n    def insert(self, table, value):\n        \"\"\"\n        @brief      Insert a row in table\n        @param      table  String\n        @param      value  Tuple\n        \"\"\"\n        sql = (\"INSERT INTO %s VALUES (\" + \",\".join(['?'] * len(value)) + \");\") % table\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql, value)\n\n    def insertmany(self, table, values):\n        \"\"\"\n        @brief      Insert many rows in table\n        @param      table  String\n        @param      values  Array of tuple\n        \"\"\"\n        c = self.conn.cursor()\n        self.lock.acquire()\n        n = len(values[0])\n        sql = (\"INSERT INTO %s VALUES (\" + \",\".join(['?'] * n) + \");\") % table\n        Log.debug('DB -> %s' % sql)\n\n        try:\n            c.executemany(sql, values)\n        except Exception, e:\n            Log.error(traceback.format_exc())\n        finally:\n            self.lock.release()\n\n        self.conn.commit()\n\n    def select(self, table, field='', condition=''):\n        \"\"\"\n        @brief      select all result from table\n        @param      table  String\n        @param      field  String\n        @param      condition  String\n        @return     result  Tuple\n        \"\"\"\n        result = []\n        if field and condition:\n            cond = (condition,)\n            sql = \"SELECT * FROM %s WHERE %s=?\" % (table, field)\n            Log.debug('DB -> %s' % sql)\n            result = self.execute(sql, cond)\n        else:\n            sql = \"SELECT * FROM %s\" % table\n            Log.debug('DB -> %s' % sql)\n            result = self.execute(sql)\n        return result\n\n    def update(self, table, dic, condition=''):\n        k_arr = []\n        v_arr = []\n        for (k, v) in dic.items():\n            k_arr.append('%s=?' % k)\n            v_arr.append(v)\n\n        sql = \"UPDATE %s SET %s\" % (table, ','.join(k_arr))\n        if condition:\n            sql += \" WHERE %s\" % condition\n\n        Log.debug('DB -> %s' % sql)\n        self.execute(sql, tuple(v_arr))\n\n    def get_table_column_name(self, table):\n        \"\"\"\n        @brief      select all result from table\n        @param      table  String\n        @return     result  Array\n        \"\"\"\n        c = self.conn.cursor()\n        c.execute(\"SELECT * FROM %s\" % table)\n        names = list(map(lambda x: x[0], c.description))\n        return names\n\n    def execute(self, sql, value=None):\n        \"\"\"\n        @brief      execute sql commands, return result if it has\n        @param      sql  String\n        @param      value  Tuple\n        @return     result  Array\n        \"\"\"\n        c = self.conn.cursor()\n        self.lock.acquire()\n        hasReturn = sql.lstrip().upper().startswith(\"SELECT\")\n\n        try:\n            if value:\n                c.execute(sql, value)\n            else:\n                c.execute(sql)\n\n            if hasReturn:\n                result = c.fetchall()\n        except Exception, e:\n            Log.error(traceback.format_exc())\n        finally:\n            self.lock.release()\n\n        self.conn.commit()\n\n        if hasReturn:\n            return result\n\n    def delete(self, table, field='', condition=''):\n        \"\"\"\n        @brief      execute sql commands, return result if it has\n        @param      table  String\n        @param      field  String\n        @param      condition  String\n        \"\"\"\n        sql = \"DELETE FROM %s WHERE %s=?\" % (table, field)\n        Log.debug('DB -> %s' % sql)\n        cond = (condition,)\n        self.execute(sql, cond)\n\n    def close(self):\n        \"\"\"\n        @brief      close connection to database\n        \"\"\"\n        Log.debug('DB -> close')\n        self.conn.close()\n"
  },
  {
    "path": "wxbot_project_py2.7/docker/Dockerfile",
    "content": "FROM ubuntu:16.04\nMAINTAINER Urinx <uri.lqy@gmail.com>\n\nRUN apt-get update && \\\n    apt-get install -y python \\\n                      python-dev \\\n                      python-pip && \\\n    apt-get clean && \\\n    apt-get autoclean && \\\n    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*\n\nADD weixin_bot.tar.gz /\nWORKDIR /weixin_bot\n\nRUN pip install -r config/requirements.txt\nEXPOSE 80\nENTRYPOINT [\"./weixin_bot.py\"]\nCMD [\"\"]\n"
  },
  {
    "path": "wxbot_project_py2.7/docker/README.md",
    "content": "# 本地构建 wechat_bot docker 镜像\n\n拉下镜像:\n\n```bash\ndocker pull ubuntu:16.04\n```\n\n打包本项目，将压缩包放到`docker`目录下:\n\n```\ntar -czf weixin_bot.tar.gz wxbot_project_py2.7/\n```\n\n切换到`docker`目录，执行`build`命令:\n\n```bash\ndocker build -t wechat-bot .\n```\n\n导出镜像:\n\n```bash\ndocker save wechat-bot > wechat.tar\n```\n\n导入镜像:\n\n```bash\ndocker load < wechat.tar\n```\n\n运行:\n\n```bash\ndocker run -d -P --name xxx -v /src/data/dir:/Wechat_bot/test wechat-bot\n```\n\n删除镜像:\n\n```bash\ndocker rmi -f wechat-bot\n```\n\n查看log:\n\n```bash\ndocker log wechat-bot\n```\n"
  },
  {
    "path": "wxbot_project_py2.7/flask_templates/index.html",
    "content": "<!DOCTYPE html>\n<html>\n<head>\n\t<meta charset=\"utf-8\">\n\t<title>WeChat Bot Server</title>\n\t<style type=\"text/css\">\n\t.container {\n\t\tdisplay: flex;\n\t}\n\tdiv.doc{\n\t\tmargin: auto;\n\t\ttext-align: left;\n\t}\n\t</style>\n</head>\n<body class=\"container\">\n\n<div class=\"doc\">\n<pre>\n\t#####################\n\t# WeChat Bot Server #\n\t#####################\n\n\n## APIs\n\n- /qrcode\n  ------------------------\n  @brief      login qrcode\n  @method     get\n  @return     raw image\n  ------------------------\n\n- /runtime\n  -----------------------------\n  @brief      basic info\n  @method     get\n  @return     application/json\n              {\n                'ret': 0,\n                'runtime': '',\n                'total_size': '',\n                'db_size': '',\n                'msg_count': '',\n                'image_count': '',\n                'voice_count': '',\n                'video_count': '',\n              }\n  -----------------------------\n\n- /group_list\n  ------------------------\n  @brief      list groups\n  @method     get\n  ------------------------\n\n- /group_member_list/&lt;g_id&gt;\n  -----------------------------\n  @brief      list group member\n  @method     get\n  @param      g_id String\n  -----------------------------\n\n- /group_chat_log/&lt;g_name&gt;\n  -------------------------------\n  @brief      list group chat log\n  @method     get\n  @param      g_name String\n  -------------------------------\n\n- /upload\n  -------------------------------\n  @brief      upload a file\n  @method     get/post\n  @return     application/json\n              {\n                'ret': 0,\n                'msg': '',\n              }\n  -------------------------------\n\n- /send_msg/&lt;to&gt;/&lt;msg&gt;\n  -------------------------------------------\n  @brief      send message to user or gourp\n  @method     get\n  @param      to: String, user id or group id\n  @param      msg: String, words\n  @return     application/json\n              {\n                'ret': 0,\n              }\n  -------------------------------------------\n\n- /send_img/&lt;to&gt;/&lt;img&gt;\n  -------------------------------------------\n  @brief      send image to user or gourp\n  @method     get\n  @param      to: String, user id or group id\n  @param      img: String, image file name\n  @return     application/json\n              {\n                'ret': 0,\n              }\n  -------------------------------------------\n\n- /send_emot/&lt;to&gt;/&lt;emot&gt;\n  -------------------------------------------\n  @brief      send emotion to user or gourp\n  @method     get\n  @param      to: String, user id or group id\n  @param      emot: String, emotion file name\n  @return     application/json\n              {\n                'ret': 0,\n              }\n  -------------------------------------------\n\n- /send_file/&lt;to&gt;/&lt;file&gt;\n  -------------------------------------------\n  @brief      send emotion to user or gourp\n  @method     get\n  @param      to: String, user id or group id\n  @param      file: String, file name\n  @return     application/json\n              {\n                'ret': 0,\n              }\n  -------------------------------------------\n\n- /mass_send_msg\n  -------------------------------------------\n  @brief      send text to mass users or gourps\n  @method     post\n  @param      application/json\n              {\n                'to_list': [\n                  'group_id',\n                  ...\n                ],\n                'msg': '',\n              }\n  @return     application/json\n              {\n                'ret': 0,\n                'unsend_list': [],\n              }\n  -------------------------------------------\n\n- /mass_send_img\n  -------------------------------------------\n  @brief      send image to mass users or gourps\n  @method     post\n  @param      application/json\n              {\n                'to_list': [\n                  'group_id',\n                  ...\n                ],\n                'msg': '',\n              }\n  @return     application/json\n              {\n                'ret': 0,\n                'unsend_list': [],\n              }\n  -------------------------------------------\n\n- /mass_send_emot\n  -------------------------------------------\n  @brief      send emoticon to mass users or gourps\n  @method     post\n  @param      application/json\n              {\n                'to_list': [\n                  'group_id',\n                  ...\n                ],\n                'msg': '',\n              }\n  @return     application/json\n              {\n                'ret': 0,\n                'unsend_list': [],\n              }\n  -------------------------------------------\n\n- /mass_send_file\n  -------------------------------------------\n  @brief      send file to mass users or gourps\n  @method     post\n  @param      application/json\n              {\n                'to_list': [\n                  'group_id',\n                  ...\n                ],\n                'msg': '',\n              }\n  @return     application/json\n              {\n                'ret': 0,\n                'unsend_list': [],\n              }\n  -------------------------------------------\n</pre>\n</div>\n\n</body>\n</html>\n\n"
  },
  {
    "path": "wxbot_project_py2.7/flask_templates/upload.html",
    "content": "<!doctype html>\n<title>Upload new File</title>\n<h1>Upload new File</h1>\n<form action=\"upload\" method=post enctype=multipart/form-data>\n  <p><input type=file name=file>\n     <input type=submit value=Upload>\n</form>"
  },
  {
    "path": "wxbot_project_py2.7/wechat/__init__.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\nfrom wechat import WeChat"
  },
  {
    "path": "wxbot_project_py2.7/wechat/utils.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom config import Log\nfrom config import Constant\n#---------------------------------------------------\nimport qrcode\nimport re\nimport os\nimport sys\nimport json\nimport urllib\nimport urllib2\nimport cookielib\nimport cPickle as pickle\nimport traceback\nimport time\nimport hashlib\n#===================================================\n\n\ndef _decode_data(data):\n    \"\"\"\n    @brief      decode array or dict to utf-8\n    @param      data   array or dict\n    @return     utf-8\n    \"\"\"\n    if isinstance(data, dict):\n        rv = {}\n        for key, value in data.iteritems():\n            if isinstance(key, unicode):\n                key = key.encode('utf-8')\n            rv[key] = _decode_data(value)\n        return rv\n    elif isinstance(data, list):\n        rv = []\n        for item in data:\n            item = _decode_data(item)\n            rv.append(item)\n        return rv\n    elif isinstance(data, unicode):\n        return data.encode('utf-8')\n    else:\n        return data\n\n\ndef str2qr_terminal(text):\n    \"\"\"\n    @brief      convert string to qrcode matrix and outprint\n    @param      text   The string\n    \"\"\"\n    Log.debug(text)\n    qr = qrcode.QRCode()\n    qr.border = 1\n    qr.add_data(text)\n    mat = qr.get_matrix()\n    print_qr(mat)\n\n\ndef str2qr_image(text, image_path):\n    \"\"\"\n    @brief      convert string to qrcode image & save\n    @param      text         The string\n    @param      image_path   Save image to the path\n    \"\"\"\n    qr = qrcode.QRCode()\n    qr.border = 1\n    qr.add_data(text)\n    qr.make(fit=True)\n    img = qr.make_image()\n    img.save(image_path)\n\n\ndef print_qr(mat):\n    for i in mat:\n        BLACK = Constant.QRCODE_BLACK\n        WHITE = Constant.QRCODE_WHITE\n        echo(''.join([BLACK if j else WHITE for j in i])+'\\n')\n\n\ndef echo(str):\n    Log.info(str[:-1])\n    sys.stdout.write(str)\n    sys.stdout.flush()\n\n\ndef run(str, func, *args):\n    t = time.time()\n    echo(str)\n    r = False\n    try:\n        r = func(*args)\n    except:\n        Log.error(traceback.format_exc())\n    if r:\n        totalTime = int(time.time() - t)\n        echo(Constant.RUN_RESULT_SUCCESS % totalTime)\n    else:\n        echo(Constant.RUN_RESULT_FAIL)\n        exit()\n\n\ndef get(url, api=None):\n    \"\"\"\n    @brief      http get request\n    @param      url   String\n    @param      api   wechat api\n    @return     http response\n    \"\"\"\n    Log.debug('GET -> ' + url)\n    request = urllib2.Request(url=url)\n    request.add_header(*Constant.HTTP_HEADER_CONNECTION)\n    request.add_header(*Constant.HTTP_HEADER_REFERER)\n    if api in ['webwxgetvoice', 'webwxgetvideo']:\n        request.add_header(*Constant.HTTP_HEADER_RANGE)\n\n    while True:\n        try:\n            response = urllib2.urlopen(request, timeout=30)\n            data = response.read()\n            response.close()\n            if api == None:\n                Log.debug(data)\n            return data\n        except (KeyboardInterrupt, SystemExit):\n            raise\n        except:\n            Log.error(traceback.format_exc())\n\n        time.sleep(1)\n\n\ndef post(url, params, jsonfmt=True):\n    \"\"\"\n    @brief      http post request\n    @param      url      String\n    @param      params   Dict, post params\n    @param      jsonfmt  Bool, whether is json format\n    @return     http response\n    \"\"\"\n    Log.debug('POST -> '+url)\n    Log.debug(params)\n    if jsonfmt:\n        request = urllib2.Request(url=url, data=json.dumps(params, ensure_ascii=False).encode('utf8'))\n        request.add_header(*Constant.HTTP_HEADER_CONTENTTYPE)\n    else:\n        request = urllib2.Request(url=url, data=urllib.urlencode(params))\n\n    while True:\n        try:\n            response = urllib2.urlopen(request, timeout=30)\n            data = response.read()\n            response.close()\n\n            if jsonfmt:\n                Log.debug(data)\n                return json.loads(data, object_hook=_decode_data)\n            return data\n        except (KeyboardInterrupt, SystemExit):\n            raise\n        except:\n            Log.error(traceback.format_exc())\n\n        time.sleep(1)\n\n\ndef set_cookie(cookie_file):\n    \"\"\"\n    @brief      Load cookie from file\n    @param      cookie_file\n    @param      user_agent\n    @return     cookie, LWPCookieJar\n    \"\"\"\n    cookie = cookielib.LWPCookieJar(cookie_file)\n    try:\n        cookie.load(ignore_discard=True)\n    except:\n        Log.error(traceback.format_exc())\n    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))\n    opener.addheaders = Constant.HTTP_HEADER_USERAGENT\n    urllib2.install_opener(opener)\n    return cookie\n\n\ndef generate_file_name(filename):\n    \"\"\"\n    @brief      generate file name\n    @return     new file name\n    \"\"\"\n    i = filename.rfind('.')\n    ext = filename[i:]\n    tmp = filename + str(int(time.time()))\n    hash_md5 = hashlib.md5(tmp)\n    return hash_md5.hexdigest() + ext\n\n\ndef save_file(filename, data, dirName):\n    \"\"\"\n    @brief      Saves raw data to file.\n    @param      filename  String\n    @param      data      Binary data\n    @param      dirName   String\n    @return     file path\n    \"\"\"\n    Log.debug('save file: ' + filename)\n    fn = filename\n    if not os.path.exists(dirName):\n        os.makedirs(dirName)\n    fn = os.path.join(dirName, filename)\n    with open(fn, 'wb') as f:\n        f.write(data)\n    return fn\n\n\ndef save_json(filename, data, dirName, mode='w+'):\n    \"\"\"\n    @brief      Saves dict to json file.\n    @param      filename  String\n    @param      data      Dict\n    @param      dirName   String\n    @return     file path\n    \"\"\"\n    Log.debug('save json: ' + filename)\n    fn = filename\n    if not os.path.exists(dirName):\n        os.makedirs(dirName)\n    fn = os.path.join(dirName, filename)\n    with open(fn, mode) as f:\n        f.write(json.dumps(data, indent=4)+'\\n')\n    return fn\n\n\ndef load_json(filepath):\n    Log.debug('load json: ' + filepath)\n    with open(filepath, 'r') as f:\n        return _decode_data(json.loads(f.read()))\n\n\ndef pickle_save(data, file):\n    \"\"\"\n    @brief      Use pickle to save python object into file\n    @param      data  The pyhton data\n    @param      file  The file\n    \"\"\"\n    basedir = os.path.dirname(file)\n    if not os.path.exists(basedir):\n        os.makedirs(basedir)\n    with open(file, 'wb') as f:\n        pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)\n\n\ndef pickle_load(file):\n    \"\"\"\n    @brief      Use pickle to load python object from file\n    @param      file  The file\n    @return     python data\n    \"\"\"\n    if os.path.isfile(file):\n        with open(file, 'rb') as f:\n            return pickle.load(f)\n    return None\n\n\ndef search_content(key, content, fmat='attr'):\n    \"\"\"\n    @brief      Search content from xml or html format\n    @param      key      String\n    @param      content  String\n    @param      fmat     attr\n                         xml\n    @return     String\n    \"\"\"\n    if fmat == 'attr':\n        pm = re.search(key + '\\s?=\\s?\"([^\"<]+)\"', content)\n        if pm:\n            return pm.group(1)\n    elif fmat == 'xml':\n        pm = re.search('<{0}>([^<]+)</{0}>'.format(key), content)\n        if not pm:\n            pm = re.search(\n                '<{0}><\\!\\[CDATA\\[(.*?)\\]\\]></{0}>'.format(key), content)\n        if pm:\n            return pm.group(1)\n    return 'unknown'\n\n\ndef is_str(s):\n    \"\"\"\n    @brief      Determines if string.\n    @param      s     String\n    @return     True if string, False otherwise.\n    \"\"\"\n    return isinstance(s, basestring)\n\n\ndef trans_coding(data):\n    \"\"\"\n    @brief      Transform string to unicode\n    @param      data  String\n    @return     unicode\n    \"\"\"\n    if not data:\n        return data\n    result = None\n    if type(data) == unicode:\n        result = data\n    elif type(data) == str:\n        result = data.decode('utf-8')\n    return result\n\n\ndef trans_emoji(text):\n    \"\"\"\n    @brief      Transform emoji html text to unicode\n    @param      text  String\n    @return     emoji unicode\n    \"\"\"\n    def _emoji(matched):\n        hex = matched.group(1)\n        return ('\\\\U%08x' % int(hex, 16)).decode('unicode-escape').encode('utf-8')\n\n    replace_t = re.sub(Constant.REGEX_EMOJI, _emoji, text)\n    return replace_t\n\n\ndef auto_reload(mod):\n    \"\"\"\n    @brief      reload modules\n    @param      mod: the need reload modules\n    \"\"\"\n    try:\n        module = sys.modules[mod]\n    except:\n        Log.error(traceback.format_exc())\n        return False\n\n    filename = module.__file__\n    # .pyc 修改时间不会变\n    # 所以就用 .py 的修改时间\n    if filename.endswith(\".pyc\"):\n        filename = filename.replace(\".pyc\", \".py\")\n    mod_time = os.path.getmtime(filename)\n    if not \"loadtime\" in module.__dict__:\n        module.loadtime = 0\n\n    try:\n        if mod_time > module.loadtime:\n            reload(module)\n        else:\n            return False\n    except:\n        Log.error(traceback.format_exc())\n        return False\n\n    module.loadtime = mod_time\n\n    echo('[*] load \\'%s\\' successful.\\n' % mod)\n    return True\n\n\ndef split_array(arr, n):\n    for i in xrange(0, len(arr), n):\n        yield arr[i:i+n]\n\n"
  },
  {
    "path": "wxbot_project_py2.7/wechat/wechat.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom utils import *\nfrom wechat_apis import WXAPI\nfrom config import ConfigManager\nfrom config import Constant\nfrom config import Log\n#---------------------------------------------------\nimport json\nimport re\nimport sys\nimport os\nimport time\nimport random\nfrom collections import defaultdict\nfrom datetime import timedelta\nimport traceback\nimport Queue\nimport threading\n#===================================================\n\n\nclass WeChat(WXAPI):\n\n    def __str__(self):\n        description = \\\n            \"=========================\\n\" + \\\n            \"[#] Web WeChat\\n\" + \\\n            \"[#] UUID: \" + self.uuid + \"\\n\" + \\\n            \"[#] Uin: \" + str(self.uin) + \"\\n\" + \\\n            \"[#] Sid: \" + self.sid + \"\\n\" + \\\n            \"[#] Skey: \" + self.skey + \"\\n\" + \\\n            \"[#] DeviceId: \" + self.device_id + \"\\n\" + \\\n            \"[#] PassTicket: \" + self.pass_ticket + \"\\n\" + \\\n            \"[#] Run Time: \" + self.get_run_time() + '\\n' + \\\n            \"=========================\"\n        return description\n\n    def __init__(self, host='wx.qq.com'):\n        super(WeChat, self).__init__(host)\n\n        self.db = None\n        self.save_data_folder = ''  # 保存图片，语音，小视频的文件夹\n        self.last_login = 0  # 上次退出的时间\n        self.time_out = 5  # 同步时间间隔（单位：秒）\n        self.msg_handler = None\n        self.start_time = time.time()\n        self.bot = None\n\n        cm = ConfigManager()\n        self.save_data_folders = cm.get_wechat_media_dir()\n        self.cookie_file = cm.get_cookie()\n        self.pickle_file = cm.get_pickle_files()\n        self.log_mode = cm.get('setting', 'log_mode') == 'True'\n        self.exit_code = 0\n\n    def start(self):\n        echo(Constant.LOG_MSG_START)\n        run(Constant.LOG_MSG_RECOVER, self.recover)\n\n        timeOut = time.time() - self.last_login\n        echo(Constant.LOG_MSG_TRY_INIT)\n        if self.webwxinit():\n            echo(Constant.LOG_MSG_SUCCESS)\n            run(Constant.LOG_MSG_RECOVER_CONTACT, self.recover_contacts)\n        else:\n            echo(Constant.LOG_MSG_FAIL)\n\n            while True:\n                # first try to login by uin without qrcode\n                echo(Constant.LOG_MSG_ASSOCIATION_LOGIN)\n                if self.association_login():\n                    echo(Constant.LOG_MSG_SUCCESS)\n                else:\n                    echo(Constant.LOG_MSG_FAIL)\n                    # scan qrcode to login\n                    run(Constant.LOG_MSG_GET_UUID, self.getuuid)\n                    echo(Constant.LOG_MSG_GET_QRCODE)\n                    self.genqrcode()\n                    echo(Constant.LOG_MSG_SCAN_QRCODE)\n\n                if not self.waitforlogin():\n                    continue\n                echo(Constant.LOG_MSG_CONFIRM_LOGIN)\n                if not self.waitforlogin(0):\n                    continue\n                break\n\n            run(Constant.LOG_MSG_LOGIN, self.login)\n            run(Constant.LOG_MSG_INIT, self.webwxinit)\n            run(Constant.LOG_MSG_STATUS_NOTIFY, self.webwxstatusnotify)\n            run(Constant.LOG_MSG_GET_CONTACT, self.webwxgetcontact)\n            echo(Constant.LOG_MSG_CONTACT_COUNT % (\n                    self.MemberCount, len(self.MemberList)\n                ))\n            echo(Constant.LOG_MSG_OTHER_CONTACT_COUNT % (\n                    len(self.GroupList), len(self.ContactList),\n                    len(self.SpecialUsersList), len(self.PublicUsersList)\n                ))\n            run(Constant.LOG_MSG_GET_GROUP_MEMBER, self.fetch_group_contacts)\n\n        run(Constant.LOG_MSG_SNAPSHOT, self.snapshot)\n\n        while True:\n            [retcode, selector] = self.synccheck()\n            Log.debug('retcode: %s, selector: %s' % (retcode, selector))\n            self.exit_code = int(retcode)\n\n            if retcode == '1100':\n                echo(Constant.LOG_MSG_LOGOUT)\n                break\n            if retcode == '1101':\n                echo(Constant.LOG_MSG_LOGIN_OTHERWHERE)\n                break\n            if retcode == '1102':\n                echo(Constant.LOG_MSG_QUIT_ON_PHONE)\n                break\n            elif retcode == '0':\n                if selector == '2':\n                    r = self.webwxsync()\n                    if r is not None:\n                        try:\n                            self.handle_msg(r)\n                        except:\n                            Log.error(traceback.format_exc())\n                elif selector == '7':\n                    r = self.webwxsync()\n                elif selector == '0':\n                    time.sleep(self.time_out)\n                elif selector == '4':\n                    # 保存群聊到通讯录\n                    # 修改群名称\n                    # 新增或删除联系人\n                    # 群聊成员数目变化\n                    r = self.webwxsync()\n                    if r is not None:\n                        try:\n                            self.handle_mod(r)\n                        except:\n                            Log.error(traceback.format_exc())\n                elif selector == '3' or selector == '6':\n                    break\n            else:\n                r = self.webwxsync()\n                Log.debug('webwxsync: %s\\n' % json.dumps(r))\n\n            # 执行定时任务\n            if self.msg_handler:\n                self.msg_handler.check_schedule_task()\n\n            # if self.bot:\n            #     r = self.bot.time_schedule()\n            #     if r:\n            #         for g in self.GroupList:\n            #             echo('[*] 推送 -> %s: %s' % (g['NickName'], r))\n            #             g_id = g['UserName']\n            #             self.webwxsendmsg(r, g_id)\n\n    def get_run_time(self):\n        \"\"\"\n        @brief      get how long this run\n        @return     String\n        \"\"\"\n        totalTime = int(time.time() - self.start_time)\n        t = timedelta(seconds=totalTime)\n        return '%s Day %s' % (t.days, t)\n\n    def stop(self):\n        \"\"\"\n        @brief      Save some data and use shell to kill this process\n        \"\"\"\n        run(Constant.LOG_MSG_SNAPSHOT, self.snapshot)\n        echo(Constant.LOG_MSG_RUNTIME % self.get_run_time())\n        # close database connect\n        self.db.close()\n\n    def fetch_group_contacts(self):\n        \"\"\"\n        @brief      Fetches all groups contacts.\n        @return     Bool: whether operation succeed.\n        @note       This function must be finished in 180s\n        \"\"\"\n        Log.debug('fetch_group_contacts')\n        # clean database\n        if self.msg_handler:\n            self.msg_handler.clean_db()\n\n        # sqlite\n        # ----------------------------------------------------\n        # group max_thread_num  max_fetch_group_num    time(s)\n        # 197      10                 10               108\n        # 197      10                 15               95\n        # 197      20                 10               103\n        # 197      10                 20               55\n        # 197       5                 30               39\n        # 197       4                 50               35\n        # ----------------------------------------------------\n        # mysql\n        # ----------------------------------------------------\n        # group max_thread_num  max_fetch_group_num    time(s)\n        # 197       4                 50               20\n        # ----------------------------------------------------\n\n        max_thread_num = 4\n        max_fetch_group_num = 50\n        group_list_queue = Queue.Queue()\n\n        class GroupListThread(threading.Thread):\n\n            def __init__(self, group_list_queue, wechat):\n                threading.Thread.__init__(self)\n                self.group_list_queue = group_list_queue\n                self.wechat = wechat\n\n            def run(self):\n                while not self.group_list_queue.empty():\n                    g_list = self.group_list_queue.get()\n                    gid_list = []\n                    g_dict = {}\n                    for g in g_list:\n                        gid = g['UserName']\n                        gid_list.append(gid)\n                        g_dict[gid] = g\n\n                    group_member_list = self.wechat.webwxbatchgetcontact(gid_list)\n\n                    for member_list in group_member_list:\n                        gid = member_list['UserName']\n                        g = g_dict[gid]\n                        g['MemberCount'] = member_list['MemberCount']\n                        g['OwnerUin'] = member_list['OwnerUin']\n                        self.wechat.GroupMemeberList[gid] = member_list['MemberList']\n\n                        # 如果使用 Mysql 则可以在多线程里操作数据库\n                        # 否则请注释下列代码在主线程里更新群列表\n                        # -----------------------------------\n                        # 处理群成员\n                        # if self.wechat.msg_handler:\n                        #     self.wechat.msg_handler.handle_group_member_list(gid, member_list['MemberList'])\n                        # -----------------------------------\n\n                    self.group_list_queue.task_done()\n\n        for g_list in split_array(self.GroupList, max_fetch_group_num):\n            group_list_queue.put(g_list)\n\n        for i in range(max_thread_num):\n            t = GroupListThread(group_list_queue, self)\n            t.setDaemon(True)\n            t.start()\n\n        group_list_queue.join()\n\n        if self.msg_handler:\n            # 处理群\n            if self.GroupList:\n                self.msg_handler.handle_group_list(self.GroupList)\n\n            # 这个是用 sqlite 来存储群列表，sqlite 对多线程的支持不太好\n            # ----------------------------------------------------\n            # 处理群成员\n            for (gid, member_list) in self.GroupMemeberList.items():\n                self.msg_handler.handle_group_member_list(gid, member_list)\n            # ----------------------------------------------------\n\n        return True\n\n    def snapshot(self):\n        \"\"\"\n        @brief      Save basic infos for next login.\n        @return     Bool: whether operation succeed.\n        \"\"\"\n        try:\n            conf = {\n                'uuid': self.uuid,\n                'redirect_uri': self.redirect_uri,\n                'uin': self.uin,\n                'sid': self.sid,\n                'skey': self.skey,\n                'pass_ticket': self.pass_ticket,\n                'synckey': self.synckey,\n                'device_id': self.device_id,\n                'last_login': time.time(),\n            }\n            cm = ConfigManager()\n            Log.debug('save wechat config')\n            cm.set_wechat_config(conf)\n\n            # save cookie\n            Log.debug('save cookie')\n            if self.cookie:\n                self.cookie.save(ignore_discard=True)\n\n            # save contacts\n            Log.debug('save contacts')\n            self.save_contacts()\n        except Exception, e:\n            Log.error(traceback.format_exc())\n            return False\n        return True\n\n    def recover(self):\n        \"\"\"\n        @brief      Recover from snapshot data.\n        @return     Bool: whether operation succeed.\n        \"\"\"\n        cm = ConfigManager()\n        [self.uuid, self.redirect_uri, self.uin,\n        self.sid, self.skey, self.pass_ticket,\n        self.synckey, device_id, self.last_login] = cm.get_wechat_config()\n\n        if device_id:\n            self.device_id = device_id\n\n        self.base_request = {\n            'Uin': int(self.uin),\n            'Sid': self.sid,\n            'Skey': self.skey,\n            'DeviceID': self.device_id,\n        }\n\n        # set cookie\n        Log.debug('set cookie')\n        self.cookie = set_cookie(self.cookie_file)\n\n        return True\n\n    def save_contacts(self):\n        \"\"\"\n        @brief      Save contacts.\n        \"\"\"\n        pickle_save(self.User, self.pickle_file['User'])\n        pickle_save(self.MemberList, self.pickle_file['MemberList'])\n        pickle_save(self.GroupList, self.pickle_file['GroupList'])\n        pickle_save(self.GroupMemeberList, self.pickle_file['GroupMemeberList'])\n        pickle_save(self.SpecialUsersList, self.pickle_file['SpecialUsersList'])\n\n    def recover_contacts(self):\n        \"\"\"\n        @brief      recover contacts.\n        @return     Bool: whether operation succeed.\n        \"\"\"\n        try:\n            self.User = pickle_load(self.pickle_file['User'])\n            self.MemberList = pickle_load(self.pickle_file['MemberList'])\n            self.GroupList = pickle_load(self.pickle_file['GroupList'])\n            self.GroupMemeberList = pickle_load(self.pickle_file['GroupMemeberList'])\n            self.SpecialUsersList = pickle_load(self.pickle_file['SpecialUsersList'])\n            return True\n        except Exception, e:\n            Log.error(traceback.format_exc())\n        return False\n\n    def handle_mod(self, r):\n        # ModContactCount: 变更联系人或群聊成员数目\n        # ModContactList: 变更联系人或群聊列表，或群名称改变\n        Log.debug('handle modify')\n        self.handle_msg(r)\n        for m in r['ModContactList']:\n            if m['UserName'][:2] == '@@':\n                # group\n                in_list = False\n                g_id = m['UserName']\n                for g in self.GroupList:\n                    # group member change\n                    if g_id == g['UserName']:\n                        g['MemberCount'] = m['MemberCount']\n                        g['NickName'] = m['NickName']\n                        self.GroupMemeberList[g_id] = m['MemberList']\n                        in_list = True\n                        if self.msg_handler:\n                            self.msg_handler.handle_group_member_change(g_id, m['MemberList'])\n                        break\n                if not in_list:\n                    # a new group\n                    self.GroupList.append(m)\n                    self.GroupMemeberList[g_id] = m['MemberList']\n                    if self.msg_handler:\n                        self.msg_handler.handle_group_list_change(m)\n                        self.msg_handler.handle_group_member_change(g_id, m['MemberList'])\n\n            elif m['UserName'][0] == '@':\n                # user\n                in_list = False\n                for u in self.MemberList:\n                    u_id = m['UserName']\n                    if u_id == u['UserName']:\n                        u = m\n                        in_list = True\n                        break\n                # if don't have then add it\n                if not in_list:\n                    self.MemberList.append(m)\n\n    def handle_msg(self, r):\n        \"\"\"\n        @brief      Recover from snapshot data.\n        @param      r  Dict: message json\n        \"\"\"\n        Log.debug('handle message')\n        if self.msg_handler:\n            self.msg_handler.handle_wxsync(r)\n\n        n = len(r['AddMsgList'])\n        if n == 0:\n            return\n\n        if self.log_mode:\n            echo(Constant.LOG_MSG_NEW_MSG % n)\n\n        for msg in r['AddMsgList']:\n\n            msgType = msg['MsgType']\n            msgId = msg['MsgId']\n            content = msg['Content'].replace('&lt;', '<').replace('&gt;', '>')\n            raw_msg = None\n\n            if msgType == self.wx_conf['MSGTYPE_TEXT']:\n                # 地理位置消息\n                if content.find('pictype=location') != -1:\n                    location = content.split('<br/>')[1][:-1]\n                    raw_msg = {\n                        'raw_msg': msg,\n                        'location': location,\n                        'log': Constant.LOG_MSG_LOCATION % location\n                    }\n                # 普通文本消息\n                else:\n                    text = content.split(':<br/>')[-1]\n                    raw_msg = {\n                        'raw_msg': msg,\n                        'text': text,\n                        'log': text.replace('<br/>', '\\n')\n                    }\n            elif msgType == self.wx_conf['MSGTYPE_IMAGE']:\n                data = self.webwxgetmsgimg(msgId)\n                fn = 'img_' + msgId + '.jpg'\n                dir = self.save_data_folders['webwxgetmsgimg']\n                path = save_file(fn, data, dir)\n                raw_msg = {'raw_msg': msg,\n                           'image': path,\n                           'log': Constant.LOG_MSG_PICTURE % path}\n            elif msgType == self.wx_conf['MSGTYPE_VOICE']:\n                data = self.webwxgetvoice(msgId)\n                fn = 'voice_' + msgId + '.mp3'\n                dir = self.save_data_folders['webwxgetvoice']\n                path = save_file(fn, data, dir)\n                raw_msg = {'raw_msg': msg,\n                           'voice': path,\n                           'log': Constant.LOG_MSG_VOICE % path}\n            elif msgType == self.wx_conf['MSGTYPE_SHARECARD']:\n                info = msg['RecommendInfo']\n                card = Constant.LOG_MSG_NAME_CARD % (\n                    info['NickName'],\n                    info['Alias'],\n                    info['Province'], info['City'],\n                    Constant.LOG_MSG_SEX_OPTION[info['Sex']]\n                )\n                namecard = '%s %s %s %s %s' % (\n                    info['NickName'], info['Alias'], info['Province'],\n                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']]\n                )\n                raw_msg = {\n                    'raw_msg': msg,\n                    'namecard': namecard,\n                    'log': card\n                }\n            elif msgType == self.wx_conf['MSGTYPE_EMOTICON']:\n                url = search_content('cdnurl', content)\n                raw_msg = {'raw_msg': msg,\n                           'emoticon': url,\n                           'log': Constant.LOG_MSG_EMOTION % url}\n            elif msgType == self.wx_conf['MSGTYPE_APP']:\n                card = ''\n                # 链接, 音乐, 微博\n                if msg['AppMsgType'] in [\n                    self.wx_conf['APPMSGTYPE_AUDIO'],\n                    self.wx_conf['APPMSGTYPE_URL'],\n                    self.wx_conf['APPMSGTYPE_OPEN']\n                ]:\n                    card = Constant.LOG_MSG_APP_LINK % (\n                        Constant.LOG_MSG_APP_LINK_TYPE[msg['AppMsgType']],\n                        msg['FileName'],\n                        search_content('des', content, 'xml'),\n                        msg['Url'],\n                        search_content('appname', content, 'xml')\n                    )\n                    raw_msg = {\n                        'raw_msg': msg,\n                        'link': msg['Url'],\n                        'log': card\n                    }\n                # 图片\n                elif msg['AppMsgType'] == self.wx_conf['APPMSGTYPE_IMG']:\n                    data = self.webwxgetmsgimg(msgId)\n                    fn = 'img_' + msgId + '.jpg'\n                    dir = self.save_data_folders['webwxgetmsgimg']\n                    path = save_file(fn, data, dir)\n                    card = Constant.LOG_MSG_APP_IMG % (\n                        path,\n                        search_content('appname', content, 'xml')\n                    )\n                    raw_msg = {\n                        'raw_msg': msg,\n                        'image': path,\n                        'log': card\n                    }\n                else:\n                    raw_msg = {\n                        'raw_msg': msg,\n                        'log': Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)\n                    }\n            elif msgType == self.wx_conf['MSGTYPE_STATUSNOTIFY']:\n                Log.info(Constant.LOG_MSG_NOTIFY_PHONE)\n            elif msgType == self.wx_conf['MSGTYPE_MICROVIDEO']:\n                data = self.webwxgetvideo(msgId)\n                fn = 'video_' + msgId + '.mp4'\n                dir = self.save_data_folders['webwxgetvideo']\n                path = save_file(fn, data, dir)\n                raw_msg = {'raw_msg': msg,\n                           'video': path,\n                           'log': Constant.LOG_MSG_VIDEO % path}\n            elif msgType == self.wx_conf['MSGTYPE_RECALLED']:\n                recall_id = search_content('msgid', content, 'xml')\n                text = Constant.LOG_MSG_RECALL\n                raw_msg = {\n                    'raw_msg': msg,\n                    'text': text,\n                    'recall_msg_id': recall_id,\n                    'log': text\n                }\n            elif msgType == self.wx_conf['MSGTYPE_SYS']:\n                raw_msg = {\n                    'raw_msg': msg,\n                    'sys_notif': content,\n                    'log': content\n                }\n            elif msgType == self.wx_conf['MSGTYPE_VERIFYMSG']:\n                name = search_content('fromnickname', content)\n                raw_msg = {\n                    'raw_msg': msg,\n                    'log': Constant.LOG_MSG_ADD_FRIEND % name\n                }\n            else:\n                raw_msg = {\n                    'raw_msg': msg,\n                    'log': Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)\n                }\n\n            isGroupMsg = '@@' in msg['FromUserName']+msg['ToUserName']\n            if self.msg_handler and raw_msg:\n                if isGroupMsg:\n                    # handle group messages\n                    g_msg = self.make_group_msg(raw_msg)\n                    self.msg_handler.handle_group_msg(g_msg)\n                else:\n                    # handle personal messages\n                    self.msg_handler.handle_user_msg(raw_msg)\n\n            if self.log_mode:\n                self.show_msg(raw_msg)\n\n    def make_group_msg(self, msg):\n        \"\"\"\n        @brief      Package the group message for storage.\n        @param      msg  Dict: raw msg\n        @return     raw_msg Dict: packged msg\n        \"\"\"\n        Log.debug('make group message')\n        raw_msg = {\n            'raw_msg': msg['raw_msg'],\n            'msg_id': msg['raw_msg']['MsgId'],\n            'group_owner_uin': '',\n            'group_name': '',\n            'group_count': '',\n            'from_user_name': msg['raw_msg']['FromUserName'],\n            'to_user_name': msg['raw_msg']['ToUserName'],\n            'user_attrstatus': '',\n            'user_display_name': '',\n            'user_nickname': '',\n            'msg_type': msg['raw_msg']['MsgType'],\n            'text': '',\n            'link': '',\n            'image': '',\n            'video': '',\n            'voice': '',\n            'emoticon': '',\n            'namecard': '',\n            'location': '',\n            'recall_msg_id': '',\n            'sys_notif': '',\n            'time': '',\n            'timestamp': '',\n            'log': '',\n        }\n        content = msg['raw_msg']['Content'].replace(\n            '&lt;', '<').replace('&gt;', '>')\n\n        group = None\n        src = None\n\n        if msg['raw_msg']['FromUserName'][:2] == '@@':\n            # 接收到来自群的消息\n            g_id = msg['raw_msg']['FromUserName']\n            group = self.get_group_by_id(g_id)\n\n            if re.search(\":<br/>\", content, re.IGNORECASE):\n                u_id = content.split(':<br/>')[0]\n                src = self.get_group_user_by_id(u_id, g_id)\n\n        elif msg['raw_msg']['ToUserName'][:2] == '@@':\n            # 自己发给群的消息\n            g_id = msg['raw_msg']['ToUserName']\n            u_id = msg['raw_msg']['FromUserName']\n            src = self.get_group_user_by_id(u_id, g_id)\n            group = self.get_group_by_id(g_id)\n\n        if src:\n            raw_msg['user_attrstatus'] = src['AttrStatus']\n            raw_msg['user_display_name'] = src['DisplayName']\n            raw_msg['user_nickname'] = src['NickName']\n        if group:\n            raw_msg['group_count'] = group['MemberCount']\n            raw_msg['group_owner_uin'] = group['OwnerUin']\n            raw_msg['group_name'] = group['ShowName']\n\n        raw_msg['timestamp'] = msg['raw_msg']['CreateTime']\n        t = time.localtime(float(raw_msg['timestamp']))\n        raw_msg['time'] = time.strftime(\"%Y-%m-%d %T\", t)\n\n        for key in [\n            'text', 'link', 'image', 'video', 'voice',\n            'emoticon', 'namecard', 'location', 'log',\n            'recall_msg_id', 'sys_notif'\n        ]:\n            if key in msg:\n                raw_msg[key] = msg[key]\n\n        return raw_msg\n\n    def show_msg(self, message):\n        \"\"\"\n        @brief      Log the message to stdout\n        @param      message  Dict\n        \"\"\"\n        msg = message\n        src = None\n        dst = None\n        group = None\n\n        if msg and msg['raw_msg']:\n\n            content = msg['raw_msg']['Content']\n            content = content.replace('&lt;', '<').replace('&gt;', '>')\n            msg_id = msg['raw_msg']['MsgId']\n\n            if msg['raw_msg']['FromUserName'][:2] == '@@':\n                # 接收到来自群的消息\n                g_id = msg['raw_msg']['FromUserName']\n                group = self.get_group_by_id(g_id)\n\n                if re.search(\":<br/>\", content, re.IGNORECASE):\n                    u_id = content.split(':<br/>')[0]\n                    src = self.get_group_user_by_id(u_id, g_id)\n                    dst = {'ShowName': 'GROUP'}\n                else:\n                    u_id = msg['raw_msg']['ToUserName']\n                    src = {'ShowName': 'SYSTEM'}\n                    dst = self.get_group_user_by_id(u_id, g_id)\n            elif msg['raw_msg']['ToUserName'][:2] == '@@':\n                # 自己发给群的消息\n                g_id = msg['raw_msg']['ToUserName']\n                u_id = msg['raw_msg']['FromUserName']\n                group = self.get_group_by_id(g_id)\n                src = self.get_group_user_by_id(u_id, g_id)\n                dst = {'ShowName': 'GROUP'}\n            else:\n                # 非群聊消息\n                src = self.get_user_by_id(msg['raw_msg']['FromUserName'])\n                dst = self.get_user_by_id(msg['raw_msg']['ToUserName'])\n\n            if group:\n                echo('%s |%s| %s -> %s: %s\\n' % (\n                    msg_id,\n                    trans_emoji(group['ShowName']),\n                    trans_emoji(src['ShowName']),\n                    dst['ShowName'],\n                    trans_emoji(msg['log'])\n                ))\n            else:\n                echo('%s %s -> %s: %s\\n' % (\n                    msg_id,\n                    trans_emoji(src['ShowName']),\n                    trans_emoji(dst['ShowName']),\n                    trans_emoji(msg['log'])\n                ))\n"
  },
  {
    "path": "wxbot_project_py2.7/wechat/wechat_apis.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom utils import *\nfrom config import Constant\nfrom config import Log\n#---------------------------------------------------\nimport sys\nimport os\nimport cookielib\nimport random\nimport requests\nimport time\nimport xml.dom.minidom\n# for media upload\nimport mimetypes\nfrom requests_toolbelt.multipart.encoder import MultipartEncoder\n#===================================================\n\n\nclass WXAPI(object):\n\n    def __init__(self, host):\n        self.wx_host = host\n        self.wx_filehost = ''\n        self.wx_conf = {}\n        # jsLogin时这个appid只能使用: wx782c26e4c19acffb\n        self.appid = Constant.API_APPID\n        self.uuid = ''\n        self.redirect_uri = ''\n        self.skey = ''\n        self.sid = ''\n        self.uin = ''\n        self.pass_ticket = ''\n        self.base_request = {}\n        self.synckey_dic = {}\n        self.synckey = ''\n        self.device_id = 'e' + repr(random.random())[2:17]\n        # device_id: 登录手机设备\n        # web wechat 的格式为: e123456789012345 (e+15位随机数)\n        # mobile wechat 的格式为: A1234567890abcde (A+15位随机数字或字母)\n        self.user_agent = Constant.API_USER_AGENT\n        self.cookie = None\n\n        self.conf_factory()\n\n        self.User = []  # 登陆账号信息\n        self.MemberList = []  # 好友+群聊+公众号+特殊账号\n        self.MemberCount = 0\n        self.ContactList = []  # 好友\n        self.GroupList = []  # 群\n        self.GroupMemeberList = {}  # 群聊成员字典\n                                    # \"group_id\": [\n                                    #       {member}, ...\n                                    # ]\n        self.PublicUsersList = []  # 公众号／服务号\n        self.SpecialUsersList = []  # 特殊账号\n\n        self.media_count = 0\n\n    def conf_factory(self):\n        e = self.wx_host  # wx.qq.com\n        t, o, n = \"login.weixin.qq.com\", \"file.wx.qq.com\", \"webpush.weixin.qq.com\"\n\n        if e.find(\"wx2.qq.com\") > -1:\n            t, o, n = \"login.wx2.qq.com\", \"file.wx2.qq.com\", \"webpush.wx2.qq.com\"\n        elif e.find(\"wx8.qq.com\") > -1:\n            t, o, n = \"login.wx8.qq.com\", \"file.wx8.qq.com\", \"webpush.wx8.qq.com\"\n        elif e.find(\"qq.com\") > -1:\n            t, o, n = \"login.wx.qq.com\", \"file.wx.qq.com\", \"webpush.wx.qq.com\"\n        elif e.find(\"web2.wechat.com\") > -1:\n            t, o, n = \"login.web2.wechat.com\", \"file.web2.wechat.com\", \"webpush.web2.wechat.com\"\n        elif e.find(\"wechat.com\") > -1:\n            t, o, n = \"login.web.wechat.com\", \"file.web.wechat.com\", \"webpush.web.wechat.com\"\n\n        self.wx_filehost = o\n        conf = {\n            'LANG': Constant.API_LANG,\n            'SpecialUsers': Constant.API_SPECIAL_USER,\n            'API_jsLogin': \"https://\" + t + \"/jslogin\",\n            'API_qrcode': \"https://login.weixin.qq.com/l/\",\n            'API_qrcode_img': \"https://login.weixin.qq.com/qrcode/\",\n            'API_login': \"https://\" + t + \"/cgi-bin/mmwebwx-bin/login\",\n            'API_synccheck': \"https://\" + n + \"/cgi-bin/mmwebwx-bin/synccheck\",\n            'API_webwxdownloadmedia': \"https://\" + o + \"/cgi-bin/mmwebwx-bin/webwxgetmedia\",\n            'API_webwxuploadmedia': \"https://\" + o + \"/cgi-bin/mmwebwx-bin/webwxuploadmedia\",\n            'API_webwxpreview': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxpreview\",\n            'API_webwxinit': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxinit\",\n            'API_webwxgetcontact': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetcontact\",\n            'API_webwxsync': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsync\",\n            'API_webwxbatchgetcontact': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxbatchgetcontact\",\n            'API_webwxgeticon': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgeticon\",\n            'API_webwxsendmsg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendmsg\",\n            'API_webwxsendmsgimg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendmsgimg\",\n            'API_webwxsendmsgvedio': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendvideomsg\",\n            'API_webwxsendemoticon': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendemoticon\",\n            'API_webwxsendappmsg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendappmsg\",\n            'API_webwxgetheadimg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetheadimg\",\n            'API_webwxgetmsgimg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetmsgimg\",\n            'API_webwxgetmedia': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetmedia\",\n            'API_webwxgetvideo': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetvideo\",\n            'API_webwxlogout': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxlogout\",\n            'API_webwxgetvoice': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxgetvoice\",\n            'API_webwxupdatechatroom': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxupdatechatroom\",\n            'API_webwxcreatechatroom': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxcreatechatroom\",\n            'API_webwxstatusnotify': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxstatusnotify\",\n            'API_webwxcheckurl': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxcheckurl\",\n            'API_webwxverifyuser': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxverifyuser\",\n            'API_webwxfeedback': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsendfeedback\",\n            'API_webwxreport': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxstatreport\",\n            'API_webwxsearch': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxsearchcontact\",\n            'API_webwxoplog': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxoplog\",\n            'API_checkupload': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxcheckupload\",\n            'API_webwxrevokemsg': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxrevokemsg\",\n            'API_webwxpushloginurl': \"https://\" + e + \"/cgi-bin/mmwebwx-bin/webwxpushloginurl\",\n            'CONTACTFLAG_CONTACT': 1,\n            'CONTACTFLAG_CHATCONTACT': 2,\n            'CONTACTFLAG_CHATROOMCONTACT': 4,\n            'CONTACTFLAG_BLACKLISTCONTACT': 8,\n            'CONTACTFLAG_DOMAINCONTACT': 16,\n            'CONTACTFLAG_HIDECONTACT': 32,\n            'CONTACTFLAG_FAVOURCONTACT': 64,\n            'CONTACTFLAG_3RDAPPCONTACT': 128,\n            'CONTACTFLAG_SNSBLACKLISTCONTACT': 256,\n            'CONTACTFLAG_NOTIFYCLOSECONTACT': 512,\n            'CONTACTFLAG_TOPCONTACT': 2048,\n            'MSGTYPE_TEXT': 1,\n            'MSGTYPE_IMAGE': 3,\n            'MSGTYPE_VOICE': 34,\n            'MSGTYPE_VIDEO': 43,\n            'MSGTYPE_MICROVIDEO': 62,\n            'MSGTYPE_EMOTICON': 47,\n            'MSGTYPE_APP': 49,\n            'MSGTYPE_VOIPMSG': 50,\n            'MSGTYPE_VOIPNOTIFY': 52,\n            'MSGTYPE_VOIPINVITE': 53,\n            'MSGTYPE_LOCATION': 48,\n            'MSGTYPE_STATUSNOTIFY': 51,\n            'MSGTYPE_SYSNOTICE': 9999,\n            'MSGTYPE_POSSIBLEFRIEND_MSG': 40,\n            'MSGTYPE_VERIFYMSG': 37,\n            'MSGTYPE_SHARECARD': 42,\n            'MSGTYPE_SYS': 10000,\n            'MSGTYPE_RECALLED': 10002,\n            'APPMSGTYPE_TEXT': 1,\n            'APPMSGTYPE_IMG': 2,\n            'APPMSGTYPE_AUDIO': 3,\n            'APPMSGTYPE_VIDEO': 4,\n            'APPMSGTYPE_URL': 5,\n            'APPMSGTYPE_ATTACH': 6,\n            'APPMSGTYPE_OPEN': 7,\n            'APPMSGTYPE_EMOJI': 8,\n            'APPMSGTYPE_VOICE_REMIND': 9,\n            'APPMSGTYPE_SCAN_GOOD': 10,\n            'APPMSGTYPE_GOOD': 13,\n            'APPMSGTYPE_EMOTION': 15,\n            'APPMSGTYPE_CARD_TICKET': 16,\n            'APPMSGTYPE_REALTIME_SHARE_LOCATION': 17,\n            'APPMSGTYPE_TRANSFERS': 2e3,\n            'APPMSGTYPE_RED_ENVELOPES': 2001,\n            'APPMSGTYPE_READER_TYPE': 100001,\n            'UPLOAD_MEDIA_TYPE_IMAGE': 1,\n            'UPLOAD_MEDIA_TYPE_VIDEO': 2,\n            'UPLOAD_MEDIA_TYPE_AUDIO': 3,\n            'UPLOAD_MEDIA_TYPE_ATTACHMENT': 4,\n        }\n        self.wx_conf = conf\n\n    def getuuid(self):\n        \"\"\"\n        @brief      Gets the uuid just used for login.\n        @return     Bool: whether operation succeed.\n        \"\"\"\n        url = self.wx_conf['API_jsLogin']\n        params = {\n            'appid': self.appid,\n            'fun': 'new',\n            'lang': self.wx_conf['LANG'],\n            '_': int(time.time()),\n        }\n        data = post(url, params, False)\n        regx = r'window.QRLogin.code = (\\d+); window.QRLogin.uuid = \"(\\S+?)\"'\n        pm = re.search(regx, data)\n        if pm:\n            code = pm.group(1)\n            self.uuid = pm.group(2)\n            return code == '200'\n        return False\n\n    def genqrcode(self):\n        \"\"\"\n        @brief      outprint the qrcode to stdout on macos/linux\n                    or open image on windows\n        \"\"\"\n        if sys.platform.startswith('win'):\n            url = self.wx_conf['API_qrcode_img'] + self.uuid\n            params = {\n                't': 'webwx',\n                '_': int(time.time())\n            }\n            data = post(url, params, False)\n            if data == '':\n                return\n            qrcode_path = save_file('qrcode.jpg', data, './')\n            os.startfile(qrcode_path)\n        else:\n            str2qr_terminal(self.wx_conf['API_qrcode'] + self.uuid)\n\n    def waitforlogin(self, tip=1):\n        \"\"\"\n        @brief      wait for scaning qrcode to login\n        @param      tip   1: wait for scan qrcode\n                          0: wait for confirm\n        @return     Bool: whether operation succeed\n        \"\"\"\n        time.sleep(tip)\n        url = self.wx_conf['API_login'] + '?tip=%s&uuid=%s&_=%s' % (\n            tip, self.uuid, int(time.time()))\n        data = get(url)\n        pm = re.search(r'window.code=(\\d+);', data)\n        code = pm.group(1)\n\n        if code == '201':\n            return True\n        elif code == '200':\n            pm = re.search(r'window.redirect_uri=\"(\\S+?)\";', data)\n            r_uri = pm.group(1) + '&fun=new'\n            self.redirect_uri = r_uri\n            self.wx_host = r_uri.split('://')[1].split('/')[0]\n            self.conf_factory()\n            return True\n        elif code == '408':\n            echo(Constant.LOG_MSG_WAIT_LOGIN_ERR1)\n        else:\n            echo(Constant.LOG_MSG_WAIT_LOGIN_ERR2)\n        return False\n\n    def login(self):\n        \"\"\"\n        @brief      login\n                    redirect_uri 有效时间是从扫码成功后算起，\n                    大概是 300 秒，在此期间可以重新登录，但获取的联系人和群ID会改变\n        @return     Bool: whether operation succeed\n        \"\"\"\n        data = get(self.redirect_uri)\n        doc = xml.dom.minidom.parseString(data)\n        root = doc.documentElement\n\n        for node in root.childNodes:\n            if node.nodeName == 'ret':\n                if node.childNodes[0].data != \"0\":\n                    return False\n            elif node.nodeName == 'skey':\n                self.skey = node.childNodes[0].data\n            elif node.nodeName == 'wxsid':\n                self.sid = node.childNodes[0].data\n            elif node.nodeName == 'wxuin':\n                self.uin = node.childNodes[0].data\n            elif node.nodeName == 'pass_ticket':\n                self.pass_ticket = node.childNodes[0].data\n\n        if '' in (self.skey, self.sid, self.uin, self.pass_ticket):\n            return False\n\n        self.base_request = {\n            'Uin': int(self.uin),\n            'Sid': self.sid,\n            'Skey': self.skey,\n            'DeviceID': self.device_id,\n        }\n\n        return True\n\n    def webwxinit(self):\n        \"\"\"\n        @brief      wechat initial\n                    掉线后 300 秒可以重新使用此 api 登录\n                    获取的联系人和群ID保持不变\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxinit'] + \\\n            '?pass_ticket=%s&skey=%s&r=%s' % (\n            self.pass_ticket, self.skey, int(time.time())\n        )\n        params = {\n            'BaseRequest': self.base_request\n        }\n        dic = post(url, params)\n        self.User = dic['User']\n        self.make_synckey(dic)\n\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxstatusnotify(self):\n        \"\"\"\n        @brief      notify the mobile phone, this not necessary\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxstatusnotify'] + \\\n            '?lang=%s&pass_ticket=%s' % (\n                self.wx_conf['LANG'], self.pass_ticket\n            )\n        params = {\n            'BaseRequest': self.base_request,\n            \"Code\": 3,\n            \"FromUserName\": self.User['UserName'],\n            \"ToUserName\": self.User['UserName'],\n            \"ClientMsgId\": int(time.time())\n        }\n        dic = post(url, params)\n\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxgetcontact(self):\n        \"\"\"\n        @brief      get all contacts: people, group, public user, special user\n        @return     Bool: whether operation succeed\n        \"\"\"\n        SpecialUsers = self.wx_conf['SpecialUsers']\n        url = self.wx_conf['API_webwxgetcontact'] + \\\n            '?pass_ticket=%s&skey=%s&r=%s' % (\n                self.pass_ticket, self.skey, int(time.time())\n            )\n        dic = post(url, {})\n\n        self.MemberCount = dic['MemberCount']\n        self.MemberList = dic['MemberList']\n        ContactList = self.MemberList[:]\n        GroupList = self.GroupList[:]\n        PublicUsersList = self.PublicUsersList[:]\n        SpecialUsersList = self.SpecialUsersList[:]\n\n        for i in xrange(len(ContactList) - 1, -1, -1):\n            Contact = ContactList[i]\n            if Contact['VerifyFlag'] & 8 != 0:  # 公众号/服务号\n                ContactList.remove(Contact)\n                self.PublicUsersList.append(Contact)\n            elif Contact['UserName'] in SpecialUsers:  # 特殊账号\n                ContactList.remove(Contact)\n                self.SpecialUsersList.append(Contact)\n            elif Contact['UserName'].find('@@') != -1:  # 群聊\n                ContactList.remove(Contact)\n                self.GroupList.append(Contact)\n            elif Contact['UserName'] == self.User['UserName']:  # 自己\n                ContactList.remove(Contact)\n        self.ContactList = ContactList\n\n        return True\n\n    def webwxbatchgetcontact(self, gid_list):\n        \"\"\"\n        @brief      get group contacts\n        @param      gid_list, The list of group id\n        @return     List, list of group contacts\n        \"\"\"\n        url = self.wx_conf['API_webwxbatchgetcontact'] + \\\n            '?type=ex&r=%s&pass_ticket=%s' % (\n                int(time.time()), self.pass_ticket\n            )\n        params = {\n            'BaseRequest': self.base_request,\n            \"Count\": len(gid_list),\n            \"List\": [{\"UserName\": gid, \"EncryChatRoomId\": \"\"} for gid in gid_list]\n        }\n        dic = post(url, params)\n        return dic['ContactList']\n\n    def synccheck(self):\n        \"\"\"\n        @brief      check whether there's a message\n        @return     [retcode, selector]\n                    retcode: 0    successful\n                             1100 logout\n                             1101 login otherwhere\n                    selector: 0 nothing\n                              2 message\n                              6 unkonwn\n                              7 webwxsync\n        \"\"\"\n        params = {\n            'r': int(time.time()),\n            'sid': self.sid,\n            'uin': self.uin,\n            'skey': self.skey,\n            'deviceid': self.device_id,\n            'synckey': self.synckey,\n            '_': int(time.time()),\n        }\n        url = self.wx_conf['API_synccheck'] + '?' + urllib.urlencode(params)\n        data = get(url)\n        reg = r'window.synccheck={retcode:\"(\\d+)\",selector:\"(\\d+)\"}'\n        pm = re.search(reg, data)\n        retcode = pm.group(1)\n        selector = pm.group(2)\n        return [retcode, selector]\n\n    def webwxsync(self):\n        \"\"\"\n        @brief      sync the messages\n        @return     Dict{}\n        \"\"\"\n        url = self.wx_conf['API_webwxsync'] + \\\n            '?sid=%s&skey=%s&pass_ticket=%s' % (\n                self.sid, self.skey, self.pass_ticket\n            )\n        params = {\n            'BaseRequest': self.base_request,\n            'SyncKey': self.synckey_dic,\n            'rr': ~int(time.time())\n        }\n        dic = post(url, params)\n\n        if dic['BaseResponse']['Ret'] == 0:\n            self.make_synckey(dic)\n        return dic\n\n    def webwxgetmsgimg(self, msgid):\n        \"\"\"\n        @brief      get image in message\n        @param      msgid  The id of message\n        @return     binary data of image\n        \"\"\"\n        url = self.wx_conf['API_webwxgetmsgimg'] + \\\n            '?MsgID=%s&skey=%s' % (msgid, self.skey)\n        data = get(url, api='webwxgetmsgimg')\n        return data\n\n    def webwxgetvoice(self, msgid):\n        \"\"\"\n        @brief      get voice in message\n        @param      msgid  The id of message\n        @return     binary data of voice\n        \"\"\"\n        url = self.wx_conf['API_webwxgetvoice'] + \\\n            '?msgid=%s&skey=%s' % (msgid, self.skey)\n        data = get(url, api='webwxgetvoice')\n        return data\n\n    def webwxgetvideo(self, msgid):\n        \"\"\"\n        @brief      get video in message\n        @param      msgid  The id of message\n        @return     binary data of video\n        \"\"\"\n        url = self.wx_conf['API_webwxgetvideo'] + \\\n            '?msgid=%s&skey=%s' % (msgid, self.skey)\n        data = get(url, api='webwxgetvideo')\n        return data\n\n    def webwxgeticon(self, id):\n        \"\"\"\n        @brief      get user small icon\n        @param      id  String\n        @return     binary data of icon\n        \"\"\"\n        url = self.wx_conf['API_webwxgeticon'] + \\\n            '?username=%s&skey=%s' % (id, self.skey)\n        data = get(url, api='webwxgeticon')\n        return data\n\n    def webwxgetheadimg(self, id):\n        \"\"\"\n        @brief      get user head image\n        @param      id  String\n        @return     binary data of image\n        \"\"\"\n        url = self.wx_conf['API_webwxgetheadimg'] + \\\n            '?username=%s&skey=%s' % (id, self.skey)\n        data = get(url, api='webwxgetheadimg')\n        return data\n\n    def webwxsendmsg(self, word, to='filehelper'):\n        \"\"\"\n        @brief      send text message\n        @param      word  String\n        @param      to    User id\n        @return     dic   Dict\n        \"\"\"\n        url = self.wx_conf['API_webwxsendmsg'] + \\\n            '?pass_ticket=%s' % (self.pass_ticket)\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        params = {\n            'BaseRequest': self.base_request,\n            'Msg': {\n                \"Type\": 1,\n                \"Content\": trans_coding(word),\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": to,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        dic = post(url, params)\n        return dic\n\n    def webwxuploadmedia(self, file_path):\n        \"\"\"\n        @brief      upload image\n        @param      file_path  String\n        @return     Dict: json\n        \"\"\"\n        url = self.wx_conf['API_webwxuploadmedia'] + '?f=json'\n        # 计数器\n        self.media_count = self.media_count + 1\n        fn = file_path\n        # mime_type: \n        #   'application/pdf'\n        #   'image/jpeg'\n        #   'image/png'\n        #   ...\n        mime_type = mimetypes.guess_type(fn, strict=False)[0]\n        if not mime_type:\n            mime_type = 'text/plain'\n        # 文档格式\n        # 微信服务器目前应该支持3种类型:\n        #   pic     直接显示，包含图片，表情\n        #   video   不清楚\n        #   doc     显示为文件，包含PDF等\n        media_type = 'pic' if mime_type.split('/')[0] == 'image' else 'doc'\n        time_format = \"%a %b %d %Y %T GMT%z (%Z)\"\n        last_modifie_date = time.strftime(time_format, time.localtime())\n        file_size = os.path.getsize(fn)\n        pass_ticket = self.pass_ticket\n        client_media_id = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n\n        webwx_data_ticket = ''\n        for item in self.cookie:\n            if item.name == 'webwx_data_ticket':\n                webwx_data_ticket = item.value\n                break\n        if (webwx_data_ticket == ''):\n            Log.error(\"No Cookie\\n\")\n            return None\n\n        uploadmediarequest = json.dumps({\n            \"BaseRequest\": self.base_request,\n            \"ClientMediaId\": client_media_id,\n            \"TotalLen\": file_size,\n            \"StartPos\": 0,\n            \"DataLen\": file_size,\n            \"MediaType\": 4\n        }, ensure_ascii=False).encode('utf8')\n\n        multipart_encoder = MultipartEncoder(\n            fields={\n                'id': 'WU_FILE_' + str(self.media_count),\n                'name': fn,\n                'type': mime_type,\n                'lastModifieDate': last_modifie_date,\n                'size': str(file_size),\n                'mediatype': media_type,\n                'uploadmediarequest': uploadmediarequest,\n                'webwx_data_ticket': webwx_data_ticket,\n                'pass_ticket': pass_ticket,\n                'filename': (\n                    fn,\n                    open(fn, 'rb'),\n                    mime_type.split('/')[1]\n                )\n            },\n            boundary=(\n                '-----------------------------'\n                '1575017231431605357584454111'\n            )\n        )\n\n        headers = {\n            'Host': self.wx_filehost,\n            'User-Agent': self.user_agent,\n            'Accept': (\n                'text/html,application/xhtml+xml,'\n                'application/xml;q=0.9,*/*;q=0.8'\n            ),\n            'Accept-Language': 'en-US,en;q=0.5',\n            'Accept-Encoding': 'gzip, deflate',\n            'Referer': 'https://' + self.wx_host,\n            'Content-Type': multipart_encoder.content_type,\n            'Origin': 'https://' + self.wx_host,\n            'Connection': 'keep-alive',\n            'Pragma': 'no-cache',\n            'Cache-Control': 'no-cache',\n        }\n\n        r = requests.post(url, data=multipart_encoder, headers=headers)\n        dic = json.loads(r.text)  #修复无法发送Media消息BUG\n        if dic['BaseResponse']['Ret'] == 0:\n            return dic\n        return None\n\n    def webwxsendmsgimg(self, user_id, media_id):\n        \"\"\"\n        @brief      send image\n        @param      user_id  String\n        @param      media_id  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxsendmsgimg'] + \\\n            '?fun=async&f=json&pass_ticket=%s' % self.pass_ticket\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        data_json = {\n            \"BaseRequest\": self.base_request,\n            \"Msg\": {\n                \"Type\": 3,\n                \"MediaId\": media_id,\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": user_id,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        r = post(url, data_json)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxsendemoticon(self, user_id, media_id):\n        \"\"\"\n        @brief      send image\n        @param      user_id  String\n        @param      media_id  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxsendemoticon'] + \\\n            '?fun=sys&f=json&pass_ticket=%s' % self.pass_ticket\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        data_json = {\n            \"BaseRequest\": self.base_request,\n            \"Msg\": {\n                \"Type\": 47,\n                \"EmojiFlag\": 2,\n                \"MediaId\": media_id,\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": user_id,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            }\n        }\n        r = post(url, data_json)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxsendappmsg(self, user_id, data):\n        \"\"\"\n        @brief      send app msg\n        @param      user_id  String\n        @param      data     Dict\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxsendappmsg'] + \\\n            '?fun=sys&f=json&pass_ticket=%s' % self.pass_ticket\n        clientMsgId = str(int(time.time() * 1000)) + \\\n            str(random.random())[:5].replace('.', '')\n        content = ''.join([\n            \"<appmsg appid='%s' sdkver=''>\" % data['appid'], # 可使用其它AppID\n                \"<title>%s</title>\" % data['title'],\n                \"<des></des>\",\n                \"<action></action>\",\n                \"<type>%d</type>\" % data['type'],\n                \"<content></content>\",\n                \"<url></url>\",\n                \"<lowurl></lowurl>\",\n                \"<appattach>\",\n                    \"<totallen>%d</totallen>\" % data['totallen'],\n                    \"<attachid>%s</attachid>\" % data['attachid'],\n                    \"<fileext>%s</fileext>\" % data['fileext'],\n                \"</appattach>\",\n                \"<extinfo></extinfo>\",\n            \"</appmsg>\",\n        ])\n        data_json = {\n            \"BaseRequest\": self.base_request,\n            \"Msg\": {\n                \"Type\": data['type'],\n                \"Content\": content,\n                \"FromUserName\": self.User['UserName'],\n                \"ToUserName\": user_id,\n                \"LocalID\": clientMsgId,\n                \"ClientMsgId\": clientMsgId\n            },\n            \"Scene\": 0\n        }\n        r = post(url, data_json)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxcreatechatroom(self, uid_arr):\n        \"\"\"\n        @brief      create a chat group\n        @param      uid_arr  [String]\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxcreatechatroom'] + '?r=%s' % int(time.time())\n        params = {\n            'BaseRequest': self.base_request,\n            'Topic': '',\n            'MemberCount': len(uid_arr),\n            'MemberList': [{'UserName': uid} for uid in uid_arr],\n        }\n        dic = post(url, params)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxupdatechatroom(self, add_arr, del_arr, invite_arr):\n        \"\"\"\n        @brief      add/delete/invite member in chat group\n        @param      add_arr     [uid: String]\n        @param      del_arr     [uid: String]\n        @param      invite_arr  [uid: String]\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxupdatechatroom'] + '?r=%s' % int(time.time())\n        params = {\n            'BaseRequest': self.base_request,\n            'ChatRoomName': '',\n            'NewTopic': '',\n            'AddMemberList': add_arr,\n            'DelMemberList': del_arr,\n            'InviteMemberList': invite_arr,\n        }\n        dic = post(url, params)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxrevokemsg(self, msgid, user_id, client_msgid):\n        \"\"\"\n        @brief      revoke a message\n        @param      msgid           String\n        @param      user_id         String\n        @param      client_msgid    String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        url = self.wx_conf['API_webwxrevokemsg'] + '?r=%s' % int(time.time())\n        params = {\n            'BaseRequest': self.base_request,\n            'SvrMsgId': msgid,\n            'ToUserName': user_id,\n            'ClientMsgId': client_msgid\n        }\n        dic = post(url, params)\n        return dic['BaseResponse']['Ret'] == 0\n\n    def webwxpushloginurl(self, uin):\n        \"\"\"\n        @brief      push a login confirm alert to mobile device\n        @param      uin   String\n        @return     dic   Dict\n        \"\"\"\n        url = self.wx_conf['API_webwxpushloginurl'] + '?uin=%s' % uin\n        dic = eval(get(url))\n        return dic\n\n    def association_login(self):\n        \"\"\"\n        @brief      login without scan qrcode\n        @return     Bool: whether operation succeed\n        \"\"\"\n        if self.uin != '':\n            dic = self.webwxpushloginurl(self.uin)\n            if dic['ret'] == '0':\n                self.uuid = dic['uuid']\n                return True\n        return False\n\n    def send_text(self, user_id, text):\n        \"\"\"\n        @brief      send text\n        @param      user_id  String\n        @param      text  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        try:\n            dic = self.webwxsendmsg(text, user_id)\n            return dic['BaseResponse']['Ret'] == 0\n        except:\n            return False\n\n    def send_img(self, user_id, file_path):\n        \"\"\"\n        @brief      send image\n        @param      user_id  String\n        @param      file_path  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        response = self.webwxuploadmedia(file_path)\n        media_id = \"\"\n        if response is not None:\n            media_id = response['MediaId']\n        return self.webwxsendmsgimg(user_id, media_id)\n\n    def send_emot(self, user_id, file_path):\n        \"\"\"\n        @brief      send emotion\n        @param      user_id  String\n        @param      file_path  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        response = self.webwxuploadmedia(file_path)\n        media_id = \"\"\n        if response is not None:\n            media_id = response['MediaId']\n        return self.webwxsendemoticon(user_id, media_id)\n\n    def send_file(self, user_id, file_path):\n        \"\"\"\n        @brief      send file\n        @param      user_id  String\n        @param      file_path  String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        title = file_path.split('/')[-1]\n        data = {\n            'appid': Constant.API_WXAPPID,\n            'title': title,\n            'totallen': '',\n            'attachid': '',\n            'type': self.wx_conf['APPMSGTYPE_ATTACH'],\n            'fileext': title.split('.')[-1],\n        }\n\n        response = self.webwxuploadmedia(file_path)\n        if response is not None:\n            data['totallen'] = response['StartPos']\n            data['attachid'] = response['MediaId']\n        else:\n            Log.error('File upload error')\n        \n        return self.webwxsendappmsg(user_id, data)\n\n    def make_synckey(self, dic):\n        self.synckey_dic = dic['SyncKey']\n\n        def foo(x):\n            return str(x['Key']) + '_' + str(x['Val'])\n\n        # synckey for synccheck\n        self.synckey = '|'.join(\n            [foo(keyVal) for keyVal in self.synckey_dic['List']])\n\n    def revoke_msg(self, msgid, user_id, client_msgid):\n        \"\"\"\n        @brief      revoke a message\n        @param      msgid           String\n        @param      user_id         String\n        @param      client_msgid    String\n        @return     Bool: whether operation succeed\n        \"\"\"\n        return self.webwxrevokemsg(msgid, user_id, client_msgid)\n\n    # -----------------------------------------------------\n    # The following is getting user & group infomation apis\n    def get_user_by_id(self, user_id):\n        \"\"\"\n        @brief      get all type of name by user id\n        @param      user_id  The id of user\n        @return     Dict: {\n                        'UserName'      # 微信动态ID\n                        'RemarkName'    # 备注\n                        'NickName'      # 微信昵称\n                        'ShowName'      # Log显示用的\n                    }\n        \"\"\"\n        UnknownPeople = Constant.LOG_MSG_UNKNOWN_NAME + user_id\n        name = {\n            'UserName': user_id,\n            'RemarkName': '',\n            'NickName': '',\n            'ShowName': '',\n        }\n        name['ShowName'] = UnknownPeople\n\n        # yourself\n        if user_id == self.User['UserName']:\n            name['RemarkName'] = self.User['RemarkName']\n            name['NickName'] = self.User['NickName']\n            name['ShowName'] = name['NickName']\n        else:\n            # 联系人\n            for member in self.MemberList:\n                if member['UserName'] == user_id:\n                    r, n = member['RemarkName'], member['NickName']\n                    name['RemarkName'] = r\n                    name['NickName'] = n\n                    name['ShowName'] = r if r else n\n                    break\n            # 特殊帐号\n            for member in self.SpecialUsersList:\n                if member['UserName'] == user_id:\n                    name['RemarkName'] = user_id\n                    name['NickName'] = user_id\n                    name['ShowName'] = user_id\n                    break\n\n        return name\n\n    def get_group_user_by_id(self, user_id, group_id):\n        \"\"\"\n        @brief      get group user by user id\n        @param      user_id  The id of user\n        @param      group_id  The id of group\n        @return     Dict: {\n                        'UserName'      # 微信动态ID\n                        'NickName'      # 微信昵称\n                        'DisplayName'   # 群聊显示名称\n                        'ShowName'      # Log显示用的\n                        'AttrStatus'    # 群用户id\n                    }\n        \"\"\"\n        UnknownPeople = Constant.LOG_MSG_UNKNOWN_NAME + user_id\n        name = {\n            'UserName': user_id,\n            'NickName': '',\n            'DisplayName': '',\n            'ShowName': '',\n            'AttrStatus': '',\n        }\n        name['ShowName'] = UnknownPeople\n\n        # 群友\n        if group_id in self.GroupMemeberList:\n            for member in self.GroupMemeberList[group_id]:\n                if member['UserName'] == user_id:\n                    n, d = member['NickName'], member['DisplayName']\n                    name['NickName'] = n\n                    name['DisplayName'] = d\n                    name['AttrStatus'] = member['AttrStatus']\n                    name['ShowName'] = d if d else n\n                    break\n\n        return name\n\n    def get_group_by_id(self, group_id):\n        \"\"\"\n        @brief      get basic info by group id\n        @param      group_id  The id of group\n        @return     Dict: {\n                        'UserName'      # 微信动态ID\n                        'NickName'      # 微信昵称\n                        'DisplayName'   # 群聊显示名称\n                        'ShowName'      # Log显示用的\n                        'OwnerUin'      # 群主ID\n                        'MemberCount'   # 群人数\n                    }\n        \"\"\"\n        UnknownGroup = Constant.LOG_MSG_UNKNOWN_GROUP_NAME + group_id\n        group = {\n            'UserName': group_id,\n            'NickName': '',\n            'DisplayName': '',\n            'ShowName': '',\n            'OwnerUin': '',\n            'MemberCount': '',\n        }\n\n        for member in self.GroupList:\n            if member['UserName'] == group_id:\n                group['NickName'] = member['NickName']\n                group['DisplayName'] = member.get('DisplayName', '')\n                group['ShowName'] = member.get('NickName', UnknownGroup)\n                group['OwnerUin'] = member.get('OwnerUin', '')\n                group['MemberCount'] = member['MemberCount']\n                break\n\n        return group\n\n    def get_user_id(self, name):\n        \"\"\"\n        @brief      Gets the user id.\n        @param      name  The user nickname or remarkname\n        @return     The user id.\n        \"\"\"\n        for member in self.MemberList:\n            if name == member['RemarkName'] or name == member['NickName']:\n                return member['UserName']\n        return None\n"
  },
  {
    "path": "wxbot_project_py2.7/weixin_bot.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom wechat import WeChat\nfrom wechat.utils import *\nfrom wx_handler import WeChatMsgProcessor\nfrom wx_handler import Bot\nfrom wx_handler import SGMail\nfrom db import SqliteDB\nfrom db import MysqlDB\nfrom config import ConfigManager\nfrom config import Constant\nfrom config import Log\n#---------------------------------------------------\nfrom flask import Flask, render_template, send_file, jsonify, request\nimport threading\nimport traceback\nimport os\nimport logging\nimport time\n#===================================================\n\n\ncm = ConfigManager()\ndb = SqliteDB(cm.getpath('database'))\n# db = MysqlDB(cm.mysql())\nwechat_msg_processor = WeChatMsgProcessor(db)\nwechat = WeChat(cm.get('wechat', 'host'))\nwechat.db = db\nwechat.bot = Bot()\nwechat.msg_handler = wechat_msg_processor\nwechat_msg_processor.wechat = wechat\n\nPORT = int(cm.get('setting', 'server_port'))\napp = Flask(__name__, template_folder='flask_templates')\napp.config['UPLOAD_FOLDER'] = cm.getpath('uploaddir')\n\nlogger = logging.getLogger('werkzeug')\nlog_format_str = Constant.SERVER_LOG_FORMAT\nformatter = logging.Formatter(log_format_str)\nflask_log_handler = logging.FileHandler(cm.getpath('server_log_file'))\nflask_log_handler.setLevel(logging.INFO)\nflask_log_handler.setFormatter(formatter)\nlogger.addHandler(flask_log_handler)\napp.logger.addHandler(flask_log_handler)\n\n# sendgrid mail\nsg_apikey = cm.get('sendgrid', 'api_key')\nfrom_email = cm.get('sendgrid', 'from_email')\nto_email = cm.get('sendgrid', 'to_email')\nsg = SGMail(sg_apikey, from_email, to_email)\n\n@app.route('/')\ndef index():\n    return render_template(Constant.SERVER_PAGE_INDEX)\n\n\n@app.route('/qrcode')\ndef qrcode():\n    qdir = cm.getpath('qrcodedir')\n    if not os.path.exists(qdir):\n        os.makedirs(qdir)\n    image_path = '%s/%s_%d.png' % (qdir, wechat.uuid, int(time.time()*100))\n    s = wechat.wx_conf['API_qrcode'] + wechat.uuid\n    str2qr_image(s, image_path)\n    return send_file(image_path, mimetype='image/png')\n\n\n@app.route(\"/group_list\")\ndef group_list():\n    \"\"\"\n    @brief      list groups\n    \"\"\"\n    result = wechat.db.select(Constant.TABLE_GROUP_LIST())\n    return jsonify({'count': len(result), 'group': result})\n\n\n@app.route('/group_member_list/<g_id>')\ndef group_member_list(g_id):\n    \"\"\"\n    @brief      list group member\n    @param      g_id String\n    \"\"\"\n    result = wechat.db.select(Constant.TABLE_GROUP_USER_LIST(), 'RoomID', g_id)\n    return jsonify({'count': len(result), 'member': result})\n\n\n@app.route('/group_chat_log/<g_name>')\ndef group_chat_log(g_name):\n    \"\"\"\n    @brief      list group chat log\n    @param      g_name String\n    \"\"\"\n    result = wechat.db.select(Constant.TABLE_GROUP_MSG_LOG, 'RoomName', g_name)\n    return jsonify({'count': len(result), 'chats': result})\n\n\n@app.route('/upload', methods=['GET', 'POST'])\ndef upload_file():\n    if request.method == 'POST':\n        def allowed_file(filename):\n            return '.' in filename and \\\n                filename.rsplit('.', 1)[1] in Constant.SERVER_UPLOAD_ALLOWED_EXTENSIONS\n\n        j = {'ret': 1, 'msg': ''}\n\n        # check if the post request has the file part\n        if 'file' not in request.files:\n            j['msg'] = 'No file part'\n            return jsonify(j)\n\n        # if user does not select file, browser also\n        # submit a empty part without filename\n        file = request.files['file']\n        if file.filename == '':\n            j['msg'] = 'No selected file'\n        elif file and allowed_file(file.filename):\n            filename = generate_file_name(file.filename)\n            file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)\n            file.save(file_path)\n            j['ret'] = 0\n            j['msg'] = filename\n        else:\n            j['msg'] = 'File type not support'\n        return jsonify(j)\n    else:\n        return render_template(Constant.SERVER_PAGE_UPLOAD)\n\n\n@app.route('/send_msg/<to>/<msg>')\ndef send_msg(to, msg):\n    \"\"\"\n    @brief      send message to user or gourp\n    @param      to: String, user id or group id\n    @param      msg: String, words\n    \"\"\"\n    return jsonify({'ret': 0 if wechat.send_text(to, msg) else 1})\n\n\n@app.route('/send_img/<to>/<img>')\ndef send_img(to, img):\n    \"\"\"\n    @brief      send image to user or gourp\n    @param      to: String, user id or group id\n    @param      img: String, image file name\n    \"\"\"\n    img_path = os.path.join(app.config['UPLOAD_FOLDER'], img)\n    return jsonify({'ret': 0 if wechat.send_img(to, img_path) else 1})\n\n\n@app.route('/send_emot/<to>/<emot>')\ndef send_emot(to, emot):\n    \"\"\"\n    @brief      send emotion to user or gourp\n    @param      to: String, user id or group id\n    @param      emot: String, emotion file name\n    \"\"\"\n    emot_path = os.path.join(app.config['UPLOAD_FOLDER'], emot)\n    return jsonify({'ret': 0 if wechat.send_emot(to, emot_path) else 1})\n\n\n@app.route('/send_file/<to>/<file>')\ndef send_file(to, file):\n    \"\"\"\n    @brief      send file to user or gourp\n    @param      to: String, user id or group id\n    @param      file: String, file name\n    \"\"\"\n    file_path = os.path.join(app.config['UPLOAD_FOLDER'], file)\n    return jsonify({'ret': 0 if wechat.send_file(to, file_path) else 1})\n\n\ndef mass_send(method, data, func):\n    j = {'ret': -1, 'unsend_list':[]}\n    if method == 'POST' and data:\n        to_list = data['to_list']\n        msg = data['msg']\n        media_type = data.get('media_type', '')\n\n        if media_type in ['img', 'emot']:\n            file_path = os.path.join(app.config['UPLOAD_FOLDER'], msg)\n            response = wechat.webwxuploadmedia(file_path)\n            if response is not None:\n                msg = response['MediaId']\n        elif media_type == 'file':\n            file_path = os.path.join(app.config['UPLOAD_FOLDER'], msg)\n            data = {\n                'appid': Constant.API_WXAPPID,\n                'title': msg,\n                'totallen': '',\n                'attachid': '',\n                'type': wechat.wx_conf['APPMSGTYPE_ATTACH'],\n                'fileext': msg.split('.')[-1],\n            }\n            response = wechat.webwxuploadmedia(file_path)\n            if response is not None:\n                data['totallen'] = response['StartPos']\n                data['attachid'] = response['MediaId']\n            else:\n                Log.error('File upload error')\n            msg = data\n\n        for groups in split_array(to_list, 20):\n            for g in groups:\n                r = func(g, msg)\n                if not r:\n                    j['unsend_list'].append(g)\n            time.sleep(1)\n\n        j['ret'] = len(j['unsend_list'])\n\n    return j\n\n\n@app.route('/mass_send_msg/', methods=[\"GET\", \"POST\"])\ndef mass_send_msg():\n    \"\"\"\n    @brief      send text to mass users or gourps\n    \"\"\"\n    j = mass_send(request.method, request.json, wechat.send_text)\n    return jsonify(j)\n\n\n@app.route('/mass_send_img', methods=[\"GET\", \"POST\"])\ndef mass_send_img():\n    \"\"\"\n    @brief      send iamge to mass users or gourps\n    \"\"\"\n    j = mass_send(request.method, request.json, wechat.webwxsendmsgimg)\n    return jsonify(j)\n\n\n@app.route('/mass_send_emot', methods=[\"GET\", \"POST\"])\ndef mass_send_emot():\n    \"\"\"\n    @brief      send emoticon to mass users or gourps\n    \"\"\"\n    j = mass_send(request.method, request.json, wechat.webwxsendemoticon)\n    return jsonify(j)\n\n\n@app.route('/mass_send_file', methods=[\"GET\", \"POST\"])\ndef mass_send_file():\n    \"\"\"\n    @brief      send file to mass users or gourps\n    \"\"\"\n    j = mass_send(request.method, request.json, wechat.webwxsendappmsg)\n    return jsonify(j)\n\n\ndef run_server():\n    app.run(port=PORT)\n\nif cm.get('setting', 'server_mode') == 'True':\n    serverProcess = threading.Thread(target=run_server)\n    serverProcess.start()\n\nwhile True:\n    try:\n        wechat.start()\n    except KeyboardInterrupt:\n        echo(Constant.LOG_MSG_QUIT)\n        wechat.exit_code = 1\n    else:\n        Log.error(traceback.format_exc())\n    finally:\n        wechat.stop()\n    \n    # send a mail to tell the wxbot is failing\n    subject = 'wxbot stop message'\n    log_file = open(eval(cm.get('handler_fileHandler', 'args'))[0], 'r')\n    mail_content = '<pre>' + str(wechat) + '\\n\\n-----\\nLogs:\\n-----\\n\\n' + ''.join(log_file.readlines()[-100:]) + '</pre>'\n    sg.send_mail(subject, mail_content, 'text/html')\n    log_file.close()\n\n    if wechat.exit_code == 0:\n        echo(Constant.MAIN_RESTART)\n    else:\n        # kill process\n        os.system(Constant.LOG_MSG_KILL_PROCESS % os.getpid())\n"
  },
  {
    "path": "wxbot_project_py2.7/wx_handler/__init__.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\nfrom wechat_msg_processor import WeChatMsgProcessor\nfrom bot import Bot\nfrom sendgrid_mail import SGMail "
  },
  {
    "path": "wxbot_project_py2.7/wx_handler/bot.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom wechat.utils import *\nfrom config import Constant\n#---------------------------------------------------\nimport random, time, json\n#===================================================\n\n\nclass Bot(object):\n\n    def __init__(self):\n        self.emoticons = Constant.EMOTICON\n        self.gifs = []\n        self.last_time = time.time()\n    \n    def time_schedule(self):\n        r = ''\n        now = time.time()\n        if int(now - self.last_time) > 3600:\n            self.last_time = now\n            url_latest = Constant.BOT_ZHIHU_URL_LATEST\n            url_daily = Constant.BOT_ZHIHU_URL_DAILY\n            data = get(url_latest)\n            j = json.loads(data)\n            story = j['stories'][random.randint(0, len(j['stories'])-1)]\n            r = story['title'] + '\\n' + url_daily + str(story['id'])\n        return r.encode('utf-8')\n\n    def reply(self, text):\n        APIKEY = Constant.BOT_TULING_API_KEY\n        api_url = Constant.BOT_TULING_API_URL % (APIKEY, text, '12345678')\n        r = json.loads(get(api_url))\n        if r.get('code') == 100000 and r.get('text') != Constant.BOT_TULING_BOT_REPLY:\n            p = random.randint(1, 10)\n            if p > 3:\n                return r['text']\n            elif p > 1:\n                # send emoji\n                if random.randint(1, 10) > 5:\n                    n = random.randint(0, len(self.emoticons)-1)\n                    m = random.randint(1, 3)\n                    reply = self.emoticons[n].encode('utf-8') * m\n                    return reply\n        return ''\n"
  },
  {
    "path": "wxbot_project_py2.7/wx_handler/sendgrid_mail.py",
    "content": "#!/usr/bin/env python\r\n# coding: utf-8\r\n\r\n#===================================================\r\nimport sendgrid\r\nfrom sendgrid.helpers.mail import *\r\n#===================================================\r\n\r\nclass SGMail(object):\r\n\r\n    def __init__(self, apikey, from_email, to_email):\r\n        self.sg = sendgrid.SendGridAPIClient(apikey=apikey)\r\n        self.from_email = Email(from_email)\r\n        self.to_email = Email(to_email)\r\n\r\n    def send_mail(self, subject, plain_content, type='text/plain'):\r\n        content = Content(type, plain_content)\r\n        mail = Mail(self.from_email, subject, self.to_email, content)\r\n        response = self.sg.client.mail.send.post(request_body=mail.get())\r\n        return response.status_code == 202\r\n"
  },
  {
    "path": "wxbot_project_py2.7/wx_handler/wechat_msg_processor.py",
    "content": "#!/usr/bin/env python\n# coding: utf-8\n\n#===================================================\nfrom wechat.utils import *\nfrom config import ConfigManager\nfrom config import Constant\nfrom config import Log\n#---------------------------------------------------\nimport os\nimport time\nimport json\nimport re\n#===================================================\n\n\nclass WeChatMsgProcessor(object):\n    \"\"\"\n    Process fetched data\n    \"\"\"\n\n    def __init__(self, db):\n        self.db = db\n        self.wechat = None  # recieve `WeChat` class instance\n                            # for call some wechat apis\n\n        # read config\n        cm = ConfigManager()\n        [self.upload_dir, self.data_dir, self.log_dir] = cm.setup_database()\n\n    def clean_db(self):\n        \"\"\"\n        @brief clean database, delete table & create table\n        \"\"\"\n        self.db.delete_table(Constant.TABLE_GROUP_LIST())\n        self.db.delete_table(Constant.TABLE_GROUP_USER_LIST())\n        self.db.create_table(Constant.TABLE_GROUP_MSG_LOG, Constant.TABLE_GROUP_MSG_LOG_COL)\n        self.db.create_table(Constant.TABLE_GROUP_LIST(), Constant.TABLE_GROUP_LIST_COL)\n        self.db.create_table(Constant.TABLE_GROUP_USER_LIST(), Constant.TABLE_GROUP_USER_LIST_COL)\n        self.db.create_table(Constant.TABLE_RECORD_ENTER_GROUP, Constant.TABLE_RECORD_ENTER_GROUP_COL)\n        self.db.create_table(Constant.TABLE_RECORD_RENAME_GROUP, Constant.TABLE_RECORD_RENAME_GROUP_COL)\n\n    def handle_wxsync(self, msg):\n        \"\"\"\n        @brief      Recieve webwxsync message, saved into json\n        @param      msg  Dict: webwxsync msg\n        \"\"\"\n        fn = time.strftime(Constant.LOG_MSG_FILE, time.localtime())\n        save_json(fn, msg, self.log_dir, 'a+')\n\n    def handle_group_list(self, group_list):\n        \"\"\"\n        @brief      handle group list & saved in DB\n        @param      group_list  Array\n        \"\"\"\n        fn = Constant.LOG_MSG_GROUP_LIST_FILE\n        save_json(fn, group_list, self.data_dir)\n        cols = [(\n            g['NickName'],\n            g['UserName'],\n            g['OwnerUin'],\n            g['MemberCount'],\n            g['HeadImgUrl']\n        ) for g in group_list]\n        self.db.insertmany(Constant.TABLE_GROUP_LIST(), cols)\n\n    def handle_group_member_list(self, group_id, member_list):\n        \"\"\"\n        @brief      handle group member list & saved in DB\n        @param      member_list  Array\n        \"\"\"\n        fn = group_id + '.json'\n        save_json(fn, member_list, self.data_dir)\n        cols = [(\n            group_id,\n            m['UserName'],\n            m['NickName'],\n            m['DisplayName'],\n            m['AttrStatus']\n        ) for m in member_list]\n        self.db.insertmany(Constant.TABLE_GROUP_USER_LIST(), cols)\n\n    def handle_group_list_change(self, new_group):\n        \"\"\"\n        @brief      handle adding a new group & saved in DB\n        @param      new_group  Dict\n        \"\"\"\n        self.handle_group_list([new_group])\n\n    def handle_group_member_change(self, group_id, member_list):\n        \"\"\"\n        @brief      handle group member changes & saved in DB\n        @param      group_id  Dict\n        @param      member_list  Dict\n        \"\"\"\n        self.db.delete(Constant.TABLE_GROUP_USER_LIST(), \"RoomID\", group_id)\n        self.handle_group_member_list(group_id, member_list)\n\n    def handle_group_msg(self, msg):\n        \"\"\"\n        @brief      Recieve group messages\n        @param      msg  Dict: packaged msg\n        \"\"\"\n        # rename media files\n        for k in ['image', 'video', 'voice']:\n            if msg[k]:\n                t = time.localtime(float(msg['timestamp']))\n                time_str = time.strftime(\"%Y%m%d%H%M%S\", t)\n                # format: 时间_消息ID_群名\n                file_name = '/%s_%s_%s.' % (time_str, msg['msg_id'], msg['group_name'])\n                new_name = re.sub(r'\\/\\w+\\_\\d+\\.', file_name, msg[k])\n                Log.debug('rename file to %s' % new_name)\n                os.rename(msg[k], new_name)\n                msg[k] = new_name\n\n        if msg['msg_type'] == 10000:\n            # record member enter in group\n            m = re.search(r'邀请(.+)加入了群聊', msg['sys_notif'])\n            if m:\n                name = m.group(1)\n                col_enter_group = (\n                    msg['msg_id'],\n                    msg['group_name'],\n                    msg['from_user_name'],\n                    msg['to_user_name'],\n                    name,\n                    msg['time'],\n                )\n                self.db.insert(Constant.TABLE_RECORD_ENTER_GROUP, col_enter_group)\n\n            # record rename group\n            n = re.search(r'(.+)修改群名为“(.+)”', msg['sys_notif'])\n            if n:\n                people = n.group(1)\n                to_name = n.group(2)\n                col_rename_group = (\n                    msg['msg_id'],\n                    msg['group_name'],\n                    to_name,\n                    people,\n                    msg['time'],\n                )\n                self.db.insert(Constant.TABLE_RECORD_RENAME_GROUP, col_rename_group)\n                \n                # upadte group in GroupList\n                for g in self.wechat.GroupList:\n                    if g['UserName'] == msg['from_user_name']:\n                        g['NickName'] = to_name\n                        break\n\n        # normal group message\n        col = (\n            msg['msg_id'],\n            msg['group_owner_uin'],\n            msg['group_name'],\n            msg['group_count'],\n            msg['from_user_name'],\n            msg['to_user_name'],\n            msg['user_attrstatus'],\n            msg['user_display_name'],\n            msg['user_nickname'],\n            msg['msg_type'],\n            msg['emoticon'],\n            msg['text'],\n            msg['image'],\n            msg['video'],\n            msg['voice'],\n            msg['link'],\n            msg['namecard'],\n            msg['location'],\n            msg['recall_msg_id'],\n            msg['sys_notif'],\n            msg['time'],\n            msg['timestamp']\n        )\n        self.db.insert(Constant.TABLE_GROUP_MSG_LOG, col)\n\n        text = msg['text']\n        if text and text[0] == '@':\n            n = trans_coding(text).find(u'\\u2005')\n            name = trans_coding(text)[1:n].encode('utf-8')\n            if name in [self.wechat.User['NickName'], self.wechat.User['RemarkName']]:\n                self.handle_command(trans_coding(text)[n+1:].encode('utf-8'), msg)\n\n    def handle_user_msg(self, msg):\n        \"\"\"\n        @brief      Recieve personal messages\n        @param      msg  Dict\n        \"\"\"\n        wechat = self.wechat\n\n        text = trans_coding(msg['text']).encode('utf-8')\n        uid = msg['raw_msg']['FromUserName']\n\n        if text == 'test_revoke': # 撤回消息测试\n            dic = wechat.webwxsendmsg('这条消息将被撤回', uid)\n            wechat.revoke_msg(dic['MsgID'], uid, dic['LocalID'])\n        elif text == 'reply':\n            wechat.send_text(uid, '自动回复')\n\n\n    def handle_command(self, cmd, msg):\n        \"\"\"\n        @brief      handle msg of `@yourself cmd`\n        @param      cmd   String\n        @param      msg   Dict\n        \"\"\"\n        wechat = self.wechat\n        g_id = ''\n        for g in wechat.GroupList:\n            if g['NickName'] == msg['group_name']:\n                g_id = g['UserName']\n\n        cmd = cmd.strip()\n        if cmd == 'runtime':\n            wechat.send_text(g_id, wechat.get_run_time())\n        elif cmd == 'test_sendimg':\n            wechat.send_img(g_id, 'test/emotion/7.gif')\n        elif cmd == 'test_sendfile':\n            wechat.send_file(g_id, 'test/Data/upload/shake.wav')\n        elif cmd == 'test_bot':\n            # reply bot\n            # ---------\n            if wechat.bot:\n                r = wechat.bot.reply(cmd)\n                if r:\n                    wechat.send_text(g_id, r)\n                else:\n                    pass\n        elif cmd == 'test_emot':\n            img_name = [\n                '0.jpg', '1.jpeg', '2.gif', '3.jpg', '4.jpeg',\n                '5.gif', '6.gif', '7.gif', '8.jpg', '9.jpg'\n            ]\n            name = img_name[int(time.time()) % 10]\n            emot_path = os.path.join('test/emotion/', name)\n            wechat.send_emot(g_id, emot_path)\n        else:\n            pass\n\n    def check_schedule_task(self):\n        # update group member list at 00:00 am every morning\n        t = time.localtime()\n        if t.tm_hour == 0 and t.tm_min <= 1:\n            # update group member\n            Log.debug('update group member list everyday')\n            self.db.delete_table(Constant.TABLE_GROUP_LIST())\n            self.db.delete_table(Constant.TABLE_GROUP_USER_LIST())\n            self.db.create_table(Constant.TABLE_GROUP_LIST(), Constant.TABLE_GROUP_LIST_COL)\n            self.db.create_table(Constant.TABLE_GROUP_USER_LIST(), Constant.TABLE_GROUP_USER_LIST_COL)\n            self.wechat.fetch_group_contacts()\n\n"
  }
]