Created
February 26, 2021 10:51
-
-
Save anonrose/01d9860bf7d6d75b8870acf8e57aa168 to your computer and use it in GitHub Desktop.
synthesize earnings call transcripts from GCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const textToSpeech = require('@google-cloud/text-to-speech'); | |
const fs = require('fs'); | |
const util = require('util'); | |
const puppeteer = require('puppeteer'); | |
const auth = { | |
credentials: { // your gcp service account credentials | |
} | |
}; | |
// seeking alpha earnings transcript, change for different transcript | |
const seekingAlphaURL = 'https://seekingalpha.com/article/4409236-root-root-ceo-alex-timm-on-q4-2020-results-earnings-call-transcript'; | |
(async () => { | |
try { | |
const browser = await puppeteer.launch({ | |
args: [ | |
'--incognito', | |
], | |
headless: false | |
}); | |
const page = await browser.newPage(); | |
await page.goto(seekingAlphaURL, { | |
waitUntil: 'networkidle0', | |
}); | |
await new Promise((res) => setTimeout(res, 2000)) | |
page.setViewport({ width: 1200, height: 1600 }) | |
await page.waitFor('[data-test-id="content-container"]'); | |
let [text] = Object.values(await page.evaluate(() => ({ transcript: document.querySelector('[data-test-id="content-container"]').innerText }))); | |
const segments = text.split('\n\n'); | |
const client = new textToSpeech.TextToSpeechClient(auth); | |
const responses = await Promise.all(segments.map(text => { | |
const request = { | |
input: { text }, | |
voice: { languageCode: 'en-US', ssmlGender: 'MALE' }, | |
audioConfig: { audioEncoding: 'MP3' }, | |
}; | |
return client.synthesizeSpeech(request) | |
})); | |
const writeFile = util.promisify(fs.writeFile); | |
await writeFile('output.mp3', Buffer.concat(responses.map(([r]) => r.audioContent)), 'binary'); | |
console.log('Audio content written to file: output.mp3'); | |
await browser.close(); | |
} catch (error) { | |
console.error(error) | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment