Skip to content

Commit

Permalink
新增 提交诗歌。
Browse files Browse the repository at this point in the history
  • Loading branch information
yeying-xingchen committed May 11, 2024
1 parent d678cda commit f7a5b70
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
30 changes: 20 additions & 10 deletions poetry_moment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@
def index():
return "欢迎使用诗歌时刻!"

@main.route('/text/')
def text():
log.info(f"请求IP: {request.remote_addr} 请求内容: 文本")
result = core.get()
return result.get('content', 0), 200
@main.route('/get/<poetry_type>')
def get_poetry(poetry_type):
if poetry_type=='text':
log.info(f"请求IP: {request.remote_addr} 请求内容: 文本")
result = core.get()
return result.get('content', 0), 200
if poetry_type=='json':
log.info(f"请求IP: {request.remote_addr} 请求内容: JSON")
response_data = core.get()
return jsonify(response_data), 200
return "请求类型错误", 404

@main.route('/json/', methods=['GET'])
def json_endpoint():
log.info(f"请求IP: {request.remote_addr} 请求内容: JSON")
response_data = core.get()
return jsonify(response_data), 200
@main.route('/post')
def post_poetry():
log.info(f"请求IP: {request.remote_addr} 请求内容: 提交")
content = request.args.get('content', None)
category = request.args.get('category', 'default')
user = request.args.get('user', '雷锋')
if content is None:
return "无内容!", 404
return jsonify(core.post(content, category, user)), 200
14 changes: 0 additions & 14 deletions poetry_moment/config.py

This file was deleted.

15 changes: 8 additions & 7 deletions poetry_moment/core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# 导入所需模块
from . import config
from apicat import config
import os
import json
import random
import time

def get(path):
# 使用config模块获取诗歌数据所在的目录
directory = config.get_path(path)
plugin_name = 'poetry_moment'
directory = config.get_plugin_cfg(plugin_name)

def get():
# 获取该目录下所有文件的列表
json_files = [file for file in os.listdir(directory) if file.endswith('.json')]
# 随机选择一个语录文件
Expand All @@ -31,7 +32,7 @@ def get(path):
return final_data


def post(path, category, content, user):
def post(content, category, user):
# 创建待写入的新语录记录字典
new_entry = {
"content": content,
Expand All @@ -40,10 +41,10 @@ def post(path, category, content, user):
"date": time.strftime("%Y-%m-%d", time.localtime())
}
# 获取用于存储语录的目标目录
discourse_directory = config.get_config_path(path)
directory = config.get_plugin_cfg(plugin_name)
new_filename = f"{category}.json"
# 写入新语录
with open(os.path.join(discourse_directory, new_filename), 'w', encoding='utf-8') as file:
with open(os.path.join(directory, new_filename), 'w+', encoding='utf-8') as file:
json.dump(new_entry, file, ensure_ascii=False, indent=4)
# 返回新创建语录的详细信息
poetry_data = {
Expand Down

0 comments on commit f7a5b70

Please sign in to comment.