Reputation: 3
I'm trying to convert a javascript snippet to typescript, the javascript version looks to be working but I'm struggling a bit with to refactor the promise part in typescript.
javascript code
var youtubeCaptionsScraper = require("youtube-captions-scraper")
function srtTimestamp(seconds) {
var $milliseconds = seconds*1000;
$seconds = Math.floor($milliseconds / 1000);
$minutes = Math.floor($seconds / 60);
$hours = Math.floor($minutes / 60);
$milliseconds = $milliseconds % 1000;
$seconds = $seconds % 60;
$minutes = $minutes % 60;
return ($hours < 10 ? '0' : '') + $hours + ':'
+ ($minutes < 10 ? '0' : '') + $minutes + ':'
+ ($seconds < 10 ? '0' : '') + $seconds + ','
+ ($milliseconds < 100 ? '0' : '') + ($milliseconds < 10 ? '0' : '') + $milliseconds;
}
function inputToSRT(srtCount, sub_in) {
return srtCount + "\r\n" + srtTimestamp(sub_in.start) + " --> " + srtTimestamp(sub_in.dur) + "\r\n" + sub_in.text + "\r\n\r\n";
}
async function getUrlSrtSubtitles(url, language) {
await youtubeCaptionsScraper.getSubtitles({
videoID: url.split("v=")[1].split("&")[0], // youtube video id
lang: language
}).then(captions => {
console.log(captions);
// formating captions to srt text format
srtCount = 0;
srtString = '';
for (var i=0; i<captions.length; i++) {
srtString = srtString + inputToSRT(++srtCount, captions[i]);
}
console.log(srtString);
return srtString;
});
}
var url = "https://www.youtube.com/watch?v=7wnfj-b-wp4";
getUrlSrtSubtitles(url, 'en')
I've validated my first part here
I'm not sure how to refactor the promise from js getUrlSrtSubtitles function into typescript so it works, any help would be appreciated.
thanks
The typescript code I've made so far looks like this
src/types.ts
export interface SubCaptions {
start: string;
dur: string;
text: string;
}
src/utils.ts
const youtubeCaptionsScraper = require("youtube-captions-scraper"); // youtubeCaptionsScraper
import {SubCaptions} from "./types";
///////////////////////////////////////////////////////
// youtubeCaptionsScraper functions
///////////////////////////////////////////////////////
export function srtTimestamp(seconds: number): string {
var milliseconds = seconds * 1000;
seconds = Math.floor(milliseconds / 1000);
var minutes = Math.floor(seconds / 60);
var hours = Math.floor(minutes / 60);
milliseconds = milliseconds % 1000;
seconds = seconds % 60;
minutes = minutes % 60;
return (hours < 10 ? '0' : '') + hours + ':'
+ (minutes < 10 ? '0' : '') + minutes + ':'
+ (seconds < 10 ? '0' : '') + seconds + ','
+ (milliseconds < 100 ? '0' : '') + (milliseconds < 10 ? '0' : '') + milliseconds;
}
export function inputToSRT(srtCount: number, sub_in: SubCaptions) {
return srtCount + "\r\n" + srtTimestamp(Number(sub_in.start)) + " --> " + srtTimestamp(Number(sub_in.dur)) + "\r\n" + sub_in.text + "\r\n\r\n";
}
export function getUrlCaptions(url: string, language: string): Array<SubCaptions> {
const captionsArray = youtubeCaptionsScraper.getSubtitles(
{
videoID: url.split("v=")[1].split("&")[0], // extract youtube video id
lang: language
}
)
return captionsArray
}
export function getUrlSrtSubtitles(url: string, language: string): string {
const captionsArray = getUrlCaptions(url, language);
// declare var
var srtCount = 0;
var srtString = '';
// formating captions to srt text format
for (var i=0; i<captionsArray.length; i++) {
srtString = srtString + inputToSRT(++srtCount, captionsArray[i]);
}
console.log(srtString);
return srtString;
}
Any help dealing with this promise would be greatly appreciated!
Upvotes: 0
Views: 117
Reputation: 1827
youtubeCaptionsScaper.getSubtitle
returns a Promise, so you can't declare getUrlCaptions
to return a simple SubCaptions[]
, you can do the following:
export async function getUrlCaptions(
url: string,
language: string
): Promise<SubCaptions[]> {
return youtubeCaptionsScraper.getSubtitles({
videoID: url.split("v=")[1].split("&")[0], // extract youtube video id
lang: language,
});
}
And in order to use this make sure to use await
const captions = await getUrlCaptions(url, language);
You can check it here
Upvotes: 1