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

[BUG] #1694

Open Dua-Jan-Muhammad opened 11 months ago

Dua-Jan-Muhammad commented 11 months ago

hashtag_medias_top function giving key error.

Describe the bug hashtag_medias_top function as well as hashtag_medias_recent both are producing the same error which returns KeyError: 'data'.

To Reproduce To reproduce the issue, please call the following function. medias = cl.hashtag_medias_top('love', amount=15)

a complete code can be written as:

from instagrapi import Client
import csv
import time
import random

cl = Client()

def read_credentials(filename):
    '''
    Read the credentials from the file and return the list of credentials one line at a time
    args:
        filename: the name of the file to read
    returns:
        list of credentials   
    '''
    with open(filename,encoding="utf8", errors="ignore") as f:
        reader = csv.reader(f)
        data = list(reader)
        print(data[0])
    return data[0]

def login(credential):
    '''
    logins to instagram
    args:
        credential: list of username and password. username at index 0 and password at index 1
    returns:
        None    
    '''
    cl.login(credential[0],credential[1])
    print("login as ",credential[0])
    ran = random.randrange(10,18)
    print("sleeping for {} seconds".format(ran))
    time.sleep(ran)

def get_hashtags_users(hashtag):
    medias = cl.hashtag_medias_top(hashtag, amount=15)
    for i in range(len(medias)):
        username = medias[i].dict()['user']['username']
        print(username)

        #save usernames into a file
        with open('target.csv', 'a', newline='') as file:
            writer = csv.writer(file)
            writer.writerow([username])
    return username

def main():

    #getting credentials
    credential = read_credentials('logins.csv')
    #logging in
    login(credential)

    #getting hashtags from user
    hashtag = input("please type hashtag  here...!! : ")
    # get_hashtags_users(hashtag)
    medias = cl.hashtag_medias_top('love', amount=15)
    print(medias)

    #logging out
    cl.logout()

if __name__ == "__main__":
    main()

Traceback

Traceback (most recent call last):
  File "C:\Users\hp\Desktop\instabot\test.py", line 74, in <module>
    main()
  File "C:\Users\hp\Desktop\instabot\test.py", line 67, in main
    medias = cl.hashtag_medias_top(hashtag, amount=15)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\anaconda3\envs\Instabot\Lib\site-packages\instagrapi\mixins\hashtag.py", line 386, in hashtag_medias_top    
    medias = self.hashtag_medias_top_a1(name, amount)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\anaconda3\envs\Instabot\Lib\site-packages\instagrapi\mixins\hashtag.py", line 349, in hashtag_medias_top_a1 
    return self.hashtag_medias_a1(name, amount, tab_key="top")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\anaconda3\envs\Instabot\Lib\site-packages\instagrapi\mixins\hashtag.py", line 222, in hashtag_medias_a1     
    medias, _ = self.hashtag_medias_a1_chunk(name, amount, tab_key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hp\anaconda3\envs\Instabot\Lib\site-packages\instagrapi\mixins\hashtag.py", line 179, in hashtag_medias_a1_chunk
    result = data["data"][tab_key]
             ~~~~^^^^^^^^
KeyError: 'data'

Expected behavior according to the documentation it should return a dict like this:

''' {'pk': 2574092718364154697, 'id': '2574092718364154697_376712420', 'code': 'CO5A7BxA9tJ', 'taken_at': datetime.datetime(2021, 5, 15, 10, 49, 45, tzinfo=datetime.timezone.utc), 'media_type': 1, 'product_type': '', 'thumbnail_url': HttpUrl('https://instagram.fhel3-1.fna.fbcdn.net/v/t51.2885-15/e35/s1080x1080/186430270_473573763896149_2030909827389015824_n.jpg?tp=1&_nc_ht=instagram.fhel3-1.fna.fbcdn.net&_nc_cat=101&_nc_ohc=4jFHY_INCnMAX-7fObK&edm=AP_V10EBAAAA&ccb=7-4&oh=9fb0c4cdb01a7aa376a96c0df366d844&oe=60C4C01A&_nc_sid=4f375e', scheme='https', host='instagram.fhel3-1.fna.fbcdn.net', tld='net', host_type='domain', path='/v/t51.2885-15/e35/s1080x1080/186430270_473573763896149_2030909827389015824_n.jpg', query='tp=1&_nc_ht=instagram.fhel3-1.fna.fbcdn.net&_nc_cat=101&_nc_ohc=4jFHY_INCnMAX-7fObK&edm=AP_V10EBAAAA&ccb=7-4&oh=9fb0c4cdb01a7aa376a96c0df366d844&oe=60C4C01A&_nc_sid=4f375e'), 'location': {'pk': 517543, 'name': 'Sestola', 'address': '', 'lng': 10.77328, 'lat': 44.2266, 'external_id': 103150459725396, 'external_id_source': 'facebook_places'}, 'user': {'pk': 376712420, 'username': 'vascobica', 'full_name': '⚡Vasco Bica®⚡', 'profile_pic_url': HttpUrl('https://scontent-hel3-1.cdninstagram.com/v/t51.2885-19/s150x150/96211403_922669918147090_5138958292701151232_n.jpg?tp=1&_nc_ht=scontent-hel3-1.cdninstagram.com&_nc_ohc=tYlGX8kDuSgAX9WtBRF&edm=AP_V10EBAAAA&ccb=7-4&oh=ac96c75846d17519e53923a0ddb3aad0&oe=60C51486&_nc_sid=4f375e', scheme='https', host='scontent-hel3-1.cdninstagram.com', tld='com', host_type='domain', path='/v/t51.2885-19/s150x150/96211403_922669918147090_5138958292701151232_n.jpg', query='tp=1&_nc_ht=scontent-hel3-1.cdninstagram.com&_nc_ohc=tYlGX8kDuSgAX9WtBRF&edm=AP_V10EBAAAA&ccb=7-4&oh=ac96c75846d17519e53923a0ddb3aad0&oe=60C51486&_nc_sid=4f375e'), 'stories': []}, 'comment_count': 8, 'like_count': 327, 'has_liked': None, 'caption_text': 'Ready to fight ⚔️\n#js7 \n.\n.\n#swissmountainsports #racing #coppaitaliadh \n#mirandabikeparts\xa0#burning\xa0#jumping \xa0#whipit\xa0#scrubit\xa0#enduro\xa0#mtblife\xa0 #downhill\xa0#mountainbiking\xa0#sliding\xa0#dirt\xa0#dh\xa0 #mtb\xa0#bike\xa0#bikelife\xa0#friends\xa0#mtbswitzerland\xa0#downhillmtb\xa0#valais\xa0 #swissmountains\xa0\xa0#italy #italydownhill', 'usertags': [{'user': {'pk': 3636959873, 'username': 'christopherstrm', 'full_name': 'Christopher Ström', 'profile_pic_url': HttpUrl('https://scontent-hel3-1.cdninstagram.com/v/t51.2885-19/s150x150/173775865_527371595096868_8991176723035066304_n.jpg?tp=1&_nc_ht=scontent-hel3-1.cdninstagram.com&_nc_ohc=tbsAzTDoLtEAX_HaT9Z&edm=AP_V10EBAAAA&ccb=7-4&oh=94a18b3b4d0d39d9dbda849b4c23a5a9&oe=60C5192F&_nc_sid=4f375e', scheme='https', host='scontent-hel3-1.cdninstagram.com', tld='com', host_type='domain', path='/v/t51.2885-19/s150x150/173775865_527371595096868_8991176723035066304_n.jpg', query='tp=1&_nc_ht=scontent-hel3-1.cdninstagram.com&_nc_ohc=tbsAzTDoLtEAX_HaT9Z&edm=AP_V10EBAAAA&ccb=7-4&oh=94a18b3b4d0d39d9dbda849b4c23a5a9&oe=60C5192F&_nc_sid=4f375e'), 'stories': []}, 'x': 0.211352657, 'y': 0.8478260870000001}], 'video_url': None, 'view_count': 0, 'video_duration': 0.0, 'title': '', 'resources': []}

'''

Screenshots image

Desktop (please complete the following information):

jarrodnorwell commented 11 months ago

@Dua-Jan-Muhammad issue is caused by the sessionid, when not injected into the public request it will return graphql and not data and the graphql data is not correct it seems (maybe query_hash?), unsure of how @adw0rd wants to go about fixing it but one fix would be to inject it all the time