subzeroid / instagrapi

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

Make populating location data optional #653

Closed dougwettlaufer closed 2 years ago

dougwettlaufer commented 2 years ago

Is your feature request related to a problem? Please describe.

For my use case I can accomplish everything I need with just the public API, but the problem I run into is my media_info requests fail when a location is returned. What I'm running into is inside media_info_gql I get to this if block

if data["shortcode_media"]["location"]:
    data["shortcode_media"]["location"] = self.location_complete(
        extract_location(data["shortcode_media"]["location"])
    ).dict()

and for my case eventually makes it to location_search which requires a private request. Since my requests are not authenticated this fails with a stack like

Traceback (most recent call last):
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/public.py", line 128, in _send_public_request
    response.raise_for_status()
  File "/redacted/venv/lib/python3.8/site-packages/requests/models.py", line 960, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: https://www.instagram.com/explore/locations/271456804/?__a=1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/location.py", line 171, in location_info
    location = self.location_info_a1(location_pk)
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/location.py", line 132, in location_info_a1
    data = self.public_a1_request(f"/explore/locations/{location_pk}/") or {}
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/public.py", line 173, in public_a1_request
    response = self.public_request(
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/public.py", line 89, in public_request
    raise e
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/public.py", line 72, in public_request
    return self._send_public_request(url, **kwargs)
  File "/redacted/venv/lib/python3.8/site-packages/instagrapi/mixins/public.py", line 161, in _send_public_request
    raise ClientError(e, response=e.response)
instagrapi.exceptions.ClientError: 500 Server Error: Internal Server Error for url: https://www.instagram.com/explore/locations/271456804/?__a=1

Describe the solution you'd like

Since the media gql already contains everything I need there's no benefit to populating the location data so I'd like the option to disable it.

One option I've looked at is changing media_info_gql from

if data["shortcode_media"]["location"]:

to

if data["shortcode_media"]["location"] and self.authorization:

Although it looks like there's other places where location_complete is called which would still fail if you're not authenticated, but maybe that's acceptable? If it would be preferred to centralize this logic I guess there could be a check at the top of location_complete and if self.authorization isn't set then just return None.

Describe alternatives you've considered

Instead of flexing on the authorization attribute another, more explicit option would be to add a new populate_location config that gets checked before calling any of the methods in LocationMixin.

dougwettlaufer commented 2 years ago

@adw0rd I'm happy to put up a PR just let me know which would be the preferred way to handle this.