This commit is contained in:
2026-03-27 08:54:47 +08:00
parent 2920abbe17
commit 592be4e43e
5 changed files with 74 additions and 8 deletions
+48
View File
@@ -0,0 +1,48 @@
from rest_framework.decorators import permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
import requests
import json
import requests
import random
import json
from hashlib import md5
def make_md5(s, encoding='utf-8'):
return md5(s.encode(encoding)).hexdigest()
@permission_classes([AllowAny])
class BaiduFanyiView(APIView):
def post(self, request):
print(request.data)
# Set your own appid/appkey.
appid = '20220718001275847'
appkey = 'CJHmvf7qGs3szQy32cMg'
from_lang = 'en'
to_lang = 'zh'
endpoint = 'http://api.fanyi.baidu.com'
path = '/api/trans/vip/translate'
url = endpoint + path
query = request.data['q']
salt = random.randint(32768, 65536)
sign = make_md5(appid + query + str(salt) + appkey)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = {'appid': appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
r = requests.post(url, params=payload, headers=headers)
result = r.json()
return Response(result, status=status.HTTP_200_OK)