I use Python 3.11 and instagrapi package to upload photos and videos.
Here are the snippets of the method that I create to do the content upload (instagram.py):
def make_instagram_content(request) -> ResponseJSON:
try:
os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"
if(request['image_path'] == None or request['caption'] == None or request['media_type'] == None):
return {
"success": False,
"message": "Image path or caption is empty",
"data": None,
}
client: Client = login_user()
if(request['media_type'] == 'photo'):
media = client.photo_upload('./src/assets/images/square.jpg', 'test 101')
elif(request['media_type'] == 'video'):
media = client.video_upload('./src/assets/videos/golf.mp4', 'test 101')
else:
return {
"success": False,
"message": "Invalid media type",
"data": None,
}
print(media)
return {
"success": True,
"message": "Success post content to Instagram",
"data": None,
}
except Exception as e:
return {
"success": False,
"message": "Failed post content to Instagram: " + repr(e),
"data": None,
}
And here are my directory structure:
- src/
- assets/
- images/
- square.jpg
- videos
- golf.mp4
- services/
- instagram.py
When I run the script with request['media_type'] value is photo, it works properly (the photo being posted and the methods return {"success": True, ...}).
But when I do it with request['media_type'] value is video, it jumps to the catch exception and returns the error FileNotFoundError(2, 'No such file or directory')".
I even try to input the absolute path as the Path params in client.video_upload() method but it doesn't work either. How to fix this problem and input the proper file path in the client.video_upload()?
Notes:
-
I have to make sure that the file format is correct as mentioned in the documentation.

-
I have also tried using an absolute path but it doesn't work either. I have also tried to use the
Pathtype rather than the string one by doing this:absPath = os.path.abspath(f"./src/assets/videos/golf.mp4") pathType = Path(absPath) media = client.video_upload(path=pathType, caption='test 101') # media = client.video_upload(path=absPath, caption='test 101') # Tried this too
from How to handle relative paths doesn't work for video_upload method but work in photo_upload using instagrapi on Python
No comments:
Post a Comment