def send_sms_to_admin(self, phone, name, subject_name): """发送单个验证码""" request = CommonRequest() request.set_accept_format('json') request.set_domain('dysmsapi.aliyuncs.com') request.set_method('POST') request.set_protocol_type('https') # https | http request.set_version('2017-05-25') request.set_action_name('SendSms') request.add_query_param('TemplateCode', AliYunSMS.SMS_TEMPLATE_ID_ORDER_NOTICE_ADMIN) request.add_query_param('SignName', AliYunSMS.SMS_TEMPLATE_NAME_ORDER_NOTICE_ADMIN) request.add_query_param('PhoneNumbers', phone) request.add_query_param( 'TemplateParam', json.dumps({ "name": name, "subject_name": subject_name })) request.add_body_params('OutId', uuid.uuid1()) response = self.client.do_action(request) # b'{"Message":"OK","RequestId":"97FDD87D-5B50-4BFA-BF9C-CA24768A5FE0","BizId":"419112860178747080^0","Code":"OK"}' return json.loads(response.decode('utf-8'))
def ListFace(groupName: "需要查询的库"): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_version("2018-12-03") request.set_action_name("ListFace") request.add_body_params("Group", groupName) request.set_accept_format('json') # 设置返回的结果 response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def detect_face_by_content(image_content): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_action_name("DetectFace") request.set_version("2018-12-03") request.set_accept_format('json') request.add_body_params("Content", imageContent) response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def GetFaceAttribute(imageUrl_1: "待检测图片URL"): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_action_name("GetFaceAttribute") request.set_version("2018-12-03") request.set_accept_format('json') # 设置返回的结果 request.add_body_params("ImageUrl", imageUrl_1) response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def VerifyFace(imageUrl_1: "待对比的图片URL_1", imageUrl_2: "待对比的图片URL_2"): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_action_name("VerifyFace") request.set_version("2018-12-03") request.set_accept_format('json') request.add_body_params("ImageUrl1", imageUrl_1) request.add_body_params("ImageUrl2", imageUrl_2) response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def test_rpc_post_with_init(self): request = CommonRequest(domain='ft.aliyuncs.com', version='2016-01-01', action_name='TestRpcApi') request.set_method('POST') request.add_query_param('QueryParam', 'queryValue') request.add_body_params('BodyParam', 'bodyValue') body = self.acs_client.do_action_with_exception(request) assert body response = json.loads(body) assert response assert response.get("Params").get("QueryParam") == 'queryValue' assert response.get("Params").get("BodyParam") == 'bodyValue'
def 提交任务(self, 文件链接): # 提交录音文件识别请求 postRequest = CommonRequest() postRequest.set_domain(self.DOMAIN) postRequest.set_version(self.API_VERSION) postRequest.set_product(self.PRODUCT) postRequest.set_action_name(self.POST_REQUEST_ACTION) postRequest.set_method('POST') # 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置 # 设置是否输出词信息,默认为false,开启时需要设置version为4.0 task = { self.KEY_APP_KEY: self.appKey, self.KEY_FILE_LINK: 文件链接, self.KEY_VERSION: "4.0", self.KEY_ENABLE_WORDS: True, 'max_single_segment_time': 10000 } # 开启智能分轨,如果开启智能分轨 task中设置KEY_AUTO_SPLIT : True # task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True} task = json.dumps(task) # print(task) postRequest.add_body_params(self.KEY_TASK, task) 任务回执 = "" try: postResponse = self.client.do_action_with_exception(postRequest) postResponse = json.loads(postResponse) print(f'postResponse: {postResponse}') statusText = postResponse[self.KEY_STATUS_TEXT] if statusText == self.STATUS_SUCCESS: self.任务回执 = postResponse[self.KEY_TASK_ID] print( f'录音文件识别请求成功响应!\n statusText: {statusText}\n keyTaskId: {self.任务回执}' ) return True elif statusText == 'USER_BIZDURATION_QUOTA_EXCEED': print(f'你今天的阿里云识别额度已用完!\n statusText: {statusText}') return False else: print( f'录音文件识别请求失败,失败原因是:{statusText},你可以将这个代码复制,到 “https://help.aliyun.com/document_detail/90727.html” 查询具体原因\n' ) return False except Exception as e: print(f'错误信息:\n {e}') return False
def test_roa_form_with_init(self): request = CommonRequest(domain='ft.aliyuncs.com', version='2016-01-02', action_name='TestRoaApi', uri_pattern='/web/cloudapi') request.set_method('POST') request.add_header('HeaderParam', 'headerValue') request.add_query_param('QueryParam', 'queryValue') request.add_body_params('BodyParam', 'bodyValue') body = self.acs_client.do_action_with_exception(request) assert body response = json.loads(body) assert response assert response.get("Params").get("QueryParam") == 'queryValue' assert response.get("Headers").get("HeaderParam") == 'headerValue' assert response.get("Params").get("BodyParam") == 'bodyValue' assert response.get("Headers").get("x-sdk-invoke-type") == 'common'
def test_bug_with_body_params(self): # body_params request = CommonRequest() request.set_domain("filetrans.cn-shanghai.aliyuncs.com") request.set_version("2018-08-17") request.set_product("nls-filetrans") request.set_action_name("SubmitTask") request.set_method('POST') app_key = 'qVwEQ6wIZ9Pxb36t' file_link = 'https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/' \ 'examples/nls-sample-16k.wav' task = {"app_key": app_key, "file_link": file_link} task = json.dumps(task) request.add_body_params("Task", task) response = self.client.do_action_with_exception(request) response = self.get_dict_response(response) self.assertTrue(response.get("RequestId"))
def test_resolve_endpoint(self): region_provider.modify_point('Ft', 'cn-hangzhou', 'ft.aliyuncs.com') request = CommonRequest() request.set_product('Ft') request.set_version('2016-01-02') request.set_action_name('TestRoaApi') request.set_uri_pattern('/web/cloudapi') request.set_method('POST') request.add_header('HeaderParam', 'headerValue') request.add_query_param('QueryParam', 'queryValue') request.add_body_params('BodyParam', 'bodyValue') body = self.acs_client.do_action_with_exception(request) assert body response = json.loads(body) assert response assert response.get("Params").get("QueryParam") == 'queryValue' assert response.get("Headers").get("HeaderParam") == 'headerValue' assert response.get("Params").get("BodyParam") == 'bodyValue'
def AddFace(groupName: "添加人脸的分组", person: "添加人脸的姓名", image: "添加人脸的编号", imageUrl: "添加人脸图片的URL"): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_version("2018-12-03") request.set_action_name("AddFace") request.add_body_params("Group", groupName) request.add_body_params("Person", person) request.add_body_params("Image", image) request.set_accept_format('json') # 设置返回的结果 request.add_body_params("ImageUrl", imageUrl) # request.putBodyParameter("Content", "/9j/4AAQSkZJRgABA..."); //检测图片的内容,Base64编码 response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def DeleteFace(groupName: "添加人脸的分组", person: "添加人脸的姓名", image: "添加人脸的编号"): request = CommonRequest() request.set_method("POST") request.set_domain("face.cn-shanghai.aliyuncs.com") request.set_version("2018-12-03") request.set_action_name("DeleteFace") request.add_body_params("Group", groupName) request.add_body_params("Person", person) request.add_body_params("Image", image) request.set_accept_format('json') # 设置返回的结果 response = client.get_response(request) response_str = str(response[2], 'utf-8') # bytes 转 string print(response_str) # 打印输出结果
def test_common_request(self): r = CommonRequest() # accept format self.assertIsNone(r.get_accept_format()) r.set_accept_format('json') self.assertEqual(r.get_accept_format(), "json") # action name self.assertEqual(r.get_action_name(), None) r.set_action_name('new action name') self.assertEqual(r.get_action_name(), "new action name") # body params self.assertDictEqual(r.get_body_params(), {}) r.set_body_params({'key': 'value'}) self.assertDictEqual(r.get_body_params(), {'key': 'value'}) r.add_body_params("key2", "value2") self.assertDictEqual(r.get_body_params(), { 'key': 'value', 'key2': 'value2' }) # content self.assertIsNone(r.get_content()) r.set_content("content") self.assertEqual(r.get_content(), "content") # headers self.assertDictEqual(r.get_headers(), {'x-sdk-invoke-type': 'common'}) r.set_headers({}) self.assertDictEqual(r.get_headers(), {}) r.add_header("key", "value") self.assertDictEqual(r.get_headers(), {"key": "value"}) # location endpoint type self.assertEqual(r.get_location_endpoint_type(), 'openAPI') # no set_location_endpoint_type ?? # location_service_code self.assertEqual(r.get_location_service_code(), None) r.set_location_service_code('new service code') self.assertEqual(r.get_location_service_code(), 'new service code') # method self.assertEqual(r.get_method(), 'GET') r.set_method('POST') self.assertEqual(r.get_method(), 'POST') # product self.assertEqual(r.get_product(), None) r.set_product('new-product') self.assertEqual(r.get_product(), 'new-product') # protocol_type self.assertEqual(r.get_protocol_type(), "http") r.set_protocol_type('https') self.assertEqual(r.get_protocol_type(), "https") # query params self.assertDictEqual(r.get_query_params(), {}) r.set_query_params({'key': 'value'}) self.assertDictEqual(r.get_query_params(), {'key': 'value'}) r.add_query_param("key2", "value2") self.assertDictEqual(r.get_query_params(), { 'key': 'value', "key2": "value2" }) # uri params self.assertEqual(r.get_uri_params(), None) r.set_uri_params({'user': "******"}) self.assertDictEqual(r.get_uri_params(), {'user': '******'}) # uri pattern self.assertEqual(r.get_uri_pattern(), None) r.set_uri_pattern('/users/:userid') self.assertEqual(r.get_uri_pattern(), '/users/:userid') # url # version self.assertEqual(r.get_version(), None) r.set_version('2014-10-18') self.assertEqual(r.get_version(), "2014-10-18") # user-agent self.assertEqual(r.get_headers().get('User-Agent'), None) r.set_user_agent("user-agent") self.assertEqual(r.get_headers().get('User-Agent'), "user-agent") # content-type self.assertEqual(r.get_headers().get('Content-Type'), None) r.set_content_type("application/json") self.assertEqual(r.get_headers().get('Content-Type'), "application/json") # domain self.assertEqual(r.get_domain(), None) r.set_domain("new-domain") self.assertEqual(r.get_domain(), "new-domain") # path_params self.assertEqual(r.get_path_params(), None) r.add_path_param("userid", "jacksontian") r.add_path_param("repo", "snow") self.assertEqual(r.get_path_params(), { "userid": "jacksontian", "repo": "snow" }) r.set_path_params({"userid": "linatian"}) self.assertEqual(r.get_path_params(), {"userid": "linatian"})
def video_to_txt(video_path, audio_path, txt_path): ''' 提取视频中的语音,识别出文字后保存到文本文件。 思路: 1. 提取视频中的音频 2. 把音频转为云服务需要的格式:16K采样率 :param video_path: 视频文件地址 :param audio_path: 提取的音频文件地址 :param txt_path: 文本保存路径 ''' audio_path = video_to_mono(video_path, audio_path) file_url = upload_file(audio_path) print(file_url) # 构造请求,并设置参数 post_req = CommonRequest() post_req.set_domain('filetrans.cn-shanghai.aliyuncs.com') post_req.set_version('2018-08-17') post_req.set_product('nls-filetrans') post_req.set_action_name('SubmitTask') post_req.set_method('POST') task = { 'appkey': APP_KEY, 'file_link': file_url, 'version': '4.0', 'enable_words': False } post_req.add_body_params('Task', json.dumps(task)) task_id = '' try: post_res = client.do_action_with_exception(post_req) post_res = json.loads(post_res) status_txt = post_res['StatusText'] if status_txt == 'SUCCESS': task_id = post_res['TaskId'] print(f'录音文件识别请求成功响应,task_id: {task_id}') else: print(f'录音文件识别请求失败: {status_txt}') return except Exception as e: print(e) return get_req = CommonRequest() get_req.set_domain('filetrans.cn-shanghai.aliyuncs.com') get_req.set_version('2018-08-17') get_req.set_product('nls-filetrans') get_req.set_action_name('GetTaskResult') get_req.set_method('GET') get_req.add_query_param('TaskId', task_id) status_txt = '' while True: try: get_res = client.do_action_with_exception(get_req) get_res = json.loads(get_res) status_txt = get_res['StatusText'] if status_txt in ['RUNNING', 'QUEUEING']: time.sleep(10) else: break except Exception as e: print(e) return # print(get_res) if status_txt == 'SUCCESS': result = get_res["Result"] sentences = result["Sentences"] txt_list = [s['Text'] for s in sentences] with open(txt_path, 'a') as f: f.writelines('\n'.join(txt_list)) else: print(f'录音文件识别失败, {status_txt}') return
def fileTrans(akId, akSecret, appKey, fileLink, savepath): REGION_ID = "cn-shanghai" PRODUCT = "nls-filetrans" DOMAIN = "filetrans.cn-shanghai.aliyuncs.com" API_VERSION = "2018-08-17" POST_REQUEST_ACTION = "SubmitTask" GET_REQUEST_ACTION = "GetTaskResult" KEY_APP_KEY = "app_key" KEY_FILE_LINK = "file_link" KEY_TASK = "Task" KEY_TASK_ID = "TaskId" KEY_STATUS_TEXT = "StatusText" # 创建AcsClient实例 client = AcsClient(akId, akSecret, REGION_ID) # 创建提交录音文件识别请求,并设置请求参数 postRequest = CommonRequest() postRequest.set_domain(DOMAIN) postRequest.set_version(API_VERSION) postRequest.set_product(PRODUCT) postRequest.set_action_name(POST_REQUEST_ACTION) postRequest.set_method('POST') task = {KEY_APP_KEY: appKey, KEY_FILE_LINK: fileLink} task = json.dumps(task) postRequest.add_body_params(KEY_TASK, task) try: # 提交录音文件识别请求,处理服务端返回的响应 postResponse = client.do_action_with_exception(postRequest) postResponse = json.loads(postResponse) print(postResponse) # 获取录音文件识别请求任务的ID,以供识别结果查询使用 taskId = "" statusText = postResponse[KEY_STATUS_TEXT] if statusText == "SUCCESS": print("录音文件识别请求成功响应!") taskId = postResponse[KEY_TASK_ID] else: print("录音文件识别请求失败!") return except ServerException as e: print(e) except ClientException as e: print(e) # 创建识别结果查询请求,设置查询参数为任务ID getRequest = CommonRequest() getRequest.set_domain(DOMAIN) getRequest.set_version(API_VERSION) getRequest.set_product(PRODUCT) getRequest.set_action_name(GET_REQUEST_ACTION) getRequest.set_method('GET') getRequest.add_query_param(KEY_TASK_ID, taskId) # 提交录音文件识别结果查询请求 # 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述符为"SUCCESS"、"SUCCESS_WITH_NO_VALID_FRAGMENT", # 或者为错误描述,则结束轮询。 statusText = "" while True: try: getResponse = client.do_action_with_exception(getRequest) getResponse = json.loads(getResponse) print(getResponse) statusText = getResponse[KEY_STATUS_TEXT] if statusText == "RUNNING" or statusText == "QUEUEING": # 继续轮询 time.sleep(3) else: # 退出轮询 # print(len(statusText)) break except ServerException as e: print(e) # print('HERE') except ClientException as e: print(e) # print('THERE') if statusText == "SUCCESS" or statusText == "SUCCESS_WITH_NO_VALID_FRAGMENT": print("录音文件识别成功!") numpy.save(savepath, getResponse) else: print("录音文件识别失败!") exit() return
def fileTrans(akId, akSecret, appKey, fileLink): # 地域ID,常量内容,请勿改变 REGION_ID = "cn-shanghai" PRODUCT = "nls-filetrans" DOMAIN = "filetrans.cn-shanghai.aliyuncs.com" API_VERSION = "2018-08-17" POST_REQUEST_ACTION = "SubmitTask" GET_REQUEST_ACTION = "GetTaskResult" # 请求参数key KEY_APP_KEY = "appkey" KEY_FILE_LINK = "file_link" KEY_VERSION = "version" KEY_ENABLE_WORDS = "enable_words" # 是否开启智能分轨 KEY_AUTO_SPLIT = "auto_split" # 响应参数key KEY_TASK = "Task" KEY_TASK_ID = "TaskId" KEY_STATUS_TEXT = "StatusText" KEY_RESULT = "Result" # 状态值 STATUS_SUCCESS = "SUCCESS" STATUS_RUNNING = "RUNNING" STATUS_QUEUEING = "QUEUEING" # 创建AcsClient实例 client = AcsClient(akId, akSecret, REGION_ID) # 提交录音文件识别请求 postRequest = CommonRequest() postRequest.set_domain(DOMAIN) postRequest.set_version(API_VERSION) postRequest.set_product(PRODUCT) postRequest.set_action_name(POST_REQUEST_ACTION) postRequest.set_method('POST') # 新接入请使用4.0版本,已接入(默认2.0)如需维持现状,请注释掉该参数设置 # 设置是否输出词信息,默认为false,开启时需要设置version为4.0 task = { KEY_APP_KEY: appKey, KEY_FILE_LINK: fileLink, KEY_VERSION: "4.0", KEY_ENABLE_WORDS: False } # 开启智能分轨,如果开启智能分轨 task中设置KEY_AUTO_SPLIT : True # task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True} task = json.dumps(task) print(task) postRequest.add_body_params(KEY_TASK, task) taskId = "" try: postResponse = client.do_action_with_exception(postRequest) postResponse = json.loads(postResponse) print(postResponse) statusText = postResponse[KEY_STATUS_TEXT] if statusText == STATUS_SUCCESS: print("录音文件识别请求成功响应!") taskId = postResponse[KEY_TASK_ID] else: print("录音文件识别请求失败!") return except ServerException as e: print(e) except ClientException as e: print(e) # 创建CommonRequest,设置任务ID getRequest = CommonRequest() getRequest.set_domain(DOMAIN) getRequest.set_version(API_VERSION) getRequest.set_product(PRODUCT) getRequest.set_action_name(GET_REQUEST_ACTION) getRequest.set_method('GET') getRequest.add_query_param(KEY_TASK_ID, taskId) # 提交录音文件识别结果查询请求 # 以轮询的方式进行识别结果的查询,直到服务端返回的状态描述符为"SUCCESS"、"SUCCESS_WITH_NO_VALID_FRAGMENT", # 或者为错误描述,则结束轮询。 statusText = "" while True: try: getResponse = client.do_action_with_exception(getRequest) getResponse = json.loads(getResponse) print(getResponse) statusText = getResponse[KEY_STATUS_TEXT] if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING: # 继续轮询 time.sleep(10) else: # 退出轮询 break except ServerException as e: print(e) except ClientException as e: print(e) if statusText == STATUS_SUCCESS: print("录音文件识别成功!") print(getResponse['Result']) else: print("录音文件识别失败!") return
def fileTrans(akId, akSecret, appKey, fileLink) : # The constant parameters, such as the region ID. Do not modify their values. REGION_ID = "ap-southeast-1" PRODUCT = "nls-filetrans" DOMAIN = "filetrans.ap-southeast-1.aliyuncs.com" API_VERSION = "2019-08-23" POST_REQUEST_ACTION = "SubmitTask" GET_REQUEST_ACTION = "GetTaskResult" # The keys of request parameters. KEY_APP_KEY = "appkey" KEY_FILE_LINK = "file_link" KEY_VERSION = "version" KEY_ENABLE_WORDS = "enable_words" # The key that specifies whether to enable automatic track splitting. KEY_AUTO_SPLIT = "auto_split" # The keys of response parameters. KEY_TASK = "Task" KEY_TASK_ID = "TaskId" KEY_STATUS_TEXT = "StatusText" KEY_RESULT = "Result" # The status values. STATUS_SUCCESS = "SUCCESS" STATUS_RUNNING = "RUNNING" STATUS_QUEUEING = "QUEUEING" # Create an AcsClient instance. client = AcsClient(akId, akSecret, REGION_ID) # Create and send a recording file recognition request. postRequest = CommonRequest() postRequest.set_domain(DOMAIN) postRequest.set_version(API_VERSION) postRequest.set_product(PRODUCT) postRequest.set_action_name(POST_REQUEST_ACTION) postRequest.set_method('POST') # Specify the version of the recording file recognition service. If you are a new user, set this parameter to 4.0. If you use the default version 2.0, comment out this parameter. # Specify whether to return the recognition results of words. Default value: false. This parameter takes effect only when the version of the recording file recognition service is 4.0. task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False} # Specify whether to enable automatic track splitting. You can set the KEY_AUTO_SPLIT parameter to True to enable automatic track splitting. # task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True} task = json.dumps(task) print(task) postRequest.add_body_params(KEY_TASK, task) taskId = "" try : postResponse = client.do_action_with_exception(postRequest) postResponse = json.loads(postResponse) print (postResponse) statusText = postResponse[KEY_STATUS_TEXT] if statusText == STATUS_SUCCESS : print ("The recording file recognition request is successful.") taskId = postResponse[KEY_TASK_ID] else : print ("The recording file recognition request fails.") return except ServerException as e: print (e) except ClientException as e: print (e) # Create a CommonRequest object and specify the task ID. getRequest = CommonRequest() getRequest.set_domain(DOMAIN) getRequest.set_version(API_VERSION) getRequest.set_product(PRODUCT) getRequest.set_action_name(GET_REQUEST_ACTION) getRequest.set_method('GET') getRequest.add_query_param(KEY_TASK_ID, taskId) # Send the query request for the recording file recognition result. # Use the polling method to query the recognition result. The polling runs until the status message that the server returns is SUCCESS, SUCCESS_WITH_NO_VALID_FRAGMENT, # or an error message. statusText = "" while True : try : getResponse = client.do_action_with_exception(getRequest) getResponse = json.loads(getResponse) print (getResponse) statusText = getResponse[KEY_STATUS_TEXT] if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING : # Continue the polling. time.sleep(10) else : # End the polling. break except ServerException as e: print (e) except ClientException as e: print (e) if statusText == STATUS_SUCCESS : print ("The recording file is recognized.") else : print ("Failed to recognize the recording file.") return