{"size": 1578, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "oms-services/google-speech", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "oms-services/google-speech", "max_issues_repo_issues_event_min_datetime": "2019-11-18T00:26:43.000Z", "max_issues_repo_issues_event_max_datetime": "2019-11-18T00:26:43.000Z", "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "oms-services/google-speech", "max_forks_repo_forks_event_min_datetime": "2021-12-07T11:27:29.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-07T11:27:29.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 1, "avg_line_length": 31.56, "max_line_length": 106, "alphanum_fraction": 0.6920152091}
#!/usr/bin/env node

const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const speech = require('@google-cloud/speech')
const fs = require('fs')
const { fetchRemoteAudioFile } = require('./helpers')

app.use(bodyParser.json())

const { GOOGLE_APPLICATION_CREDENTIALS, PORT = 8080 } = process.env

// @TODO: Clean this up? What's the best way to deal w/ `GOOGLE_APPLICATION_CREDENTIALS` & OMS/Containers?
const encodedCredentials = Buffer.from(GOOGLE_APPLICATION_CREDENTIALS, 'base64')
const decodedCredentials = encodedCredentials.toString('ascii')
fs.writeFileSync('/tmp/credentials.json', decodedCredentials)
process.env.GOOGLE_APPLICATION_CREDENTIALS = '/tmp/credentials.json'

// Instantiates a client
const client = new speech.SpeechClient()

app.post('/transcribe', async (req, res) => {
  const { encoding = 'LINEAR16', languageCode = 'en-US', sampleRateHertz = 16000, url = '' } = req.body

  const file = await fetchRemoteAudioFile(url).catch(er => {
    res.status(500).json({ message: er })
  })
  const audioBytes = file.toString('base64')

  const request = {
    audio: { content: audioBytes },
    config: { encoding, sampleRateHertz, languageCode }
  }

  client
    .recognize(request)
    .then(data => {
      const results = data[0].results[0].alternatives[0]
      res.json(results)
    })
    .catch(er => {
      res.status(500).json({ message: er.message })
    })
})

app.get('/health', (req, res) => res.send('OK'))

app.listen(PORT, () => console.log(`Listening on localhost: http://localhost:${PORT}`))
