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] Private API for searching hashtags throws a validation error #1805

Open Overk1lls opened 7 months ago

Overk1lls commented 7 months ago

Describe the bug I need to search for hashtags on Instagram. At first, I tried using the Public API, but it seems not working (the same problem as described here). Then I switched to using the private API endpoint (/tags/search/). But every time I get a successful response from Instagram (logs checking), the validation error pops up (and my API returns 400/500 as a result):

{
  "detail": "1 validation error for Hashtag\nid\n  Input should be a valid string [type=string_type, input_value=17843993695028891, input_type=int]\n    For further information visit https://errors.pydantic.dev/2.5/v/string_type"
}

I checked the Hashtag model and its id property has str type, while getting the int type (e.g. 17843993695028891):

[
  {
    "id": 17843915557058484,
    "name": "restaurant",
    "media_count": 65043150,
    "profile_pic_url": null
  },
]

So I updated the type from str to str | int and it started working without any exceptions:

class Hashtag(BaseModel):
    id: str | int

I thought you guys might have a look at this and change/update or give some kind of answer/clarification to this.

To Reproduce I have an endpoint to search for hashtags, something like this:

async def get_hashtags(
  query: Annotated[str, Query(min_length=1)],
  db: Session = Depends(get_db),
) -> list[Hashtag]:
  try:
    cl = Client()
    // ... proxy, session, etc...

    hashtags = cl.search_hashtags(query)
    hashtags = sorted(hashtags, key=lambda x: x.media_count or 0, reverse=True)

    return hashtags
  except Exception as e:
    raise HTTPException(400, str(e))

Traceback

Traceback (most recent call last):
  File "/Users/user_bot/Apps/test-python/routers/hashtags.py", line 29, in get_hashtags
    hashtags = cl.search_hashtags(query)
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user_bot/Apps/test-python/.venv/lib/python3.12/site-packages/instagrapi/mixins/fbsearch.py", line 67, in search_hashtags
    return [extract_hashtag_v1(ht) for ht in result["results"]]
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/user_bot/Apps/test-python/.venv/lib/python3.12/site-packages/instagrapi/extractors.py", line 369, in extract_hashtag_v1
    return Hashtag(**data)
           ^^^^^^^^^^^^^^^
  File "/Users/user_bot/Apps/test-python/.venv/lib/python3.12/site-packages/pydantic/main.py", line 164, in __init__
    __pydantic_self__.__pydantic_validator__.validate_python(data, self_instance=__pydantic_self__)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Hashtag
id
  Input should be a valid string [type=string_type, input_value=17841564091087227, input_type=int]
    For further information visit https://errors.pydantic.dev/2.5/v/string_type

Expected behavior To not throw any exceptions and return a list of hashtags

Desktop (please complete the following information):

jarrodnorwell commented 7 months ago

@Overk1lls can you try my fork? pip install git+https://github.com/jarrodnorwell/instagrapi, it contains a potential fix for the issue you're having

Overk1lls commented 7 months ago

@Overk1lls can you try my fork? pip install git+https://github.com/jarrodnorwell/instagrapi, it contains a potential fix for the issue you're having

Yeah, it seems working now. Thanks for the answer!