3.20 category和title一样则更新,否则插入
This commit is contained in:
parent
f940444428
commit
eab6599075
@ -34,9 +34,6 @@ class TypechoDirectMysqlPublisher:
|
|||||||
})
|
})
|
||||||
|
|
||||||
def __get_category_id(self, category_name):
|
def __get_category_id(self, category_name):
|
||||||
"""
|
|
||||||
从 self.__exist_categories 查找匹配的分类 ID
|
|
||||||
"""
|
|
||||||
for item in self.__exist_categories:
|
for item in self.__exist_categories:
|
||||||
if item['name'] == category_name:
|
if item['name'] == category_name:
|
||||||
return item['mid']
|
return item['mid']
|
||||||
@ -84,18 +81,17 @@ class TypechoDirectMysqlPublisher:
|
|||||||
|
|
||||||
def publish_post(self, title, content, category):
|
def publish_post(self, title, content, category):
|
||||||
"""
|
"""
|
||||||
如果同一分类下 (category) 已存在相同 title,则直接返回已存在的 cid;
|
如果 (category, title) 已存在 → 更新旧文章
|
||||||
否则插入新文章并返回新 cid。
|
否则 → 插入新文章
|
||||||
"""
|
"""
|
||||||
cursor = self.__db.cursor()
|
cursor = self.__db.cursor()
|
||||||
|
|
||||||
# 1. 获取分类 ID(不存在则插入)
|
# 1. 获取分类 ID(若不存在则新建)
|
||||||
mid = self.__get_category_id(category)
|
mid = self.__get_category_id(category)
|
||||||
if mid < 0:
|
if mid < 0:
|
||||||
mid = self.__add_category(category)
|
mid = self.__add_category(category)
|
||||||
|
|
||||||
# 2. 查重:同一分类下 (mid) 是否已存在相同 title
|
# 2. 查找同一分类下,是否已存在相同 title 的文章
|
||||||
# 通过连接 contents & relationships 表判断
|
|
||||||
check_sql = """
|
check_sql = """
|
||||||
SELECT c.cid
|
SELECT c.cid
|
||||||
FROM %s c
|
FROM %s c
|
||||||
@ -111,15 +107,33 @@ class TypechoDirectMysqlPublisher:
|
|||||||
)
|
)
|
||||||
cursor.execute(check_sql)
|
cursor.execute(check_sql)
|
||||||
exist_row = cursor.fetchone()
|
exist_row = cursor.fetchone()
|
||||||
if exist_row:
|
|
||||||
# 已有同标题文章,直接返回
|
|
||||||
print(f"[INFO] 发现同一分类下已存在相同标题: {title}, cid={exist_row[0]},跳过插入。")
|
|
||||||
return exist_row[0]
|
|
||||||
|
|
||||||
# 3. 插入新文章
|
|
||||||
now_time_int = int(time.time())
|
now_time_int = int(time.time())
|
||||||
content = '<!--markdown-->' + content
|
content = '<!--markdown-->' + content
|
||||||
|
|
||||||
|
if exist_row:
|
||||||
|
# ========== 执行更新逻辑 ==========
|
||||||
|
cid = exist_row[0]
|
||||||
|
update_sql = """
|
||||||
|
UPDATE %s
|
||||||
|
SET modified=%d,
|
||||||
|
text='%s'
|
||||||
|
WHERE cid=%d
|
||||||
|
""" % (
|
||||||
|
self.__contents_table_name,
|
||||||
|
now_time_int,
|
||||||
|
escape_string(content),
|
||||||
|
cid
|
||||||
|
)
|
||||||
|
cursor.execute(update_sql)
|
||||||
|
|
||||||
|
# 如果你需要修改 slug、authorId、status 等字段,可在这里加上
|
||||||
|
# 不需要改 relationships (分类关系) 和分类计数,因为 category 没变
|
||||||
|
|
||||||
|
print(f"[INFO] 更新文章成功: title={title}, cid={cid}, category={category}")
|
||||||
|
|
||||||
|
else:
|
||||||
|
# ========== 执行插入逻辑 ==========
|
||||||
insert_sql = (
|
insert_sql = (
|
||||||
"INSERT INTO %s "
|
"INSERT INTO %s "
|
||||||
"(`title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) "
|
"(`title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) "
|
||||||
@ -135,20 +149,20 @@ class TypechoDirectMysqlPublisher:
|
|||||||
cursor.execute(insert_sql)
|
cursor.execute(insert_sql)
|
||||||
cid = cursor.lastrowid
|
cid = cursor.lastrowid
|
||||||
|
|
||||||
# 4. 更新 slug = cid
|
# slug = cid
|
||||||
update_slug_sql = (
|
update_slug_sql = (
|
||||||
"UPDATE %s SET slug=%d WHERE cid=%d"
|
"UPDATE %s SET slug=%d WHERE cid=%d"
|
||||||
) % (self.__contents_table_name, cid, cid)
|
) % (self.__contents_table_name, cid, cid)
|
||||||
cursor.execute(update_slug_sql)
|
cursor.execute(update_slug_sql)
|
||||||
|
|
||||||
# 5. 建立文章与分类的关系
|
# 建立文章与分类的关系
|
||||||
self.__insert_relationship(cursor, cid, mid)
|
self.__insert_relationship(cursor, cid, mid)
|
||||||
|
|
||||||
# 6. 更新分类下文章数
|
# 分类下文章数 +1
|
||||||
self.__update_category_count(cursor, mid)
|
self.__update_category_count(cursor, mid)
|
||||||
|
|
||||||
# 7. 提交
|
|
||||||
self.__db.commit()
|
|
||||||
|
|
||||||
print(f"[INFO] 插入新文章成功: title={title}, cid={cid}, category={category}")
|
print(f"[INFO] 插入新文章成功: title={title}, cid={cid}, category={category}")
|
||||||
|
|
||||||
|
# 3. 提交事务
|
||||||
|
self.__db.commit()
|
||||||
return cid
|
return cid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user