Fix: Fix fails for some Id's (#4)

fixes #3 

* Add tests, and test runner

* Add failing test

* Add flowconfig

* Fix #3: Fails for `en` and videoId `JueUvj6X3DA`
This commit is contained in:
syzer
2018-08-31 15:25:45 +02:00
committed by Haroen Viaene
parent e344fdd5f6
commit de7581ac7b
5 changed files with 1247 additions and 34 deletions

11
.flowconfig Normal file
View File

@@ -0,0 +1,11 @@
[ignore]
[include]
[libs]
[lints]
[options]
[strict]

View File

@@ -19,9 +19,12 @@
"scripts": {
"build": "rm -rf dist && babel src -d dist",
"prepublish": "npm run build",
"lint": "eslint src"
"lint": "eslint src",
"test": "ava",
"flow": "flow"
},
"devDependencies": {
"ava": "^0.25.0",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.0.2",
"babel-preset-env": "^1.6.1",
@@ -43,5 +46,11 @@
"he": "^1.1.1",
"lodash": "^4.17.4",
"striptags": "^3.1.0"
},
"ava": {
"babel": "inherit",
"require": [
"babel-register"
]
}
}

View File

@@ -10,7 +10,7 @@ export async function getSubtitles({
lang = 'en',
}: {
videoID: string,
lang: 'en',
lang: 'en' | 'de' | 'fr' | void,
}) {
const { data } = await axios.get(
`https://youtube.com/get_video_info?video_id=${videoID}`
@@ -32,7 +32,8 @@ export async function getSubtitles({
}) ||
find(captionTracks, {
vssId: `a.${lang}`,
});
}) ||
find(captionTracks, ({ vssId }) => vssId && vssId.match(`.${lang}`));
// * ensure we have found the correct subtitle lang
if (!subtitle || (subtitle && !subtitle.baseUrl))

16
test/index.test.js Normal file
View File

@@ -0,0 +1,16 @@
import test from 'ava';
import { getSubtitles } from '../src';
test('Extract estonia war subtitles', async t => {
const subtitles = await getSubtitles({ videoID: 'HBA0xDHZjko' });
t.deepEqual(subtitles[0], {
dur: '5.87',
start: '6.62',
text: 'November 19',
});
});
test('Extract passive income video', async t => {
const subtitles = await getSubtitles({ videoID: 'JueUvj6X3DA' });
t.deepEqual(subtitles[0].text, '- Creating passive income takes work,');
});

1238
yarn.lock

File diff suppressed because it is too large Load Diff