Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #125 from watson-developer-cloud/up
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
germanattanasio authored Nov 7, 2019
2 parents 8a3fd1a + 80b0ea6 commit 3ef2d4a
Show file tree
Hide file tree
Showing 7 changed files with 2,048 additions and 1,767 deletions.
6 changes: 0 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@

# You need to provide either username and password
TEXT_TO_SPEECH_USERNAME=
TEXT_TO_SPEECH_PASSWORD=
# OR IAM API key and URL
TEXT_TO_SPEECH_IAM_APIKEY=
TEXT_TO_SPEECH_IAM_URL=

TEXT_TO_SPEECH_URL=https://stream.watsonplatform.net/text-to-speech/api
8 changes: 2 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: node_js
dist: trusty
sudo: required
node_js: 8
node_js: 12
script:
- npm run test

Expand All @@ -18,13 +17,10 @@ before_deploy: npm install -g bx-blue-green
deploy:
- provider: script
skip_cleanup: true
script:
- bx-blue-green-travis
script: bx-blue-green-travis
on:
branch: master
repo: watson-developer-cloud/text-to-speech-nodejs
- provider: script
skip_cleanup: true
script: npx semantic-release
on:
node: 8
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can view a [demo][demo_url] of this app.
- Log in to your IBM Cloud account.
- Click **Create**.
- Click **Show** to view the service credentials.
- Copy the `apikey` value, or copy the `username` and `password` values if your service instance doesn't provide an `apikey`.
- Copy the `apikey` value.
- Copy the `url` value.

## Configuring the application
Expand All @@ -44,16 +44,6 @@ You can view a [demo][demo_url] of this app.
TEXT_TO_SPEECH_URL=https://gateway-wdc.watsonplatform.net/text-to-speech/api
```
- If your service instance uses `username` and `password` credentials, add the `TEXT_TO_SPEECH_USERNAME` and `TEXT_TO_SPEECH_PASSWORD` variables to the *.env* file.
Example *.env* file that configures the `username`, `password`, and `url` for a Text to Speech service instance hosted in the US South region:
```
TEXT_TO_SPEECH_USERNAME=522be-7b41-ab44-dec3-g1eab2ha73c6
TEXT_TO_SPEECH_PASSWORD=A4Z5BdGENrwu8
TEXT_TO_SPEECH_URL=https://stream.watsonplatform.net/text-to-speech/api
```
## Running locally
1. Install the dependencies
Expand Down
64 changes: 31 additions & 33 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
const express = require('express');

const app = express();
const TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1.js');
const { IamAuthenticator } = require('ibm-watson/auth');

const textToSpeech = new TextToSpeechV1({
version: '2018-04-05',
authenticator: new IamAuthenticator({
apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY || 'type-key-here',
}),
url: process.env.TEXT_TO_SPEECH_URL,
});

// Bootstrap application settings
require('./config/express')(app);
Expand All @@ -25,48 +34,37 @@ const getFileExtension = (acceptQuery) => {
}
};

let textToSpeech;

if (process.env.TEXT_TO_SPEECH_IAM_APIKEY && process.env.TEXT_TO_SPEECH_IAM_APIKEY !== '') {
textToSpeech = new TextToSpeechV1({
url: process.env.TEXT_TO_SPEECH_URL || 'https://stream.watsonplatform.net/text-to-speech/api',
iam_apikey: process.env.TEXT_TO_SPEECH_IAM_APIKEY || '<iam_apikey>',
iam_url: 'https://iam.bluemix.net/identity/token',
});
} else {
textToSpeech = new TextToSpeechV1({
url: process.env.TEXT_TO_SPEECH_URL || 'https://stream.watsonplatform.net/text-to-speech/api',
username: process.env.TEXT_TO_SPEECH_USERNAME || '<username>',
password: process.env.TEXT_TO_SPEECH_PASSWORD || '<password>',
});
}

app.get('/', (req, res) => {
res.render('index');
});

/**
* Pipe the synthesize method
*/
app.get('/api/v1/synthesize', (req, res, next) => {
const transcript = textToSpeech.synthesize(req.query);
transcript.on('response', (response) => {
if (req.query.download) {
response.headers['content-disposition'] = `attachment; filename=transcript.${getFileExtension(req.query.accept)}`;
}
});
transcript.on('error', next);
transcript.pipe(res);
app.get('/api/v1/synthesize', async (req, res, next) => {
try {
const { result } = await textToSpeech.synthesize(req.query);
const transcript = result;
transcript.on('response', (response) => {
if (req.query.download) {
response.headers['content-disposition'] = `attachment; filename=transcript.${getFileExtension(req.query.accept)}`;
}
});
transcript.on('error', next);
transcript.pipe(res);
} catch (error) {
res.send(error);
}
});

// Return the list of voices
app.get('/api/v1/voices', (req, res, next) => {
textToSpeech.voices(null, (error, voices) => {
if (error) {
return next(error);
}
return res.json(voices);
});
app.get('/api/v1/voices', async (req, res, next) => {
try {
const { result } = textToSpeech.listVoices();
res.json(result);
} catch (error) {
next(error);
}
});

// error-handler settings
Expand Down
4 changes: 1 addition & 3 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
applications:
- services:
- my-text-to-speech
name: text-to-speech-demo
- name: text-to-speech-demo
command: npm start
path: .
memory: 512M
Loading

0 comments on commit 3ef2d4a

Please sign in to comment.