Node + 讯飞语音 定时播放天气预报音频

本文介绍了如何使用Node.js结合讯飞语音和聚合数据API,实现每天早上八点定时播放天气预报的音频。通过创建lib和src文件夹,设置定时任务,并利用play库自动播放音频。

前言

最近看了几篇文章,总觉得自己没发挥树莓派的作用,于是就琢磨着,哎,灵光一闪,整一个早晨叫醒服务,于是便有了本篇水文。

功能

每天早上八点钟,定时播放音频(音频内容为当天天气预报和空气质量),播放完成之后继续等待到明天的八点钟播放。

效果如下

技术

开始本来是想加个客户端的,但是一想先先直接跑个服务就用的node试试,所以本文只需要你会用js就行

  1. node(服务)

  2. 讯飞语音(转音频)

  3. play(播放语言)

  4. 聚合api(天气预报接口)

  5. scheduleJob(定时任务)

准备工作

我们需要文字识别转成音频,讯飞是可以白嫖的

讯飞语音

请先在讯飞注册账号及 > 创建应用 > 实名认证

https://passport.xfyun.cn/

然后去控制台找到服务接口认证信息

https://console.xfyun.cn/services/tts

找到需要的 key

聚合数据

天气预报api,如果有你其他的当然也可以用其他的,这个也是白嫖

就不做重点讲了

官网 https://www.juhe.cn/

天气预报 https://www.juhe.cn/docs/api/id/73

快速上手

初始化项目

npm init -y

然后安装我们需要的依赖, 下面是依赖的版本

{
  "name": "node-jiaoxing",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "dev": "node src/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-schedule": "^2.0.0",
    "request": "^2.88.2",
    "websocket": "^1.0.31"
  }
}

安装依赖

npm insatll

创建lib文件夹

创建一个 lib 文件夹,放入别人封装好的讯飞的资源,其主要作用就是将文本转为音频

index.js
const fs = require("fs");
const WebSocketClient = require('websocket').client;
const { getWssInfo, textToJson } = require('./util');

const xunfeiTTS = (auth, business, text, fileName, cb) => {
  let audioData = [];

  const client = new WebSocketClient();
  client.on('connect', (con) => {
    con.on('error', error => {
      throw (error);
    });

    con.on('close', () => {
      const buffer = Buffer.concat(audioData);
      fs.writeFile(fileName, buffer, (err) => {
        if (err) {
          throw (err);
          cb(err);
        } else {
          cb(null, 'OK');
        }
      });
    })

    con.on('message', (message) => {
      if (message.type == 'utf8') {
        const ret = JSON.parse(message.utf8Data);
        audioData.push(Buffer.from(ret.data.audio, 'base64'));
        if (ret.data.status == 2) {
          con.close();
        }
      }
    });

    if (con.connected) {
      const thejson = textToJson(auth, business, text);
      con.sendUTF(thejson);
    }
  });

  client.on('connectFailed', error => {
    throw (error);
  });

  const info = getWssInfo(auth);
  client.connect(info.url);
}

module.exports = xunfeiTTS;
util.js
function btoa(text) {
  return Buffer.from(text, "utf8").toString("base64");
}

function getWssInfo(auth, path = "/v2/tts", host = "tts-api.xfyun.cn") {
  const { app_skey, app_akey } = auth;
  const date = new Date(Date.now()).toUTCString();
  const request_line = `GET ${path} HTTP/1.1`;

  const signature_origin = `host: ${host}\ndate: ${date}\n${request_line}`;

  let crypto = require("crypto");

  const signature = crypto
    .createHmac("SHA256", app_skey)
    .update(signature_origin)
    .digest("base64");

  const authorization_origin = `api_key="${app_akey}",algorithm="hmac-sha256",headers="host date request-line",signature="${signature}"`;
  const authorization = btoa(authorization_origin);

  const thepath = `${path}?authorization=${encodeURIComponent(
    authorization
  )}&host=${encodeURIComponent(host)}&date=${encodeURIComponent(date)}`;

  const final_url = `wss://${host}${thepath}`;

  return { url: final_url, host: host, path: thepath };
}

function textToJson
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值