Social Media Content Scheduler

Install Dependencies

pip install streamlit schedule selenium

Project Structure

social_scheduler/
├── scheduler.py
├── social_scheduler_app.py
├── database.db  (auto-created)


Streamlit App – social_scheduler_app.py


import streamlit as st
import sqlite3
import datetime

# === DB Setup ===
conn = sqlite3.connect('database.db', check_same_thread=False)
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS posts
             (id INTEGER PRIMARY KEY AUTOINCREMENT,
              platform TEXT, 
              content TEXT, 
              post_time TEXT)''')
conn.commit()

# === Functions ===
def add_post(platform, content, post_time):
    c.execute("INSERT INTO posts (platform, content, post_time) VALUES (?, ?, ?)", 
              (platform, content, post_time))
    conn.commit()

def get_all_posts():
    c.execute("SELECT * FROM posts ORDER BY post_time")
    return c.fetchall()

def delete_post(post_id):
    c.execute("DELETE FROM posts WHERE id = ?", (post_id,))
    conn.commit()

# === Streamlit UI ===
st.title("📅 Social Media Content Scheduler")

with st.form("post_form"):
    platform = st.selectbox("Platform", ["Twitter", "LinkedIn", "Facebook", "Instagram"])
    content = st.text_area("Post Content", max_chars=500)
    post_time = st.time_input("Post Time (24H)")
    submit = st.form_submit_button("➕ Schedule Post")

    if submit:
        post_datetime = datetime.datetime.combine(datetime.date.today(), post_time)
        add_post(platform, content, post_datetime.strftime("%Y-%m-%d %H:%M:%S"))
        st.success("✅ Post scheduled!")

st.subheader("📋 Scheduled Posts")
posts = get_all_posts()
for post in posts:
    st.markdown(f"**[{post[1]}]** {post[2]}  \n⏰ {post[3]}")
    if st.button(f"❌ Delete", key=post[0]):
        delete_post(post[0])
        st.experimental_rerun()

Background Scheduler – scheduler.py

import sqlite3
import schedule
import time
import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By

# === Dummy Post Logic ===
def post_to_platform(platform, content):
    print(f"🟢 Posting to {platform}: {content}")
    # You can replace below with Selenium logic to auto-login and post

# === Actual Scheduler ===
def check_and_post():
    conn = sqlite3.connect('database.db')
    c = conn.cursor()

    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    c.execute("SELECT * FROM posts WHERE post_time <= ?", (now,))
    posts_due = c.fetchall()

    for post in posts_due:
        post_to_platform(post[1], post[2])
        c.execute("DELETE FROM posts WHERE id = ?", (post[0],))

    conn.commit()
    conn.close()

# === Start Scheduler ===
schedule.every(1).minutes.do(check_and_post)

print("⏳ Scheduler running every minute...")
while True:
    schedule.run_pending()
    time.sleep(30)

How to Run It

  1. Start Streamlit App:

        streamlit run social_scheduler_app.py
   
   2. In another terminal, run the scheduler:

        python scheduler.py

No comments: