Skip to content

Commit

Permalink
add pinned to posts table
Browse files Browse the repository at this point in the history
  • Loading branch information
datawhores committed Apr 2, 2024
1 parent 8d2e9c5 commit 7f6454b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions ofscraper/db/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 24 additions & 3 deletions ofscraper/db/operations_/posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=?
Expand Down Expand Up @@ -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,
),
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7f6454b

Please sign in to comment.