feat:增加博客处理模块
This commit is contained in:
BIN
utils/__pycache__/blog_utils.cpython-310.pyc
Normal file
BIN
utils/__pycache__/blog_utils.cpython-310.pyc
Normal file
Binary file not shown.
BIN
utils/__pycache__/common.cpython-310.pyc
Normal file
BIN
utils/__pycache__/common.cpython-310.pyc
Normal file
Binary file not shown.
@@ -2,6 +2,56 @@ from fastapi import Request
|
||||
|
||||
from models.blog import BlogVisit
|
||||
|
||||
# 博客概要字数
|
||||
BLOG_SUMMARY_COUNT = 100
|
||||
|
||||
# 阅读速度 600 字/分钟
|
||||
READ_SPEED = 600.0
|
||||
|
||||
# 博客非统计字符
|
||||
EXCLUDE_CHARS = [' ', '\n', '\t']
|
||||
|
||||
|
||||
def get_word_count(text: str) -> int:
|
||||
"""
|
||||
获取文本字数
|
||||
|
||||
:param text: 文本
|
||||
:return: 字数
|
||||
"""
|
||||
count = 0
|
||||
|
||||
if not text: # 检查None或空字符串
|
||||
return count
|
||||
|
||||
for char in text:
|
||||
if char not in EXCLUDE_CHARS:
|
||||
count += 1
|
||||
|
||||
return count
|
||||
|
||||
|
||||
def get_read_duration(word_count: int) -> float:
|
||||
"""
|
||||
获取阅读时长 单位:分钟
|
||||
|
||||
:param word_count: 文本字数
|
||||
:return: 阅读时长
|
||||
"""
|
||||
return word_count / READ_SPEED
|
||||
|
||||
|
||||
def get_blog_summary(content: str) -> str:
|
||||
"""
|
||||
获取简要信息
|
||||
|
||||
:param content: 博客内容
|
||||
:return: 简要信息
|
||||
"""
|
||||
if len(content) > BLOG_SUMMARY_COUNT:
|
||||
return content[:BLOG_SUMMARY_COUNT]
|
||||
return content
|
||||
|
||||
|
||||
def get_client_ip(request):
|
||||
"""从请求中获取客户端IP地址"""
|
||||
|
||||
Reference in New Issue
Block a user