def getMusic(self, musicName="", fromName=""): tool.line() res = [] self.out("歌曲名字[" + musicName + "] from:" + fromName) responce = self.http.do("http://music.163.com/api/search/get/web?csrf_token=", { "hlpretag":"", "hlposttag":"", "s":musicName, "type":1, "offset":0, "total":"true", "limit":5, }) obj = tool.toJson(responce.read()) songs = obj.get("result", {}).get("songs", []) self.out("抓取到音乐:") for item in songs: id = item.get("id", "") name = item.get("name", "") url = "http://link.hhtjim.com/163/" + str(id) + ".mp3" duration = item.get("duration", 0) music = {"url":url, "name":name, "fromName":fromName, "duration":duration} self.out("url:" + url + " name:" + name) res.append(music) return res
def doJson(self, url="", postData=None): res = {} responce = self.do(url, postData) if(type(responce) == str): jsonStr = responce else: jsonStr = responce.read() if(jsonStr != None and type(jsonStr) == str): jsonStr = jsonStr.strip() res = tool.toJson(jsonStr) else: self.out("responce 读取失败,url:" + str(url) + " data:" + str(postData)) return res
def getAudio(self, text=""): flag = False res = "" if (text == ""): return flag, res url = "http://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=" + self.name + "&tok=" + self.access_token + "&tex=" + self.http.encode( text) + "&vol=13&per=0&spd=5&pit=5&aue=3" responce = self.http.do(url) # tex 必填 合成的文本,使用UTF-8编码。小于2048个中文字或者英文数字。(文本在百度服务器内转换为GBK后,长度必须小于4096字节) # tok 必填 开放平台获取到的开发者access_token(见上面的“鉴权认证机制”段落) # cuid 必填 用户唯一标识,用来计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内 # ctp 必填 客户端类型选择,web端填写固定值1 # lan 必填 固定值zh。语言选择,目前只有中英文混合模式,填写固定值zh # spd 选填 语速,取值0-15,默认为5中语速 # pit 选填 音调,取值0-15,默认为5中语调 # vol 选填 音量,取值0-15,默认为5中音量 # per 选填 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声 # aue 选填 3为mp3格式(默认); 4为pcm-16k;5为pcm-8k;6为wav(内容同pcm-16k); 注意aue=4或者6是语音识别要求的格式,但是音频内容不是语音识别要求的自然人发音,所以识别效果会受影响。 if (type(responce) == str): res = "转换接口访问失败" + str(text) else: header = responce.headers # Content-Type: audio/mp3; # Content-Type: application/json ct = header.get("Content-Type", "") if (ct == "application/json"): obj = tool.toJson(responce.read()) # {"err_no":500,"err_msg":"notsupport.","sn":"abcdefgh","idx":1} code = obj.get("err_no", "") if (str(code)[0:1] == "5"): if (code == 500): info = "不支持输入" elif (code == 501): info = "输入参数不正确" elif (code == 502): info = "token验证失败" elif (code == 503): info = "合成后端错误" else: info = "其他错误" + str(code) info = info + " " + obj.get("err_msg", "") else: info = str(obj) flag = False res = info else: flag = True res = url self.out("转换文本[" + text + "] -> " + res) return flag, res
def turnArray(self, args): if(len(args) == 1): args = args[0] reg = re.match(r'^\d+', args) #25[xxxx] -> [xxxxx] if(reg is not None): start = reg.group() args = args[len(start):999999] # reg = re.match(r'^\["\w+",', args) #["message", {"to":{"id":" -> {"to":xxxx # if(reg is not None): # start = reg.group() # args = args[len(start):999999] # mtype = start[2:-2] res = tool.toJson(args) else: res = list(args) if(len(res) <= 1): res = ( 'null', res[0]) elif(callable(res[1])): res = ( 'fun', res[0]) return res
def message(self, *args): # 普通消息 try: # print("收到message ") # print(args) # tool.line() # print("转换") args = self.turnArray(args) # print(args[0]) # print(args[1]) if(len(args) <= 1): return data = args[1] #{} mtype = args[0] #message null fun if(mtype == 'message'): data = tool.toJson(data) fro = data.get("from", {}) to = data.get("to", {}) contact = data.get("contact", {}) msg = data.get("body") fullId = contact.get("fullId") sessionName = contact.get("nickName", "") uid = self.data.get("uuid") tTag = data.get("timeMillis", tool.getNowTime()) tool.line() self.out("Msg:" + fro.get("nickName","from") + ">>" + msg + ">>" + to.get("nickName","to") + " time:" + data.get("time")) # self.out(msg); # 自发消息不需要处理 if(fro.get("nickName","from").find(self.loginUser.get("ORG_VARS", {}).get("@USER_NAME@", "")) >= 0): return ttt = self.detaTime reg = re.match(r'^\d+$', str(msg)) if(reg is not None): ttt = int(msg) * 1000 self.send("updateConversationStatus", { 'contactFullId': fullId, 'clientId': uid, 'timeTag': tTag }, ttt) self.send("updateMsgStatus", { "messages":data.get("id","") }, ttt) # 自发言 且 只有自己auto回复 if(self.id != "18408249138"): return # 过滤 ff = sessionName.find("陈鹏辉") hh = fro.get("nickName","from").find("许欢") zz = fro.get("nickName","from").find("赵振国") cdf = fro.get("nickName","from").find("迪") d1 = sessionName.find("迪") d2 = sessionName.find("祝") d3 = sessionName.find("纯") if(ff < 0 or d1 >= 0 or d2 >= 0 or d3 >= 0): self.out("未命中自己title 命中特殊 不回复") return point = 76 if(hh >= 0 or zz >= 0): point = 33 self.sendTrue("updateMsgStatus", { "messages":data.get("id","") }) if(tool.getRandom(0,100) < point): self.out("概率不自动回复" + str(point)) return # data["body"] = str(data["body"]) + "." # self.send("message", data) obj = {} # if(contact.get("type") == "GROUP"): # unicode(self.robot.do(msg, fro.get("nickName"))) msg = self.robot.do(msg, fro.get("nickName")) msg = msg.get("text", ''); if(msg == ''): return obj["body"] = msg #"666" + str(tool.getNowTime()) obj["bodyType"] = "text" obj["clientId"] = str(uuid.uuid1()) obj["retry"] = 1 obj["from"] = {} obj["from"]["fullId"] = "u__" + self.data.get("userName") obj["from"]["id"] = self.data.get("userName") obj["from"]["nickName"] = "fromnickname" obj["to"] = {} if(contact.get("type") == "GROUP"): obj["to"]["fullId"] = contact.get("fullId") else: obj["to"]["fullId"] = fro.get("fullId") obj["to"]["nickName"] = "tonickname" obj["from"]["nickName"] = self.loginUser["ORG_VARS"]["@USER_NAME@"] # "from-nickName" obj["to"]["nickName"] = "to-nickName" self.sendTrue("message", obj) elif(str(mtype) == 'fun'): pass elif(str(mtype) == 'event'): pass else: self.out("其他:" + str(mtype)) print(data) # print(args) # tool.line() except Exception as e: self.out(traceback.format_exc()) return