Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
1
Indexable
Never
#pip installInstagramAPI

from InstagramAPI import InstagramAPI

username = 'YOUR_USERNAME'
password = 'YOUR_PASSWORD'

api = InstagramAPI(username, password)
api.login()

def post_to_instagram(caption, media_path):
    api.uploadPhoto(media_path, caption=caption)
    response = api.LastJson
    if response.get('status') == 'ok':
        post_id = response['media']['id']
        print('Successfully posted to Instagram. Post ID:', post_id)
        return post_id
    else:
        print('Failed to post to Instagram.')
        return None

def fetch_post_likes_comments(post_id):
    api.getMediaComments(post_id)
    response = api.LastJson
    if response.get('status') == 'ok':
        likes = response['comments']
        comments = response['comments']
        return likes, comments
    else:
        print('Failed to fetch post likes and comments.')
        return None, None


post_caption = 'Hello, this is my automated post!'
post_media_path = 'PATH_TO_MEDIA_FILE'

post_id = post_to_instagram(post_caption, post_media_path)

if post_id:
    post_likes, post_comments = fetch_post_likes_comments(post_id)
    if post_likes is not None:
        print('Likes:', len(post_likes))
    if post_comments is not None:
        print('Comments:', len(post_comments))