def get_comments_by_post_id(post_id: Union[str, int]) -> list[CommentModel]: """ Get all comments on the specified post. post_id is either base 10 (int) or base 36 (str) """ if isinstance(post_id, str): post_id = base36decode(post_id) return _comment_data.get_comments_by_post_id(post_id)
def get_post_by_id(post_id: Union[str, int]) -> Optional[PostModel]: """ Gets a single post from the database. post_id is either base 10 (int) or base 36 (str) """ if isinstance(post_id, str): post_id = reddit.base36decode(post_id) return _post_data.get_post_by_id(post_id)
def get_comment_by_id(comment_id: Union[str, int]) -> Optional[CommentModel]: """ Gets a single comment from the database. comment_id is either base 10 (int) or base 36 (str) """ if isinstance(comment_id, str): comment_id = base36decode(comment_id) return _comment_data.get_comment_by_id(comment_id)
def _create_comment_model(reddit_comment: Comment) -> CommentModel: """ Creates a model without inserting it into the database. """ comment = CommentModel() comment.set_id(reddit_comment.id) comment.score = reddit_comment.score comment.created_time = datetime.fromtimestamp(reddit_comment.created_utc, tz=timezone.utc) comment.body = reddit_comment.body comment.post_id = base36decode(reddit_comment.submission.id) if not reddit_comment.is_root: comment.parent_id = base36decode( reddit_comment.parent_id.split("t1_")[1]) # Comments by deleted users won't have an author, same for deleted comments. if reddit_comment.author is not None: comment.author = reddit_comment.author.name # edited is either a timestamp or False if it hasn't been edited. if reddit_comment.edited: comment.edited = datetime.fromtimestamp(reddit_comment.edited, tz=timezone.utc) # distinguished is a string (usually "moderator", maybe "admin"?) or None. comment.distinguished = True if reddit_comment.distinguished else False # No easy way to verify that a comment is deleted, but it's unlikely that a user would make a comment # with a body of "[deleted]" or "[removed]" *and* delete their account afterward. if reddit_comment.author is None and reddit_comment.body in ("[deleted]", "[removed]"): comment.deleted = True # Unlike with posts, removed is still true if the post has been removed and deleted. comment.removed = True if reddit_comment.removed else False return comment
def add_mod_action(reddit_mod_action: ModAction) -> ModActionModel: """ Parses some basic information for a mod action and adds it to the database. Assumes acting mod and target user/post/comment are already created if necessary, may raise an error on database integrity (foreign key relationship) if not. """ mod_action = ModActionModel() mod_action.id = reddit_mod_action.id.replace("ModAction_", "") mod_action.mod = reddit_mod_action.mod.name mod_action.action = reddit_mod_action.action mod_action.details = reddit_mod_action.details mod_action.description = reddit_mod_action.description mod_action.created_time = datetime.fromtimestamp( reddit_mod_action.created_utc, tz=timezone.utc) if reddit_mod_action.target_author: mod_action.target_user = reddit_mod_action.target_author # If the target is either a post or comment, target_permalink will be set and have the post id in it, # e.g. /r/anime/comments/kp906e/meta_thread_month_of_january_03_2021/ghvmptk/ # The extra "/comments/" check looks superfluous for now but is a safety measure. if reddit_mod_action.target_permalink and "/comments/" in reddit_mod_action.target_permalink: post_id = reddit_mod_action.target_permalink.split("/")[4] mod_action.target_post_id = base36decode(post_id) # Only need this section if comments are the target, posts should be handled by permalink section. if reddit_mod_action.target_fullname and reddit_mod_action.target_fullname.startswith( "t1_"): comment_id = reddit_mod_action.target_fullname.replace("t1_", "") mod_action.target_comment_id = base36decode(comment_id) new_mod_action = _mod_action_data.insert(mod_action, error_on_conflict=False) return new_mod_action
def migrate_snapshots(date, hour): conn = sqlite3.connect(DB_FILE) conn.row_factory = sqlite3.Row row = conn.execute("SELECT * FROM snapshots WHERE date=? and hour=?;", (date, hour)).fetchone() # No data, past the last recorded snapshot? if not row: return old_snapshot_psk = row["psk"] snapshot = SnapshotModel() snapshot.created_time = row["datetime"] snapshot.date = date snapshot.hour = hour snapshot.subscribers = row["subscribers"] new_snapshot = _snapshot_data.insert(snapshot) rows = conn.execute( "SELECT sf.*, p.id FROM snapshot_frontpage sf JOIN posts p on sf.post_psk = p.psk WHERE snapshot_psk=?;", (old_snapshot_psk, ), ).fetchall() conn.close() for row in rows: sfp_model = SnapshotFrontpageModel() sfp_model.post_id = base36decode(row["id"]) sfp_model.snapshot_id = new_snapshot.id sfp_model.rank = row["rank"] sfp_model.score = row["score"] _snapshot_data.insert(sfp_model)
def set_id(self, id_base36: str): """Sets both base 10 and base 36 forms of the id field.""" self.id = base36decode(id_base36) self.id36 = id_base36