Strip decoded text
This commit is contained in:
51
src/index.js
51
src/index.js
@@ -2,19 +2,23 @@
|
||||
|
||||
import he from 'he';
|
||||
import axios from 'axios';
|
||||
import { find } from 'lodash';
|
||||
// import striptags from 'striptags';
|
||||
import {
|
||||
find
|
||||
} from 'lodash';
|
||||
import striptags from 'striptags';
|
||||
|
||||
export async function getSubtitles({
|
||||
videoID,
|
||||
lang = 'en',
|
||||
}: {
|
||||
videoID: string,
|
||||
lang: 'en',
|
||||
}) {
|
||||
const { data } = await axios.get(
|
||||
`https://youtube.com/get_video_info?video_id=${videoID}`
|
||||
);
|
||||
videoID: string,
|
||||
lang: 'en',
|
||||
}) {
|
||||
const {
|
||||
data
|
||||
} = await axios.get(
|
||||
`https://youtube.com/get_video_info?video_id=${videoID}`
|
||||
);
|
||||
|
||||
const decodedData = decodeURIComponent(data);
|
||||
|
||||
@@ -24,17 +28,25 @@ export async function getSubtitles({
|
||||
|
||||
const regex = /({"captionTracks":.*isTranslatable":(true|false)}])/;
|
||||
const [match] = regex.exec(decodedData);
|
||||
const { captionTracks } = JSON.parse(`${match}}`);
|
||||
const {
|
||||
captionTracks
|
||||
} = JSON.parse(`${match}}`);
|
||||
|
||||
const subtitle =
|
||||
find(captionTracks, { vssId: `.${lang}` }) ||
|
||||
find(captionTracks, { vssId: `a.${lang}` });
|
||||
find(captionTracks, {
|
||||
vssId: `.${lang}`
|
||||
}) ||
|
||||
find(captionTracks, {
|
||||
vssId: `a.${lang}`
|
||||
});
|
||||
|
||||
// * ensure we have found the correct subtitle lang
|
||||
if (!subtitle || (subtitle && !subtitle.baseUrl))
|
||||
throw new Error(`Could not find ${lang} captions for ${videoID}`);
|
||||
|
||||
const { data: transcript } = await axios.get(subtitle.baseUrl);
|
||||
const {
|
||||
data: transcript
|
||||
} = await axios.get(subtitle.baseUrl);
|
||||
const lines = transcript
|
||||
.replace('<?xml version="1.0" encoding="utf-8" ?><transcript>', '')
|
||||
.replace('</transcript>', '')
|
||||
@@ -44,21 +56,22 @@ export async function getSubtitles({
|
||||
const startRegex = /start="([\d.]+)"/;
|
||||
const durRegex = /dur="([\d.]+)"/;
|
||||
|
||||
const fontTag = new RegExp('<'+'font'+'[^><]*>|<.'+'font'+'[^><]*>','g');
|
||||
const [, start] = startRegex.exec(line);
|
||||
const [, dur] = durRegex.exec(line);
|
||||
|
||||
const htmlText = line
|
||||
.replace(/<text.+>/, '')
|
||||
.replace(/&/gi, '&')
|
||||
.replace(/<\/?[^>]+(>|$)/g, '')
|
||||
.replace(fontTag, '');
|
||||
.replace(/<\/?[^>]+(>|$)/g, '');
|
||||
|
||||
// const strippedText = striptags(htmlText);
|
||||
const decodedText = he.decode(htmlText);
|
||||
const text = striptags(decodedText);
|
||||
|
||||
const text = he.decode(htmlText);
|
||||
|
||||
return { start, dur, text };
|
||||
return {
|
||||
start,
|
||||
dur,
|
||||
text
|
||||
};
|
||||
});
|
||||
|
||||
return lines;
|
||||
|
||||
Reference in New Issue
Block a user