RASRsdk.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding:utf-8 -*-
  2. import urllib.request
  3. import hmac
  4. import hashlib
  5. import base64
  6. import time
  7. import random
  8. import os
  9. import json
  10. def formatSignString(param):
  11. signstr = "POSTaai.qcloud.com/asr/v1/"
  12. for t in param:
  13. if 'appid' in t:
  14. signstr += str(t[1])
  15. break
  16. signstr += "?"
  17. for x in param:
  18. tmp = x
  19. if 'appid' in x:
  20. continue
  21. for t in tmp:
  22. signstr += str(t)
  23. signstr += "="
  24. signstr = signstr[:-1]
  25. signstr += "&"
  26. signstr = signstr[:-1]
  27. # print 'signstr',signstr
  28. return signstr
  29. def sign(signstr, secret_key):
  30. sign_bytes= bytes(signstr , 'utf-8')
  31. secret_bytes = bytes(secret_key, 'utf-8')
  32. hmacstr = hmac.new(secret_bytes, sign_bytes, hashlib.sha1).digest()
  33. s = base64.b64encode(hmacstr).decode('utf-8')
  34. return s
  35. def randstr(n):
  36. seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  37. sa = []
  38. for i in range(n):
  39. sa.append(random.choice(seed))
  40. salt = ''.join(sa)
  41. # print salt
  42. return salt
  43. def sendVoice(secret_key, secretid, appid, engine_model_type, res_type, result_text_format, voice_format, filepath, cutlength, template_name=""):
  44. if len(str(secret_key)) == 0:
  45. print('secretKey can not empty')
  46. return
  47. if len(str(secretid)) == 0:
  48. print('secretid can not empty')
  49. return
  50. if len(str(appid)) == 0:
  51. print('appid can not empty')
  52. return
  53. if len(str(engine_model_type)) == 0 or (
  54. str(engine_model_type) != '8k_0' and str(engine_model_type) != '16k_0' and str(
  55. engine_model_type) != '16k_en'):
  56. print('engine_model_type is not right')
  57. return
  58. if len(str(res_type)) == 0 or (str(res_type) != '0' and str(res_type) != '1'):
  59. print('res_type is not right')
  60. return
  61. if len(str(result_text_format)) == 0 or (str(result_text_format) != '0' and str(result_text_format) != '1' and str(
  62. result_text_format) != '2' and str(result_text_format) != '3'):
  63. print('result_text_format is not right')
  64. return
  65. if len(str(voice_format)) == 0 or (
  66. str(voice_format) != '1' and str(voice_format) != '4' and str(voice_format) != '6'):
  67. print('voice_format is not right')
  68. return
  69. if len(str(filepath)) == 0:
  70. print('filepath can not empty')
  71. return
  72. if len(str(cutlength)) == 0 or str(cutlength).isdigit() == False or cutlength > 200000:
  73. print('cutlength can not empty')
  74. return
  75. # secret_key = "oaYWFO70LGDmcpfwo8uF1IInayysGtgZ"
  76. query_arr = dict()
  77. query_arr['appid'] = appid
  78. query_arr['projectid'] = 1013976
  79. if len(template_name) > 0:
  80. query_arr['template_name'] = template_name
  81. query_arr['sub_service_type'] = 1
  82. query_arr['engine_model_type'] = engine_model_type
  83. query_arr['res_type'] = res_type
  84. query_arr['result_text_format'] = result_text_format
  85. query_arr['voice_id'] = randstr(16)
  86. query_arr['timeout'] = 100
  87. query_arr['source'] = 0
  88. query_arr['secretid'] = secretid
  89. query_arr['timestamp'] = str(int(time.time()))
  90. query_arr['expired'] = int(time.time()) + 24 * 60 * 60
  91. query_arr['nonce'] = query_arr['timestamp'][0:4]
  92. query_arr['voice_format'] = voice_format
  93. file_object = open(filepath, 'rb')
  94. file_object.seek(0, os.SEEK_END)
  95. datalen = file_object.tell()
  96. file_object.seek(0, os.SEEK_SET)
  97. seq = 0
  98. response = []
  99. while (datalen > 0):
  100. end = 0
  101. if (datalen < cutlength):
  102. end = 1
  103. query_arr['end'] = end
  104. query_arr['seq'] = seq
  105. query = sorted(query_arr.items(), key=lambda d: d[0])
  106. signstr = formatSignString(query)
  107. autho = sign(signstr, secret_key)
  108. if (datalen < cutlength):
  109. content = file_object.read(datalen)
  110. else:
  111. content = file_object.read(cutlength)
  112. seq = seq + 1
  113. datalen = datalen - cutlength
  114. headers = dict()
  115. headers['Authorization'] = autho
  116. headers['Content-Length'] = len(content)
  117. requrl = "http://"
  118. requrl += signstr[4::]
  119. req = urllib.request.Request(requrl, data=content, headers=headers)
  120. res_data = urllib.request.urlopen(req)
  121. r = res_data.read().decode('utf-8')
  122. res = json.loads(r)
  123. if res['code'] == 0:
  124. response.append(res['text'])
  125. file_object.close()
  126. return response[len(response)-1]