subzeroid / instagrapi

🔥 The fastest and powerful Python library for Instagram Private API 2024
https://hikerapi.com/p/bkXQlaVe
MIT License
4.21k stars 667 forks source link

[BUG] 'pydantic_core._pydantic_core.Url' object has no attribute 'decode' #1703

Closed Zolilio closed 8 months ago

Zolilio commented 9 months ago

I tried downloading images from url with a simple code but I got the error "AttributeError: 'pydantic_core._pydantic_core.Url' object has no attribute 'decode'"

The code I used:

import instagrapi as ig

dir_path = "C:/path/to/dir"
img_url = "https://www.instagram.com/p/XXXXXXX/"

cl = ig.Client()
cl.login("xxx", "xxx")

pk = cl.media_pk_from_url(img_url)
cl.album_download(pk)

Here is the detail traceback:

Traceback (most recent call last):

  File ~\anaconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\users\XXX\instagram_scrapper.py:10
    cl.album_download(pk)

  File ~\anaconda3\lib\site-packages\instagrapi\mixins\album.py:44 in album_download
    self.photo_download_by_url(resource.thumbnail_url, filename, folder)

  File ~\anaconda3\lib\site-packages\instagrapi\mixins\photo.py:90 in photo_download_by_url
    fname = urlparse(url).path.rsplit("/", 1)[1]

  File ~\anaconda3\lib\urllib\parse.py:399 in urlparse
    url, scheme, _coerce_result = _coerce_args(url, scheme)

  File ~\anaconda3\lib\urllib\parse.py:136 in _coerce_args
    return _decode_args(args) + (_encode_result,)

  File ~\anaconda3\lib\urllib\parse.py:120 in _decode_args
    return tuple(x.decode(encoding, errors) if x else '' for x in args)

  File ~\anaconda3\lib\urllib\parse.py:120 in <genexpr>
    return tuple(x.decode(encoding, errors) if x else '' for x in args)

AttributeError: 'pydantic_core._pydantic_core.Url' object has no attribute 'decode'
CxrlosKenobi commented 9 months ago

I've been dealing with the same error today. Looking forward for the fix 👁️

CxrlosKenobi commented 9 months ago

@Zolilio This bug seems to be at the photo_download_by_url() within the DownloadPhotoMixin. Precisely in the line 90, when the urlparse() function is called with the url param of type <class 'pydantic_core._pydantic_core.Url'>, when the urlparse() function only accepts its url param with <class 'str'> type.

https://github.com/subzeroid/instagrapi/blob/da97c12349cd70f584fde9d2e6d3dc02b5a81e68/instagrapi/mixins/photo.py#L69-L98

So, leaving that line with the url wrapped with str(), seems to make it work 🙂 (at least it did on my machine!).

fname = urlparse(str(url)).path.rsplit("/", 1)[1]

This is a simple patch I made on my local version of the lib. I hope the maintainers can fix the issue setting up the proper types soon 😁.

jangrewe commented 8 months ago

Thanks, this works great! I had to also apply this in a similar fashion to video.py and album.py, though. Not sure if that covers all types, but at least that works for me now.

@subzeroid could you have a look if this can be merged?