ping / instagram_private_api

A Python library to access Instagram's private API.
MIT License
2.95k stars 612 forks source link

How to get media_id of a user #310

Open Oyetomi opened 3 years ago

Oyetomi commented 3 years ago

Please follow the guide below


Before submitting an issue, make sure you have:

Which client are you using?


Describe your Question/Issue:

Hello Guys, I'm trying to get a users last post media_id, like this format 2431422900197522665_1985737254

Paste the output of python -V here: Python 3.8.5

Code:

# Example code that will produce the error reported
from instagram_web_api import Client

sources = ['wofaifada', 'timidakolo', 'auntyshaggi',] 
source = random.choice(sources)
  user_info = api.username_info(source)
  user_id = user_info['user']['pk']
  medias = api.user_feed(user_id,max_id=1)
  print(medias)
  media = medias[user["pk"]['media_id']]
  print(media)
Error/Debug Log:

```File "final.py", line 78, in follow
    media = medias[user["pk"]['media_id']]
NameError: global name 'user' is not defined
Oyetomi commented 3 years ago

@ping @jwtrhs @AndreaOrru please kindly help me out

rageshhub commented 3 years ago

You could take the shortcode and convert it into media id

Code:

def shortcode_to_media_id(shortcode):
    alp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    id = 0
    for i in range(0, len(shortcode)):
        char = shortcode[i]
        id = (id*64) + alp.index(char)
    return id