from datetime import datetime

class Post:
    def __init__(self, title, content, author):
        self.title = title
        self.content = content
        self.author = author
        self.created_at = datetime.now()
        self.likes = 0
        self.comments = []

    def like(self):
        self.likes += 1

    def add_comment(self, user, text):
        self.comments.append({"user": user, "text": text})

    def to_dict(self):
        # 转成字典，方便存 JSON / 传给前端
        return {
            "title": self.title,
            "author": self.author,
            "likes": self.likes,
            "comments": self.comments,
        }

    def __str__(self):
        return f"[{self.author}] {self.title} ({self.likes}❤)"

p = Post("有人一起跑步吗", "明天早上 6 点图书馆门口", "刘世强")
p.like()
p.add_comment("张三", "我来！")

print(p)
print(p.to_dict())