wechaty / python-wechaty

Python Wechaty is a Conversational RPA SDK for Chatbot Makers written in Python
https://wechaty.readthedocs.io/zh_CN/latest/
Apache License 2.0
1.55k stars 229 forks source link

Update user.url_link #399

Closed doublewinter0 closed 11 months ago

doublewinter0 commented 1 year ago

In user.url_link.py, we use urllib3.util.url.get_host method, which is deprecated.

def get_host(url):
    """
    Deprecated. Use :func:`parse_url` instead.
    """
    p = parse_url(url)
    return p.scheme or "http", p.hostname, p.port

So I use the recommended method urllib3.util.url.parse_url instead. As a matter of fact, the deprecated method is removed in newer version.

There seems to be a small problem with the code, as follows:

@staticmethod
def can_parser(url: str) -> bool:
    """the source of url

    Args:
        url (str): github urllink

    Returns:
        bool: whether is github based url
    """
    return parse_url(url).hostname

The method's declared return type does not appear to match the actual return type.

doublewinter0 commented 1 year ago

@wj-Mcat Review 一下

gsfish commented 11 months ago

运行 BOT 时遇到同样的报错:

Traceback (most recent call last):
  File "/data/Projects/wechaty-pratice/main.py", line 21, in <module>
    from wechaty import (
  File "/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/wechaty/__init__.py", line 54, in <module>
    from .wechaty import (
  File "/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/wechaty/wechaty.py", line 72, in <module>
    from wechaty.user.url_link import UrlLink
  File "/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/wechaty/user/__init__.py", line 8, in <module>
    from .message         import Message
  File "/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/wechaty/user/message.py", line 51, in <module>
    from .url_link import UrlLink
  File "/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/wechaty/user/url_link.py", line 10, in <module>
    from urllib3 import get_host
ImportError: cannot import name 'get_host' from 'urllib3' (/data/Projects/wechaty-pratice/venv/lib/python3.10/site-packages/urllib3/__init__.py)

目前我的解决方案是重新安装 2.0.0 版本以下的 urllib3 库(并加入 requirements.txt):

pip install -U 'urllib3<2'

上述问题的原因是 urllib3 在 2.0.0 版本中移除了部分弃用的 API(相关变更:https://github.com/urllib3/urllib3/compare/1.26.16...2.0.0a1#diff-793e73e680de400394dedf7d080fbcef6e74c065ffcc10b8c3dc11217a786e0bL38 ),其中就包括目前 wechaty/user/url_link.py 所使用的 get_host(相关 PR:https://github.com/urllib3/urllib3/pull/2139

wj-Mcat commented 11 months ago

LGTM

wj-Mcat commented 11 months ago

@gsfish thanks for your suggestion, I have added the package dependency rule in https://github.com/wechaty/python-wechaty/commit/5892250b6d0b40119d510ae17619c7fbaf8623a4

liyancai commented 2 months ago

pip3 install wechaty 最新版 还是0.10.7 并没有更新到0.10.8版本