Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Private API for searching hashtags throws a validation error #1805

Open
Overk1lls opened this issue Feb 16, 2024 · 2 comments
Open

[BUG] Private API for searching hashtags throws a validation error #1805

Overk1lls opened this issue Feb 16, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Overk1lls
Copy link

Overk1lls commented Feb 16, 2024

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):

  • OS: macOS Big Sur
  • Python version [e.g. 3.8.3] 3.12
  • instagrapi version [e.g. 1.9.3, not "latest"] 2.0.1
@Overk1lls Overk1lls added the bug Something isn't working label Feb 16, 2024
@jarrodnorwell
Copy link
Contributor

@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
Copy link
Author

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants