How to Created a Python Meme Bot for share memes on Instagram

How to Created a Python Meme Bot for share memes on Instagram

Do you use Instagram?

Do you want to know how to build an Instagram Bot? 1 jbiAqAM6a1IX5ZA9eJKlTQ.png So, some of my friends told me that most of the memes are uploaded to Reddit and later spread to Instagram, and Reddit was the best place to go for funny content but I was not a big fan of the Reddit UI and did not really get used to it.

One day I randomly thought, what if I could start a meme page that gets content from Reddit but without downloading and uploading the images manually and still be the first account to upload those memes on Instagram straight from Reddit?

And, I Built a BOT which uploads memes from some subreddits like r/dankmemes and r/memes and more, uploads it on my Instagram account - ameme.bot

Required libraries for making one (Bot)

pip install RedDownloader
pip install instagrapi

An Instagram account, which you will use to run the bot script. let's create .py file and import this important libraries

from RedDownloader import RedDownloader
from instagrapi import Client
import time
import os
import random

So, create first function getting Images from Reddit

A very easy to use library to download videos from reddit with sound. The library can also be used to download pictures and even picture galleries all just with a reddit post link. You can access Reddit API methods without having your own bot with classes such as DownloadBySubreddit more on that below.

NOTE: this package only downloads media from posts with images/video directly uploaded to reddit and not from sources like imgur or youtube / vimeo which are posted to Reddit.

# meme channels 
def MemeChannel():
    channels = ['dankmeme', 'meme', 'Programmingmemes', 'memes', 'hindimemes']
    return random.choice(channels)

Channels = MemeChannel()

# downloading post from reddit 
def GetMeme():
    post = RedDownloader.DownloadBySubreddit(f'{Channels}', 1, quality=720, output='meme', SortBy='new')
    author = post.GetPostAuthors()[0]
    return author

now write login code

# sign in to instagram 
clientig = Client()
clientig.login('ameme.bot', 'addpassword'))

Generate Caption for post so create generate function

# posting the post for uploading
def GenerateCap():
    captions = [
        '#meme #memes #funny #instagood #memer #shitpost',
        'add more hashtags'
    ]
    return random.choice(captions)

this is optional but you can add some stylish banner for you program. get more arts

print('''
───▄▄▄
─▄▀░▄░▀▄
─█░█▄▀░█
─█░▀▄▄▀█▄█▄▀
▄▄█▄▄▄▄███▀   InstaBot v1
''')

and create Clean function to delete downloaded post

def CleanUp():
    try:
        os.remove('meme/meme1.jpeg')
    except:
        pass

Final Code

option = input('Start Bot y/n : ').lower() or 'n'

if option=='y':
    # upload the post 
    while True:
        author = GetMeme()
        hashtags = GenerateCap()
        caption = f'Credit to : {author} \n {hashtags}'
        try:
            if not 'meme\meme1.jpeg':
                clientig.photo_upload('meme/meme1.mp4', caption)
            else:
                clientig.photo_upload('meme/meme1.jpeg', caption)
        except Exception as e:
            print(e)
        finally:
            print('Posted')

        print('Done...')
        CleanUp()
        time.sleep(20*60)
elif option=='n':
    exit()
else:
    exit()

Here is the complete Code - Download

img copyright by realpython

Good luck with making your own. If you have any Queries or Suggestions, please reach out to me in the Comments Section below.

Did you find this article valuable?

Support Amit Gajare by becoming a sponsor. Any amount is appreciated!