-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (41 loc) · 1.06 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import opencc
from flask import Flask, request, jsonify, redirect
app = Flask(__name__)
app.config['RESTFUL_JSON'] = {
'ensure_ascii': False
}
SCHEMES = [
"s2t",
"t2s",
"s2tw",
"tw2s",
"s2hk",
"hk2s",
"s2twp",
"tw2sp",
"t2tw",
"t2hk",
]
@app.route('/<path:_type>', methods=['POST'])
def convert(_type):
if _type not in SCHEMES:
_type = "t2s"
try:
cc = opencc.OpenCC(f'{_type}.json')
title = cc.convert(request.form.get('title', ''))
content = cc.convert(request.form.get('content', ''))
return jsonify({
'title': title,
'content': content,
}), 200
except Exception as e:
app.logger.error(f"Error during conversion: {e}")
return jsonify({
'title': 'Error occurred',
'content': 'An unexpected error occurred.',
}), 500
@app.route('/', methods=['GET'])
def redirect_to_github():
return redirect('https://github.com/LandonLi/YA-OpenCC-API', code=301)
if __name__ == '__main__':
app.run(debug=False)