HTML

    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:

    1. Create a GitHub Repository for your project.
    2. Go to Railway and log in.
    3. Click on New Project > Deploy from GitHub Repo.
    4. Add Environment Variables (username, password, etc.).
    5. Click Deploy.

    โœ… Steps to Deploy on Render:

    1. Go to Render.
    2. Create a New Web Service.
    3. Connect your GitHub Repo.
    4. Set the Start Command to run your bot:
      python main.py
    5. 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? ๐Ÿ˜Š