Skip to content

A python based project to automate Instagram picture and reel creation and posting for a motivational quotes page

Notifications You must be signed in to change notification settings

Pinaki-Jha/instagram-automation

Repository files navigation

COMPLETE INSTAGRAM AUTOMATION

Note from the developer

Hey all, this is a project I've been working on for a while now, way before I started using a git repository for it (Yeah, as a new developer, I forgot I could do that.) This repository might be made public in the future, and if you're reading this, that has probably already happened. Either that, or you've hacked my account. As I said before, I'm still a fairly new developer (in fact, this is my first time writing a proper README file!) so this app is not going to be perfect. Origanally, and maybe even currently, this was made for my personal use only, and the project is quite confusing even for me at times. Well, that's all. Here's to hoping that you don't have a stroke reading through the code.

P.S. If you're going to use this code anywhere, specially for your own personal instagram theme page, I strongly request you to atleast credit my instagram on your profile. My instagram : _______________________ (will add that here later lmao)

That's all from my end. Have a great day!

Version Updates --- Current Version (V 0.1.)


Version 0.1.

  • Added function to scrape quotes from Goodreads using requests and bs4
  • Added function to refine the quotes
  • Added function to create images of motivational quotes using PIL
  • Added master function for instagram quote-author post generations
  • Added function to generate instagram posts
  • Added function to post pictures to Instagram using Playwright and PyAutoIt
  • Added function to generate background images for reels from picfinder.ai using Playwright
  • Added function to generate audio for reels using ElevenLabs API
  • Added function to create a video reel of a motivational quote
  • Added master function for instagram quote-author reek generation
  • Added function to generate instagram reels

Future Versions To-do list

  • Clean up the code
  • Add randomness to the page selection
  • Add url shifting once all pages for current url exhausted
  • Add url shifting reset counter when all urls exhausted
  • Finish creating the functionality to post reels using Playwright
  • Work on a mechanism for liking posts
  • Work on a mechanism for following-unfollowing automatically
  • Work on a mechanism for scraping top hashtags from the top posts in the niche
  • Work on implementing a loop that runs 24/7 and performs all actions at specified times
  • Consider using the meta suite with Playwright automation to schedule content in advance
  • Shift the code towards a more object-oriented structure

GOAL

My goal with this app was complete automation of an instagram page. That means I do NOTHING once I've run the code, except for minor changes, maybe. What this means, in turn, is that the content for my instagram page, be it reels or posts, are all generated automatically, without any effort from me, posted automatically to instagram, and, if possible, I'd also like to automate liking, commenting, following and unfollowing.

The restrictions this places on us is that the content we post must be simple enough to be generated by a few lines of python code (ahem, ahem) Taking the motivational niche into mind, I decided to create a page that posts motivational content. I might change the code later, or create a new project (I'll link it here in the future if I do) for more general theme page purposes, but that's what this project is, for now. If you've built one by refactoring the code I've written, please do share, and I'll be glad to share the link here.

FUNCTIONALITY

Version 0.1. Functionality

  1. Motivational Quote Picture Creation
  2. Motivational Quote Video Reel Creation
  3. Posting the picture to instagram
  4. Posting the reel to instagram (still work in progress).

DEVELOPER RANTING AND HIGH LEVEL OVERVIEW OF FUNCTIONALITY

Version 0.1.

  1. Post Creation : Within the limited scope of this project, I had to create pictures of motivational quotes using python. The program automatically generates a new, unique instagram post everytime. It does so by webscraping motivational quotes from the Goodreads website using the requests and bs4 libraries. It then uses the Pillow library to create the actual picture that will be posted, by placing the contents of the quotes and authors onto a predesigned image. Quotes that are too long for the image to look good are rejected, and the remaining quotes are refined my removing unnecessary characters and adding newlline characters after a certain number of words.

    It will, eventually, run out of content to create, but that's a problem for future me.

    How the uniqueness of each post is implemented is another tricky solve. I might explain these functionalities better in a separate section that explains each function, but basically all of the data is stored in csv files for now. I'm considering using an SQL or MongoDB database for the same, but that's going to be later. Essentially, I store a page_list csv file with a list of three elements in each value. These are basically the starting page and the ending page, as well as a boolean value. I then iterate through these values until I find a page that hasn't been used before, scrape the contents off of it, and store it into csv files for the data of quotes and authors. I also create a csv file "posted" with boolean values to check if a certain quote has been posted to instagram or not. When all the quotes have been posted, I move on to a new list of pages.

    When all the pages have been exhausted? That will be a long time from now, since one page on Goodreadscontains about 30 quotes, and there. I scrape 4 pages at once, which is 120 quotes, and I'm planning on posting two quotes a day. Do the calculations.

    Still, it is a problem, and I'm planning to fix that by resetting all the values to false when that happens. Atleast that's the temporary fix for now. A better fix would be to move on to a new url (from motivational quotes to, say, inspirational quotes), but that wouldn't be really automated if I have to do it manually. I could create a list for some base urls to use, since goodreads uses the same structure for all of it's quote pages, but even that would be exhausted after some time. Once all these links have been exhausted, I loop back to the first link. Since it would exhaust after a really, REALLY long time, this seems like an acceptable fix for now, and I will try to implement that by the next version.

  2. Posting a picture to Instagram: This was another minor headache for my tiny newbie developer brain. I was initially planning on using Selenium to implement thisautomation. However, when I started developing this project, Selenium was having some issues with the new Chrome updates. I wanted to get started on the project, and Selenium was getting confusing for me, with the much anticipated Selenium 4.11.0 update and the new methods of getting a Chrome webdriver from the chromium website, and absolutely NO YOUTUBE TUTORIALS explaining it at the time, save one or two 5 hour long videos. So, frustrated, I turned to alternatives. I explored a few other alternatives, such as Cypress and Puppeteer/Pyppeteer, but I ultimately settled for Playwright - and I absolutely love it.

    The next problem I encountered was in the form of a native dialog. You see, when you want to post a picture to instagram, the webpage opens up a native dialog for you to upload the pictures to. Playwright cannot handle native dialogs, however, and so, everything seemed to crash. After spending hours banging my head on the screen of my laptop, however, I was introduced to another amazing package - The PyAutoIt module. This module specifically handled any native dialogs and made it possible to post pictures to Instagram. However, the native dialog wouldn't run and the code would fail if the playwright file was run headless, hence, I had to set headless to False for all of it to work.

    I'm further considering automating the Meta Suite's Scheduler or something similar, so that I only have to run the code say, once per week. Future upadates might explore more of this.

  3. Reel Creation: Within the limited scope of this project to stick to creating content around motivational quotes only, the primary focus was to create reels of motivational quotes. Fetching the quotes was done pretty much exactly how it was performed for the creation of posts. The next steps were creating a background image, creating the audio for the reel, as well as putting it all together into a reel.

    For the audio generation, I tried using the pyttsx3 module first. However, I felt like the voice was too monotonous, and it wouldn't go well with the type of motivational content that I wanted to provide. Next, I tried out the gTTS module, which actually performed way better, but gave up on it primarily because it lacked the option for a male voice, as far as I am aware. This page was to be focused to motivate men to do better, so a male voice was necessary. In other applications, using gTTS would've been fine, and even the preferred choice, to an extent.

    Not getting what I needed from these libraries, however, I decided to get my hands dirty, and use the ElevenLabs (yay, AI, sparkles) module with their API. And just like that, the audio generation was done with. A few adjustments had to be made to get the AI to vocalise the quotes correctly, but it was all easy work.

    Now, it was time to create a background for our reels. Initially, I tried using the OpenAI API to create pictures. However, the free plan of the same offered limited functionality, and I'm broke. So I looked for an alternative.

    Ultimately, I ended up using playwright to get to an AI image generation website (picfinder.ai) to generate images. This had its own challenges. At random, sometimes, Picfinder would take forever to generate an image. And by that, I mean FOREVER. I've waited for five hours for an image to load, staring at the screen, and cried when it worked after I cancelled execution and ran the program again.

    My first solution to this was to use another website to generate images altogether. I was familiar with (tensor.art) for image generation, but I had to login via google to use it, and google blocked any attempts to login in the new automated testing chrome environment, because the "browser wasn't safe". Yeah, right. So, I had to stick to picfinder for now. The other solution I came up with involved setting a while loop on a timer to search for the image, and that's what this program currently uses. Basically, if the website takes more that 3 minutes to generate the image, the function just returns a string. If the image is generated, it returns the src link of the image.

    Now, finally, I used MoviePY along with Imagemagick to overlay the text onto the image and add the audio. In future updates, I will be adding some background music to make the video reel a bit more interesting, as well as a promotional second clip to "follow the page for more such content", perhaps.

  4. Reel Posting: Work in Progress.

About

A python based project to automate Instagram picture and reel creation and posting for a motivational quotes page

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages