MCPcopy Create free account
hub / github.com/19-84/redd-archiver / insert_comment

Method insert_comment

core/postgres_database.py:706–762  ·  view source on GitHub ↗

Insert a single comment into the database. Args: comment: Comment dictionary with Reddit JSON structure Returns: True if inserted successfully, False otherwise

(self, comment: dict[str, Any])

Source from the content-addressed store, hash-verified

704 return False
705
706 def insert_comment(self, comment: dict[str, Any]) -> bool:
707 """Insert a single comment into the database.
708
709 Args:
710 comment: Comment dictionary with Reddit JSON structure
711
712 Returns:
713 True if inserted successfully, False otherwise
714 """
715 try:
716 with self.pool.get_connection() as conn:
717 with conn.cursor() as cur:
718 # Extract parent_thread_id from permalink or link_id
719 parent_thread_id = None
720 if comment.get("permalink"):
721 with suppress(IndexError, AttributeError):
722 parent_thread_id = comment["permalink"].split("/")[4]
723
724 if not parent_thread_id and "link_id" in comment and comment["link_id"]:
725 with suppress(AttributeError, TypeError):
726 parent_thread_id = comment["link_id"].replace("t3_", "")
727
728 # Sanitize and prepare data
729 sanitized_comment = self._sanitize_recursive(comment)
730 json_data = json.dumps(sanitized_comment, allow_nan=False)
731
732 cur.execute(
733 """
734 INSERT INTO comments
735 (id, post_id, parent_id, author, created_utc, score, body,
736 permalink, subreddit, link_id, depth, json_data)
737 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
738 ON CONFLICT (id) DO UPDATE SET
739 score = EXCLUDED.score,
740 json_data = EXCLUDED.json_data
741 """,
742 (
743 comment.get("id", ""),
744 parent_thread_id or "",
745 comment.get("parent_id", ""),
746 comment.get("author", "[deleted]"),
747 int(self._sanitize_value(comment.get("created_utc", 0))),
748 int(self._sanitize_value(comment.get("score", 0))),
749 comment.get("body", ""),
750 comment.get("permalink", ""),
751 comment.get("subreddit", ""),
752 comment.get("link_id", ""),
753 int(self._sanitize_value(comment.get("depth", 0))),
754 json_data,
755 ),
756 )
757 conn.commit()
758 return True
759
760 except Exception as e:
761 print_error(f"Failed to insert comment {comment.get('id', 'unknown')}: {e}")
762 return False
763

Callers 1

insert_comments_batchMethod · 0.95

Calls 6

_sanitize_recursiveMethod · 0.95
_sanitize_valueMethod · 0.95
print_errorFunction · 0.90
get_connectionMethod · 0.80
executeMethod · 0.80
commitMethod · 0.80

Tested by

no test coverage detected