Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Set these as environment variables before starting the server:
| `LINKEDIN_CLIENT_ID` / `LINKEDIN_CLIENT_SECRET` | From your LinkedIn developer app — enables "Connect LinkedIn" and one-click posting |
| `BASE_URL` | Public URL of the site (e.g. `https://postpilot-nsks.onrender.com`), used for the OAuth redirect. Default: `http://localhost:5000` |
| `SECRET_KEY` | Random string for login sessions (generate one; without it, logins reset on every server restart) |
| `DATABASE_URL` | Postgres connection string. When set, the drafts library uses Postgres (persistent, survives deploys); without it, a local SQLite file is used. Libraries are per-user: drafts belong to the LinkedIn account connected when saving. |

PowerShell example:

Expand Down
120 changes: 79 additions & 41 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,86 @@ def _load_dotenv(path=".env"):
from datetime import timedelta
app.permanent_session_lifetime = timedelta(days=60)

# --- Drafts library (SQLite) ----------------------------------------------
# --- Drafts library (SQLite locally, Postgres when DATABASE_URL is set) ---
DB_PATH = os.path.join(BASE_DIR, "postpilot.db")
DATABASE_URL = os.getenv("DATABASE_URL", "")

if DATABASE_URL:
import psycopg2
from psycopg2.extras import RealDictCursor

def _db():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def db_execute(query, params=(), fetch=None):
query = query.replace("?", "%s")
conn = psycopg2.connect(DATABASE_URL)
try:
with conn:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
cur.execute(query, params)
if fetch == "all":
return cur.fetchall()
if fetch == "one":
return cur.fetchone()
finally:
conn.close()
else:
def db_execute(query, params=(), fetch=None):
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
try:
with conn:
cur = conn.execute(query, params)
if fetch == "all":
return cur.fetchall()
if fetch == "one":
return cur.fetchone()
finally:
conn.close()


def _init_db():
with _db() as c:
c.execute("""CREATE TABLE IF NOT EXISTS posts (
id TEXT PRIMARY KEY,
created TEXT,
text TEXT,
source_title TEXT, source_url TEXT, source_date TEXT,
image_src TEXT, image_source TEXT, image_license TEXT,
image_attribution TEXT, image_page TEXT,
status TEXT DEFAULT 'saved',
posted_at TEXT)""")
db_execute("""CREATE TABLE IF NOT EXISTS posts (
id TEXT PRIMARY KEY,
created TEXT,
text TEXT,
source_title TEXT, source_url TEXT, source_date TEXT,
image_src TEXT, image_source TEXT, image_license TEXT,
image_attribution TEXT, image_page TEXT,
status TEXT DEFAULT 'saved',
posted_at TEXT,
owner TEXT DEFAULT 'anon')""")
# Migrations for databases created before the owner column existed
try:
db_execute("ALTER TABLE posts ADD COLUMN owner TEXT DEFAULT 'anon'")
except Exception:
pass
try:
db_execute("UPDATE posts SET owner='anon' "
"WHERE owner IS NULL OR owner=''")
except Exception:
pass


_init_db()


def _save_draft(draft):
def _owner():
"""Identity for library ownership: LinkedIn member id, else shared anon."""
return session.get("li_sub") or "anon"


def _save_draft(draft, owner):
img = draft.get("image") or {}
with _db() as c:
c.execute(
"INSERT INTO posts (id, created, text, source_title, source_url, "
"source_date, image_src, image_source, image_license, "
"image_attribution, image_page, status) "
"VALUES (?,?,?,?,?,?,?,?,?,?,?,'saved')",
(draft["id"], draft["created"], draft["text"],
draft["source"].get("title", ""), draft["source"].get("url", ""),
draft["source"].get("date", ""),
img.get("src", ""), img.get("source", ""),
img.get("license", ""), img.get("attribution", ""),
img.get("page", "")))
db_execute(
"INSERT INTO posts (id, created, text, source_title, source_url, "
"source_date, image_src, image_source, image_license, "
"image_attribution, image_page, status, owner) "
"VALUES (?,?,?,?,?,?,?,?,?,?,?,'saved',?)",
(draft["id"], draft["created"], draft["text"],
draft["source"].get("title", ""), draft["source"].get("url", ""),
draft["source"].get("date", ""),
img.get("src", ""), img.get("source", ""),
img.get("license", ""), img.get("attribution", ""),
img.get("page", ""), owner))


def _row_to_draft(r):
Expand Down Expand Up @@ -381,25 +421,24 @@ def api_library_save():
d.setdefault("source", {})
d.setdefault("created",
datetime.now(timezone.utc).isoformat(timespec="seconds"))
try:
_save_draft(d)
except sqlite3.IntegrityError:
if db_execute("SELECT id FROM posts WHERE id=?", (d["id"],), fetch="one"):
return jsonify({"ok": True, "already_saved": True})
_save_draft(d, _owner())
return jsonify({"ok": True})


@app.get("/api/library")
def api_library():
with _db() as c:
rows = c.execute("SELECT * FROM posts ORDER BY created DESC").fetchall()
rows = db_execute("SELECT * FROM posts WHERE owner=? ORDER BY created DESC",
(_owner(),), fetch="all") or []
return jsonify({"drafts": [_row_to_draft(r) for r in rows]})


@app.post("/api/library/<pid>/delete")
def api_library_delete(pid):
with _db() as c:
row = c.execute("SELECT image_src FROM posts WHERE id=?", (pid,)).fetchone()
c.execute("DELETE FROM posts WHERE id=?", (pid,))
row = db_execute("SELECT image_src FROM posts WHERE id=? AND owner=?",
(pid, _owner()), fetch="one")
db_execute("DELETE FROM posts WHERE id=? AND owner=?", (pid, _owner()))
# Clean up the image file if it was a locally stored one
if row and (row["image_src"] or "").startswith("/images/"):
p = os.path.join(BASE_DIR, g.IMAGE_DIR, os.path.basename(row["image_src"]))
Expand Down Expand Up @@ -564,11 +603,10 @@ def linkedin_post():
draft_id = data.get("draft_id")
if draft_id:
try:
with _db() as c:
c.execute("UPDATE posts SET status='posted', posted_at=? "
"WHERE id=?",
(datetime.now(timezone.utc).isoformat(
timespec="seconds"), draft_id))
db_execute("UPDATE posts SET status='posted', posted_at=? "
"WHERE id=? AND owner=?",
(datetime.now(timezone.utc).isoformat(
timespec="seconds"), draft_id, _owner()))
except Exception as e:
print(f" [warn] could not mark draft posted ({e}).")
return jsonify({"ok": True, "post_id": post_id,
Expand Down
3 changes: 3 additions & 0 deletions library.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1>Your drafts</h1>
<span class="lib-count" id="lib-count" style="display:none"></span>
</div>
<p style="color:var(--muted); margin-bottom:20px;">Drafts you save from the generator live here. Post the keepers, delete the rest.</p>
<div id="lib-note" class="voice-box" style="margin-bottom:20px;">Connect LinkedIn to get a private library tied to your account — drafts saved while connected are only visible to you.</div>

<div class="lib-toolbar">
<input type="text" id="q" placeholder="Search drafts…">
Expand Down Expand Up @@ -83,6 +84,8 @@ <h1>Your drafts</h1>
liBtn.textContent = "Connect LinkedIn";
liBtn.onclick = () => { window.location.href = "/auth/linkedin"; };
}
const note = $("lib-note");
if (note) note.classList.toggle("visible", s.configured && !s.connected);
}).catch(() => {});
}
refreshLinkedIn();
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ gunicorn>=21.0
requests>=2.31
feedparser>=6.0
anthropic>=0.40
psycopg2-binary>=2.9
Loading