From 7f6454b8f81ec7f628556427aa0a4786dcf5d164 Mon Sep 17 00:00:00 2001 From: datawhores Date: Tue, 2 Apr 2024 15:38:45 -0500 Subject: [PATCH] add pinned to posts table --- ofscraper/db/operations.py | 4 ++++ ofscraper/db/operations_/posts.py | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ofscraper/db/operations.py b/ofscraper/db/operations.py index 6a5c1cc2f..e84cecb25 100644 --- a/ofscraper/db/operations.py +++ b/ofscraper/db/operations.py @@ -67,6 +67,7 @@ def create_backup_transition(model_id, username): "media_hash", "media_model_id", "posts_model_id", + "posts_pinned" "products_model_id", "other_model_id", "stories_model_id", @@ -105,6 +106,9 @@ async def add_column_tables(model_id=None, username=None): if not "media_posted_at" in changes: await add_column_media_posted_at(model_id=model_id, username=username) await add_flag_schema("media_posted_at", model_id=model_id, username=username) + if not "posts_pinned" in changes: + await add_column_posts_pinned(model_id=model_id, username=username) + await add_flag_schema("posts_pinned", model_id=model_id, username=username) if not "posts_model_id" in changes: await add_column_post_ID(model_id=model_id, username=username) await add_flag_schema("posts_model_id", model_id=model_id, username=username) diff --git a/ofscraper/db/operations_/posts.py b/ofscraper/db/operations_/posts.py index 452febe90..8c37b065a 100644 --- a/ofscraper/db/operations_/posts.py +++ b/ofscraper/db/operations_/posts.py @@ -39,7 +39,7 @@ ) """ postInsert = """INSERT INTO 'posts'( -post_id, text,price,paid,archived,created_at,model_id) +post_id, text,price,paid,archived,pinned,created_at,model_id) VALUES (?, ?,?,?,?,?,?);""" postUpdate = """UPDATE posts SET text = ?, price = ?, paid = ?, archived = ?, created_at = ?, model_id=? @@ -78,6 +78,7 @@ def write_post_table(posts: list, model_id=None, username=None, conn=None): data.price, data.paid, data.archived, + data.pinned, data.date, model_id, ), @@ -93,7 +94,7 @@ def write_post_table_transition( inputData: list, model_id=None, username=None, conn=None ): with contextlib.closing(conn.cursor()) as cur: - ordered_keys = ('post_id', 'text', 'price', 'paid', 'archived', 'created_at',"model_id") + ordered_keys = ('post_id', 'text', 'price', 'paid', 'archived', "pinned",'created_at',"model_id") insertData = [tuple([data[key] for key in ordered_keys]) for data in inputData] cur.executemany(postInsert, insertData) conn.commit() @@ -148,7 +149,8 @@ def get_all_post_ids(model_id=None, username=None, conn=None) -> list: def get_all_posts_transition(model_id=None, username=None, conn=None) -> list: with contextlib.closing(conn.cursor()) as cur: cur.execute(postsALLTransition) - return [dict(row) for row in cur.fetchall()] + data=[dict(row) for row in cur.fetchall()] + return [dict(row,pinned=row.get("pinned")) for row in data] @wrapper.operation_wrapper_async @@ -176,6 +178,25 @@ def add_column_post_ID(conn=None, **kwargs): raise e # Rollback in case of errors +@wrapper.operation_wrapper_async +def add_column_post_pinned(conn=None, **kwargs): + with contextlib.closing(conn.cursor()) as cur: + try: + # Check if column exists (separate statement) + cur.execute("SELECT CASE WHEN EXISTS (SELECT 1 FROM PRAGMA_TABLE_INFO('posts') WHERE name = 'pinned') THEN 1 ELSE 0 END AS alter_required;") + alter_required = cur.fetchone()[0] # Fetch the result (0 or 1) + + # Add column if necessary (conditional execution) + if alter_required == 0: + cur.execute("ALTER TABLE posts ADD COLUMN pinned INTEGER;") + # Commit changes + conn.commit() + + except sqlite3.Error as e: + conn.rollback() + raise e # Rollback in case of errors + + @wrapper.operation_wrapper_async def get_archived_postinfo(model_id=None, username=None, conn=None, **kwargs) -> list: with contextlib.closing(conn.cursor()) as cur: