Post on instagram from the Computer with Python in no time

It is annoying not having the chance to use the computer to post on instagram, right? So, here is the solution in Python. Let’s see how to:

  • post a foto
  • post a video
  • post a story

This is done with instapy_cli, this vey good module to use instagram through python, avoiding having to open it and post manually. I found this very useful, so I think others will too.

How to install instapy_cli

pip install instapy-cli

Post an image on instagram with python

from instapy_cli import client

username = "username"
password = "**********"
image = 'glitch.png'
text = 'Flask for Python' + '\r\n' + '#glitch #python #gif https://pythonprogramming.altervista.org/publish-app-or-blog-with-glitch-com-and-python-in-no-time/'
with client(username, password) as cli:
    cli.upload(image, text)

The github repository of instapy_cli

instapy-cli instagram-private-api
instapy_cli module

There are many modules about instagram. I tried this and it’s very good, so I post here the link to the repository on github to follow its evolution:

https://github.com/instagrambot/instapy-cli

Examples

Here it’s the folder with the examples_

https://github.com/instagrambot/instapy-cli/tree/master/examples

Upload a video

from instapy_cli import client

username = 'USERNAME'
password = 'PASSWORD'
video = 'docs/video-sample-upload.mp4'
text = 'This will be the caption of your video.' + '\r\n' + 'You can also use hashtags! #hash #tag #now'

with client(username, password) as cli:
    cli.upload(video, text)

Upload a photo story

from instapy_cli import client

username = 'USERNAME'
password = 'PASSWORD'
image = 'docs/image-story-sample.jpg'

with client(username, password) as cli:
    cli.upload(image, story=True)

Upload photo stories

import time
from instapy_cli import client

username = 'USERNAME'
password = 'PASSWORD'
image = 'docs/image-story-sample.jpg'

image_files = ['docs/image-story-sample.jpg', 'docs/image-story-2.jpg', 'docs/image-story-3.jpg', 'docs/image-story-4.jpg']

with client(username, password) as cli:
    for i in range(0, len(image_files)):
        print('image: ', image_files[i])
        res = cli.upload(image_files[i], story=True)
        print('IG: >> ', res)
        time.sleep(1)

Errors:

I was not able to send a video, so I searched in the issues forum of instapy_cli and I found this solution:

go in the media.py file (go in packages-sites folder of the installation of python*) and write

import magic

If you do nnot have magic

pip install python-magic-bin

Then, always in media.py change the code in the check_type method (32 line or something like that) and comment the line that is there and add the two lines you see below:

    def check_type(self):
        #self.media_ext = filetype.guess(self.media_path).extension
        self.media_ext = magic.from_file(self.media_path,mime=True)
        self.media_ext=  self.media_ext.split('/')[1]

I found the solution at this link here

This worked for me.

  • to find the folder packages-sites, go in the cmp and open python
  • then import sys
  • import os
  • digit sys.path
  • copy one of the path and do
  • os.startfile(…..) with the path in the parenthesys between apostrophes
  • press enter go into the Lib folder and then into packages sites
  • search istapy_cli folder
  • open media.py
  • do what I told you above

If you want to go further in the detail of this go to this post I made recently.

Utilities

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.