Instagram with instapy_cli: how to fix an error

The module instapy_cly for instagram

I recently told you about instapy_cli, this module to send photoes, videos and stories to instagram with pyhton on the pc. As it is difficult to do it from the pc, for me this module is great for the job. To put some stuff about th blog on instagram it is perhaps the only solution, not being a good idea to use the camera right? At least, I don’t have any better idea about how to do it.

My post on istapy_cli

If you want to read the post I made about using instapy_cli go (Post on Instagram in no time) to this link here.

This time I want to talk about an issue about sendig video with it and how I found the solution on the github forum of the module. So go ahead and read.

Errors:

I was not able to send a video, and I had an error on the cosole like this:

  • Error when uploading video – “AttributeError: ‘NoneType’ object has no attribute ‘extension’”

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.

Where is media.py and what do I have to do exactly?

You may ask yourself… where I can find media.py?

  • 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 (that ends with lib) and do
  • os.startfile(…..) with the path in the parenthesys between apostrophes
  • press enter (the file browser opens) and go into packages sites
  • search istapy_cli folder
  • open media.py
  • add the import magic
  • change the lines of code into check_type method like this:

If you do not want to go into the file media.py, just substitute it with this. Make a backup copy of the original media.py file, just in case. Remember that you can always uninstall instapy_cli with

pip uninstall instapy_cli

and then reinstall it with

pip install instapy_cli

The code of media.py modified to avoid the issue when uploading a video is this:

import os, requests
from instagram_private_api import MediaRatios
from instagram_private_api_extensions import media as IGMedia
import filetype
import magic
#import urlparse for Python2 and Python3
try:
    from urllib.parse import urlparse
except ImportError:
    from urlparse import urlparse

MIN_ASPECT_RATIO = 0.80
MAX_ASPECT_RATIO = 1.91

class Media(object):
    media_path = None
    media = None
    media_ext = None
    isLink = False

    def __init__(self, file, ratio='post'):
        # if file is link
        if urlparse(file).scheme in ('http', 'https'):
            self.media_path = self.downloadMediaFromLink(file)
            self.isLink = True
        # if file is a local file
        else:
            self.media_path = file
        
        self.check_type()
    
    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]
    
    def is_image(self):
        return True if self.media_ext in ['jpg', 'png', 'gif'] else False
        
    def is_video(self):
        return True if self.media_ext in ['mp4', 'mov', 'avi'] else False

    def prepare(self, story=False):
        ratio = MediaRatios.reel if story else MediaRatios.standard
        size = (1080, 1920) if story else (1080, 1350)
        # print('ratio is: ', ratio)
        if self.is_image():
            return IGMedia.prepare_image(self.media_path, max_size=size, aspect_ratios=ratio)
        elif self.is_video():
            max_time = 15.0 if story else 60.0
            return IGMedia.prepare_video(self.media_path, max_size=size, aspect_ratios=ratio, max_duration=max_time)

    def isDownloaded(self):
        return self.isLink

    def downloadMediaFromLink(self,url):
        print('Downloading Media..')
        # print(urlparse(url))
        fileName = urlparse(url).path.split('/')[-1]
        # print(fileName)
        r = requests.get(url, allow_redirects=True)
        open(fileName, 'wb').write(r.content)
        return fileName

    def removeMedia(self):
        os.remove(self.media_path)

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.