youfou / wxpy

微信机器人 / 可能是最优雅的微信个人号 API ✨✨
http://wxpy.readthedocs.io
MIT License
13.98k stars 2.38k forks source link

New-core 分支:无法下载图片,发图片 #248

Open chenbotao828 opened 6 years ago

chenbotao828 commented 6 years ago

如题,get_file file_name file_ext 均无返回,代码如下~

In [9]: msg = bot.messages[-1]

In [10]: msg
Out[10]: cbt666 : (IMAGE)

In [11]: msg.file_name

In [12]: msg.file_ext

In [13]: msg.get_file('xxx.jpeg')

In [14]: msg.raw
Out[14]: 
{'AppInfo': {'AppID': '', 'Type': 0},
 'AppMsgType': 0,
 'Content': '@a8c40642395f2ed54f17d7dfde92684fd8caf1872d54010479c2508900ee4e94dcd81ba64208dccff25f191414b26dbdfc58278b66a6bbe8ef0b605bb9ea308a11a917c36cc55b294e74f64fd8b124597da8efca89a4dc037e4e3664a8f04a5103192b768a2307575353556539168f58705bde2c71784df17e34ddf3cd7903787cacf878a184b4f84e8a218ddc4a04ecd6faa8e33a173057ccc448ae4d594b4202c4a4a3922c75b2af9bfad515bd8924d2e829a156aa622535bfe181d191f2442e71477b9196e2bf92beddf3dfab4fb2c3642377b153608b25b47b5736826c60d3ff11f1d63feab9b29f1c032c726a571f7b8730be224340dfeb71ffd3e9a7ed6a4f9b824d4c62b703e89ac023a15b6fdbd053f7dbaab1bfe2246e8001e336b1afc81c14b9257a60ab4fa1171e45554b60d65118813614414c8de15a2b5f09697b0faccaf7441afdb68f1a0207fe728bedfc12462359b0787efae486e530b5f38b55aca4302ac1e0eb48c782b485b46cc2c028860b88a0598f1d0cb588934e52ba8940292fd12c3cd09385e20dc109c1e187d3d2de316d18013e8b2d62471dcbfe0ea80f1a5cc8ed32d3633f9968a2906650889df3bdba4e0be8105c76383cab8045ff313d438bcb6510953cf860c4ff13ce63d205c464786a68850b57a93aa944f43394df9b21d85c9af3d04c19406424293b4f801fee571050552257fa4c0d908338c5ff23d20ea8c9aafdb9e2c6930adc3653928e0dcfe76951fd97e0e19c03b968356dc6d6e9f6ad82e031ca1f7ba7e583daefc77f5b8aa9af85a5c4998f842bf5a115dca41963a3274a0780a66f32ea4bc0090cf67d26e0d6d1a69f60ccfd192bc0934d17b926a5945b1d1edb9cdf1061c5dccc05b616102a7c233b1d7c6313ab476e1dd95b93265ff13632bee84b66af5ad93175a6295dad3a18a56d00c7ef447aeda11c9a441da1811c223304',
 'CreateTime': 1515862402,
 'EncryFileName': '',
 'FileName': '',
 'FileSize': '',
 'ForwardFlag': 0,
 'FromUserName': '@ad598682bec0122488e5d24e6ed1919481a1c64caa1bf10b7acdd4b346fe773a',
 'HasProductId': 0,
 'ImgHeight': 120,
 'ImgStatus': 2,
 'ImgWidth': 67,
 'MediaId': '',
 'MsgId': '9000531098124382579',
 'MsgType': 3,
 'NewMsgId': 9000531098124382579,
 'OriContent': '',
 'PlayLength': 0,
 'RecommendInfo': {'Alias': '',
  'AttrStatus': 0,
  'City': '',
  'Content': '',
  'NickName': '',
  'OpCode': 0,
  'Province': '',
  'QQNum': 0,
  'Scene': 0,
  'Sex': 0,
  'Signature': '',
  'Ticket': '',
  'UserName': '',
  'VerifyFlag': 0},
 'Status': 3,
 'StatusNotifyCode': 0,
 'StatusNotifyUserName': '',
 'SubMsgType': 0,
 'Ticket': '',
 'ToUserName': '@216bef90305cba33f58c8787d8237123a443cea0e61bd73b15ac58a9323bd056',
 'Url': '',
 'VoiceLength': 0}

Content 这个数据为什么是“@”开头的一串数字? 补充,wxpy master 分支实验过,正常,没有出现类似情况。

chenbotao828 commented 6 years ago

更新一下,上面的bug是笔记本用手机热点信号时产生的,在宽带连接时没有出现这样的情况,可能是网速的问题?

mapleflow commented 6 years ago

源码有bug吧,改下源码吧。

chenbotao828 commented 6 years ago

之前没在意,现在发现确实不行

class Message(object):
    # ...........
    @property
    def _file_url(self):

        """
        | 消息中文件的下载地址 (内部使用)
        | 注意: 该 URL 会验证 cookies, 只能使用登陆所在的 session 进行下载
        | 若需下载,请使用 `get_file()` 方法
        """

        uris = self.core.uris
        tree = self._content_xml ###########>>>>>> 【 这里 tree 为空】

        upper_params = {'MsgID': self.id, 'skey': self.core.data.skey, 'type': 'big'}
        lower_params = {'msgid': self.id, 'skey': self.core.data.skey}

        match = {
            IMAGE: (uris.get_msg_img, upper_params),
            STICKER: (uris.get_msg_img, upper_params),
            VOICE: (uris.get_voice, lower_params),
            VIDEO: (uris.get_video, lower_params),
        }.get(self.type)

        if tree and match: 
            return '{}?{}'.format(match[0], urlencode(match[1]))
        elif self.type == FILE:
            return '{}?{}'.format(uris.get_media, urlencode(dict(
                sender=self.raw['FromUserName'],
                mediaid=self.media_id,
                filename=self._content_xml.findtext('.//title'),
                fromuser=self.core.data.raw_self['Uin'],
                pass_ticket=self.core.data.pass_ticket,
                webwx_data_ticket=self.core.from_cookies('webwx_data_ticket')
            )))

    # ...........
    @property
    def _content_xml(self):
        """ Content 字段中的 xml 对象 """
        try:
            #########>>>>>>【 这里的self._content 就是一楼的’Content‘ @a8c40642395f2ed5...' 】
            return ETree.fromstring(self._content) 
        except ETree.ParseError:
            pass

还有一个新Bug 发送图片也会报错:

In [70]: img.reply_image('XX.jpg')
---------------------------------------------------------------------------
ResponseError                             Traceback (most recent call last)
<ipython-input-70-b9b23607b36a> in <module>()
----> 1 img.reply_image('XX.jpg')

~/my_wxpy_app/venv/lib/python3.6/site-packages/wxpy/api/chats/chat.py in send_image(self, path, media_id)
    103         """
    104 
--> 105         return self.send(path, IMAGE, media_id)
    106 
    107     def send_sticker(self, path, media_id=None):

~/my_wxpy_app/venv/lib/python3.6/site-packages/wxpy/api/chats/chat.py in send(self, content, msg_type, media_id)
     91         """
     92 
---> 93         return self.core.send(self, content, msg_type, media_id)
     94 
     95     def send_image(self, path, media_id=None):

~/my_wxpy_app/venv/lib/python3.6/site-packages/wxpy/api/core.py in send(self, receiver, content, msg_type, media_id, **kwargs)
    681             else:
    682                 msg_dict['MediaId'] = media_id
--> 683 
    684         # request
    685 

~/my_wxpy_app/venv/lib/python3.6/site-packages/wxpy/api/core.py in post(self, url, ext_data, load_resp, **kwargs)
    165 
    166         resp = self.session.post(url, **kwargs)
--> 167         if load_resp:
    168             resp = self.load_response_as_json(resp)
    169         return resp

~/my_wxpy_app/venv/lib/python3.6/site-packages/wxpy/api/core.py in load_response_as_json(self, resp)
   1102                 err_msg = base_response['ErrMsg']
   1103             except (KeyError, TypeError):
-> 1104                 logger.error('failed to parse base_response:\n{}'.format(json_dict['BaseResponse']))
   1105             else:
   1106                 if err_code != 0:

ResponseError: <Core: 小红>: err_code: 1204; err_msg: 

请教@youfou @mapleflow 怎么解决? 是否是文字信息和图片信息的get post 方式有区别?然后new-core没有考虑到?

chenbotao828 commented 6 years ago

@youfou @mapleflow @hanx11 @bluedazzle @ourbest 各位大侠,这个 issue 在 new-core 还是蛮蛋疼的,你们是否有时间能看下是什么问题,我能力有限,还请各位大侠出手~谢谢~~~!!!!