Select a Subtopic
๐ Day 20: Instagram Bot - Viewing Stories & Cloud Deployment ๐
Welcome to Day 20 of your journey! Today, weโll take your Instagram bot to the next level by automating story viewing and deploying your bot to the cloud so it runs 24/7! ๐
โ What Youโll Learn Today:
- Automating Instagram Story Viewing using Selenium.
- Deploying your Instagram bot to the cloud (Render or Railway).
- Setting up cron jobs to run your bot on a schedule.
- Using headless mode for running Selenium without a GUI.
๐ท๏ธ 1. Automating Instagram Story Viewing
Weโll build a function that automatically views stories from a user's profile.
โ Story Viewing Function:
from selenium import webdriver from selenium.webdriver.common.by import By import time # Function to view Instagram stories def view_stories(username, password, profile): # Set up the driver driver = webdriver.Chrome(executable_path="path/to/chromedriver") driver.get("https://www.instagram.com/accounts/login/") time.sleep(5) # Log in driver.find_element(By.NAME, "username").send_keys(username) driver.find_element(By.NAME, "password").send_keys(password) driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click() time.sleep(5) # Navigate to the profile driver.get(f"https://www.instagram.com/{profile}/") time.sleep(5) # Click the story icon try: story = driver.find_element(By.XPATH, '//div[contains(@class, "RR-M- h5uC0")]') story.click() print("Viewing stories...") time.sleep(10) # Wait to view the story except: print("No stories available.") driver.quit()
๐ฅ๏ธ 2. Deploying Your Bot to the Cloud
To run your bot 24/7, you need to deploy it to the cloud. Letโs use Railway or Render, which are free cloud platforms.
โ Steps to Deploy on Railway:
- Create a GitHub Repository for your project.
- Go to Railway and log in.
- Click on New Project > Deploy from GitHub Repo.
- Add Environment Variables (username, password, etc.).
- Click Deploy.
โ Steps to Deploy on Render:
- Go to Render.
- Create a New Web Service.
- Connect your GitHub Repo.
- Set the Start Command to run your bot:python main.py
- Click Deploy.
๐ค 3. Using Headless Mode for Cloud Deployment
Since cloud servers donโt have a GUI, you need to run Selenium in headless mode.
โ Modify Your Code for Headless Mode:
from selenium import webdriver from selenium.webdriver.chrome.options import Options # Set up headless mode options = Options() options.headless = True driver = webdriver.Chrome(executable_path="path/to/chromedriver", options=options)
๐ 4. Setting Up Cron Jobs (Automated Schedules)
You can set up a cron job to run your bot at specific intervals.
โ For Linux/Unix:
1. Open the crontab editor: crontab -e 2. Add a schedule to run your bot every day at 9 AM: 0 9 * * * /usr/bin/python3 /path/to/your/bot.py
โ For Windows:
Use Task Scheduler to set up a scheduled task.
๐ Bonus: Full Code (Including Story Viewer & Headless Mode)
Hereโs the complete code with story viewing, headless mode, and environment variables:
import os from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options import time # Get credentials from environment variables username = os.getenv("IG_USERNAME") password = os.getenv("IG_PASSWORD") # Function to view stories def view_stories(profile): # Headless mode setup options = Options() options.headless = True # Start driver driver = webdriver.Chrome(executable_path="path/to/chromedriver", options=options) driver.get("https://www.instagram.com/accounts/login/") time.sleep(5) # Log in driver.find_element(By.NAME, "username").send_keys(username) driver.find_element(By.NAME, "password").send_keys(password) driver.find_element(By.XPATH, '//*[@id="loginForm"]/div/div[3]/button').click() time.sleep(5) # Navigate to profile driver.get(f"https://www.instagram.com/{profile}/") time.sleep(5) # View stories try: story = driver.find_element(By.XPATH, '//div[contains(@class, "RR-M- h5uC0")]') story.click() print("Viewing stories...") time.sleep(10) except: print("No stories available.") driver.quit() # Run the bot if __name__ == "__main__": profile = input("Enter the profile to view stories: ") view_stories(profile)
๐ฏ Day 20 Task:
- Automate Instagram story viewing.
- Deploy your bot to the cloud (Railway/Render).
- Run your bot in headless mode.
- Set up cron jobs to schedule your bot.
Would you like me to help you with Day 21, where weโll add more advanced automation features and explore database integration? ๐