https://www.xfyun.cn/doc/nlp/textRewriting/API.html
具體代碼如下:
# -*- coding:utf-8 -*-
from datetime import datetime
from wsgiref.handlers import format_date_time
from time import mktime
import hashlib
import base64
import hmac
from urllib.parse import urlencode
import json
import requests
'''
1捌浩、文本改寫 Web API 調(diào)用示例
2帖渠、運行前:請先填寫Appid辞友、APIKey滤灯、APISecret 相關信息
'''
class AssembleHeaderException(Exception):
def __init__(self, msg):
self.message = msg
class Url:
def __init__(this, host, path, schema):
this.host = host
this.path = path
this.schema = schema
pass
class wsParam(object):
def __init__(self):
self.APPID = APPID
self.APIKey = APIKey
self.APISecret = APISecret
self.url = 'https://api.xf-yun.com/v1/private/se3acbe7f'
self.level = level
def parse_url(self,requset_url):
stidx = requset_url.index("://")
host = requset_url[stidx + 3:]
schema = requset_url[:stidx + 3]
edidx = host.index("/")
if edidx <= 0:
raise AssembleHeaderException("invalid request url:" + requset_url)
path = host[edidx:]
host = host[:edidx]
u = Url(host, path, schema)
return u
def init_header(self):
headers = {
'content-type': "application/json",
'host': 'api.xf-yun.com'
}
return headers
def get_body(self):
data = {
"header": {
"app_id": self.APPID,
"status": 3,
},
"parameter": {
"se3acbe7f": {
"level": self.level,
"result": {
"encoding": "utf8",
"compress": "raw",
"format": "json"
}
}
},
"payload": {
"input1": {
"encoding": "utf8",
"compress": "raw",
"format": "plain",
"status": 3,
"text": str(base64.b64encode(text.encode('utf-8')), 'utf-8')
}
}
}
body = json.dumps(data)
return body
def assemble_ws_auth_url(requset_url, method="POST", api_key="", api_secret=""):
u = wsParam.parse_url(requset_url)
host = u.host
path = u.path
now = datetime.now()
date = format_date_time(mktime(now.timetuple()))
print(date)
# date = "Thu, 12 Dec 2019 01:57:27 GMT"
signature_origin = "host: {}\ndate: {}\n{} {} HTTP/1.1".format(host, date, method, path)
print("----2",signature_origin)
signature_sha = hmac.new(api_secret.encode('utf-8'), signature_origin.encode('utf-8'),
digestmod=hashlib.sha256).digest()
signature_sha = base64.b64encode(signature_sha).decode(encoding='utf-8')
authorization_origin = "api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"" % (
api_key, "hmac-sha256", "host date request-line", signature_sha)
print("----1:",authorization_origin)
authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
print(authorization_origin)
values = {
"host": host,
"date": date,
"authorization": authorization
}
return requset_url + "?" + urlencode(values)
def get_result():
request_url = assemble_ws_auth_url(wsParam.url, "POST", wsParam.APIKey, wsParam.APISecret)
print("request_url:", request_url)
response = requests.post(request_url, data=wsParam.get_body(), headers=wsParam.init_header())
print("response:", response)
str_result = response.content.decode('utf8')
json_result = json.loads(str_result)
print("response-content:", json_result)
if json_result. __contains__('header') and json_result['header']['code'] == 0:
renew_text = json_result['payload']['result']['text']
print("\n改寫結(jié)果:", str(base64.b64decode(renew_text), 'utf-8'))
#改寫結(jié)果保存到文件
#result_file = open(".\改寫結(jié)果.txt",'w',encoding='utf-8')
#result_file.write(str(base64.b64decode(renew_text), 'utf-8'))
if __name__ == "__main__":
APPID = " "
APISecret = " "
APIKey = " "
level = "<L6>" #改寫等級 <L1> ~ <L6> 等級越高点骑,改寫程度越深
text = "隨著我國城市化腳步的不斷加快综苔,園林工程建設的數(shù)量也在不斷上升,城市對于園林工程的質(zhì)量要求也隨之上升,然而就當前我國園" \
"林工程管理的實踐而言药磺,就園林工程質(zhì)量管理這一環(huán)節(jié)還存在許多不足之處,本文在探討園林工程質(zhì)量內(nèi)涵的基礎上煤伟,深入進行質(zhì)量" \
"管理策略探討癌佩,目的是保障我國園林工程施工質(zhì)量和提升整體發(fā)展效率木缝。"
# 上傳文件
#text = open(r"文本.txt",encoding='utf-8').read()
wsParam = wsParam()
get_result()
如何獲取這三個參數(shù)
1.打開控制臺
點擊相應功能進入的話,可以看到是否買了該功能
已經(jīng)買了該功能驼卖。
遇到問題時氨肌,需要好好瀏覽科大訊飛的說明書,出現(xiàn)的報錯代碼都有提示