Select a Subtopic
๐ Day 21: Instagram Bot - Advanced Automation & Database Integration ๐
Welcome to Day 21! ๐ Today, weโll move beyond basic Instagram automation and integrate a database to store important data like login details, user interactions, and stories viewed. Youโll also learn how to use environment variables securely and run your bot more efficiently.
โ What Youโll Learn Today:
- Using SQLite for local storage in your bot.
- Storing login credentials and stories viewed in a database.
- Securing your bot using environment variables.
- Making your bot more efficient by reducing redundant logins.
๐ท๏ธ 1. Using SQLite for Local Storage
Weโll use SQLite, a lightweight database, to store:
- Login credentials
- Profiles visited
- Stories viewed
โ Setting Up SQLite Database:
import sqlite3
# Connect to the SQLite database
conn = sqlite3.connect('instagram_bot.db')
cursor = conn.cursor()
# Create a table for login credentials
cursor.execute('''
CREATE TABLE IF NOT EXISTS credentials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL
)
''')
# Create a table for viewed stories
cursor.execute('''
CREATE TABLE IF NOT EXISTS viewed_stories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
profile_name TEXT NOT NULL,
story_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Commit and close
conn.commit()
conn.close()
๐ฅ๏ธ 2. Inserting Data into the Database
After setting up the database, letโs create a function to insert credentials and log viewed stories.
โ Updating database.py to Insert Data:
import sqlite3
# Function to add login credentials
def add_credentials(username, password):
conn = sqlite3.connect('instagram_bot.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO credentials (username, password) VALUES (?, ?)', (username, password))
conn.commit()
conn.close()
# Function to log viewed stories
def log_viewed_story(profile_name):
conn = sqlite3.connect('instagram_bot.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO viewed_stories (profile_name) VALUES (?)', (profile_name,))
conn.commit()
conn.close()
๐๏ธ 3. Securing Credentials with Environment Variables
Instead of hardcoding login details, store them in environment variables using dotenv.
โ Installing dotenv:
pip install python-dotenv
โ Using dotenv in Your Bot:
import os
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# Load environment variables
load_dotenv()
username = os.getenv('IG_USERNAME')
password = os.getenv('IG_PASSWORD')
# Function to view stories
def view_stories(profile):
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)
# Log the story viewed
from database import log_viewed_story
log_viewed_story(profile)
driver.quit()
๐ 4. Viewing the Database Contents
You can view your database contents using SQLite CLI or a tool like DB Browser for SQLite.
โ Viewing the Database via CLI:
sqlite3 instagram_bot.db
SELECT * FROM credentials;
SELECT * FROM viewed_stories;
๐ค 5. Efficient Login Handling
Instead of logging in every time, you can:
- Check if the bot is already logged in.
- Skip the login if already authenticated.
โ Checking Login Status:
# Function to check if the bot is already logged in
def is_logged_in(driver):
driver.get("https://www.instagram.com/")
time.sleep(3)
try:
driver.find_element(By.XPATH, '//span[@aria-label="Profile"]')
return True
except:
return False
๐ Bonus: Full Code with Database Integration
Hereโs the full code with story viewing, environment variables, and database logging:
import os
import time
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from database import log_viewed_story
# Load environment variables
load_dotenv()
username = os.getenv('IG_USERNAME')
password = os.getenv('IG_PASSWORD')
# Function to view stories
def view_stories(profile):
driver = webdriver.Chrome(executable_path="path/to/chromedriver")
driver.get("https://www.instagram.com/accounts/login/")
time.sleep(5)
# Check if logged in
if not is_logged_in(driver):
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)
# Log the story viewed
log_viewed_story(profile)
driver.quit()
# Function to check if the bot is already logged in
def is_logged_in(driver):
driver.get("https://www.instagram.com/")
time.sleep(3)
try:
driver.find_element(By.XPATH, '//span[@aria-label="Profile"]')
return True
except:
return False
# Run the bot
if __name__ == "__main__":
profile = input("Enter the profile to view stories: ")
view_stories(profile)
๐ฏ Day 21 Task:
- Set up a SQLite database for your bot.
- Store credentials and viewed stories in the database.
- Use environment variables to secure your credentials.
- Optimize the bot to skip login if already authenticated.