style(eslint): add yarn lint and fix codestyle
This commit is contained in:
@@ -18,7 +18,8 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rm -rf dist && babel src -d dist",
|
"build": "rm -rf dist && babel src -d dist",
|
||||||
"prepublish": "npm run build"
|
"prepublish": "npm run build",
|
||||||
|
"lint": "eslint src"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.26.0",
|
"babel-cli": "^6.26.0",
|
||||||
|
|||||||
32
src/index.js
32
src/index.js
@@ -2,23 +2,19 @@
|
|||||||
|
|
||||||
import he from 'he';
|
import he from 'he';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import {
|
import { find } from 'lodash';
|
||||||
find
|
|
||||||
} from 'lodash';
|
|
||||||
import striptags from 'striptags';
|
import striptags from 'striptags';
|
||||||
|
|
||||||
export async function getSubtitles({
|
export async function getSubtitles({
|
||||||
videoID,
|
videoID,
|
||||||
lang = 'en',
|
lang = 'en',
|
||||||
}: {
|
}: {
|
||||||
videoID: string,
|
videoID: string,
|
||||||
lang: 'en',
|
lang: 'en',
|
||||||
}) {
|
}) {
|
||||||
const {
|
const { data } = await axios.get(
|
||||||
data
|
`https://youtube.com/get_video_info?video_id=${videoID}`
|
||||||
} = await axios.get(
|
);
|
||||||
`https://youtube.com/get_video_info?video_id=${videoID}`
|
|
||||||
);
|
|
||||||
|
|
||||||
const decodedData = decodeURIComponent(data);
|
const decodedData = decodeURIComponent(data);
|
||||||
|
|
||||||
@@ -28,25 +24,21 @@ export async function getSubtitles({
|
|||||||
|
|
||||||
const regex = /({"captionTracks":.*isTranslatable":(true|false)}])/;
|
const regex = /({"captionTracks":.*isTranslatable":(true|false)}])/;
|
||||||
const [match] = regex.exec(decodedData);
|
const [match] = regex.exec(decodedData);
|
||||||
const {
|
const { captionTracks } = JSON.parse(`${match}}`);
|
||||||
captionTracks
|
|
||||||
} = JSON.parse(`${match}}`);
|
|
||||||
|
|
||||||
const subtitle =
|
const subtitle =
|
||||||
find(captionTracks, {
|
find(captionTracks, {
|
||||||
vssId: `.${lang}`
|
vssId: `.${lang}`,
|
||||||
}) ||
|
}) ||
|
||||||
find(captionTracks, {
|
find(captionTracks, {
|
||||||
vssId: `a.${lang}`
|
vssId: `a.${lang}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
// * ensure we have found the correct subtitle lang
|
// * ensure we have found the correct subtitle lang
|
||||||
if (!subtitle || (subtitle && !subtitle.baseUrl))
|
if (!subtitle || (subtitle && !subtitle.baseUrl))
|
||||||
throw new Error(`Could not find ${lang} captions for ${videoID}`);
|
throw new Error(`Could not find ${lang} captions for ${videoID}`);
|
||||||
|
|
||||||
const {
|
const { data: transcript } = await axios.get(subtitle.baseUrl);
|
||||||
data: transcript
|
|
||||||
} = await axios.get(subtitle.baseUrl);
|
|
||||||
const lines = transcript
|
const lines = transcript
|
||||||
.replace('<?xml version="1.0" encoding="utf-8" ?><transcript>', '')
|
.replace('<?xml version="1.0" encoding="utf-8" ?><transcript>', '')
|
||||||
.replace('</transcript>', '')
|
.replace('</transcript>', '')
|
||||||
@@ -70,7 +62,7 @@ export async function getSubtitles({
|
|||||||
return {
|
return {
|
||||||
start,
|
start,
|
||||||
dur,
|
dur,
|
||||||
text
|
text,
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user