subzeroid / instagrapi

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

[BUG] instagrapi.exceptions.LoginRequired on hashtag_medias_recent with amount #1576

Open Kweiza opened 1 year ago

Kweiza commented 1 year ago

Describe the bug If the amount increases by more than a certain number, a LoginRequired exception occurs.

To Reproduce After cl.login, cl.hashtag_medias_recent(tag, amount=100) is working but, cl.hashtag_medias_recent(tag, amount=10000) is not working. occurring LoginRequired Exception

Traceback

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/public.py", line 168, in _send_public_request
    response.raise_for_status()
  File "/opt/homebrew/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://www.instagram.com/explore/tags/%EA%B3%B5%EA%B5%AC%EC%98%88%EA%B3%A0/?__a=1&__d=dis

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 425, in hashtag_medias_recent
    medias = self.hashtag_medias_recent_a1(name, amount)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 388, in hashtag_medias_recent_a1
    return self.hashtag_medias_a1(name, amount, tab_key="edge_hashtag_to_media")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 224, in hashtag_medias_a1
    medias, _ = self.hashtag_medias_a1_chunk(name, amount, tab_key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 166, in hashtag_medias_a1_chunk
    data = self.public_a1_request(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/public.py", line 213, in public_a1_request
    response = self.public_request(
               ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/public.py", line 123, in public_request
    raise e
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/public.py", line 100, in public_request
    return self._send_public_request(url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/public.py", line 201, in _send_public_request
    raise ClientError(e, response=e.response)
instagrapi.exceptions.ClientError: 401 Client Error: Unauthorized for url: https://www.instagram.com/explore/tags/%EA%B3%B5%EA%B5%AC%EC%98%88%EA%B3%A0/?__a=1&__d=dis

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/private.py", line 360, in _send_private_request
    response.raise_for_status()
  File "/opt/homebrew/lib/python3.11/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://i.instagram.com/api/v1/tags/%EA%B3%B5%EA%B5%AC%EC%98%88%EA%B3%A0/sections/?max_id=QVFCSUZBQThLZEJwUGlzamFBUVczV21NT09EbkkxUW9Tb3lTb2p5OVZrWVZxaTBFazQ0NEdiZmZMZTZtbmpDMkpUMVRDdHZfd19PalZMNVlaN1BKS25VaQ%3D%3D

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kweiza/Developments/mintc/insta-dm/main.py", line 70, in <module>
    crawlUserData(tag)
  File "/Users/kweiza/Developments/mintc/insta-dm/main.py", line 15, in crawlUserData
    media = cl.hashtag_medias_recent(tag, amount=1000)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 427, in hashtag_medias_recent
    medias = self.hashtag_medias_recent_v1(name, amount)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 406, in hashtag_medias_recent_v1
    return self.hashtag_medias_v1(name, amount, tab_key="recent")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 309, in hashtag_medias_v1
    medias, _ = self.hashtag_medias_v1_chunk(name, amount, tab_key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/hashtag.py", line 266, in hashtag_medias_v1_chunk
    result = self.private_request(
             ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/private.py", line 542, in private_request
    raise e
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/private.py", line 527, in private_request
    self._send_private_request(endpoint, **kwargs)
  File "/opt/homebrew/lib/python3.11/site-packages/instagrapi/mixins/private.py", line 386, in _send_private_request
    raise LoginRequired(response=e.response, **last_json)
instagrapi.exceptions.LoginRequired: login_required

Expected behavior Return all hashtag media equivalent to the total amount

Screenshots

image image image

Desktop (please complete the following information):

MohammadHosein21 commented 1 year ago

for login use this method:

def login(username, password): cl = Client()

cl.delay_range = [2, 5]

try:
    if os.path.exists(f'settings({username}).json'):
        cl.load_settings(f'settings({username}).json')
        if cl.login(username=username, password=password) == True:
            cl.dump_settings(f'settings({username}).json')
    else:
        if cl.login(username=username, password=password) == True:
            cl.dump_settings(f'settings({username}).json')
    print('login successfully')
except Exception as e:
    print(e)
return cl
Kweiza commented 1 year ago

@MohammadHosein21 it is not working

def login(username, password):
    cl = Client()
    # cl.delay_range = [2, 5]
    try:
        if os.path.exists(f"settings({username}).json"):
            cl.load_settings(f"settings({username}).json")
            if cl.login(username=username, password=password) == True:
                cl.dump_settings(f"settings({username}).json")
        else:
            if cl.login(username=username, password=password) == True:
                cl.dump_settings(f"settings({username}).json")
        print("login successfully")
        return cl

    except Exception as e:
        print(e)

print "login successfully" but client functions are not working. functions throw exception LoginRequired

MohammadHosein21 commented 1 year ago

return cl must be in the last line and use instagrapi version 2.0.0