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:
11
.flowconfig
Normal file
11
.flowconfig
Normal file
@@ -0,0 +1,11 @@
|
||||
[ignore]
|
||||
|
||||
[include]
|
||||
|
||||
[libs]
|
||||
|
||||
[lints]
|
||||
|
||||
[options]
|
||||
|
||||
[strict]
|
||||
11
package.json
11
package.json
@@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
16
test/index.test.js
Normal 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,');
|
||||
});
|
||||
Reference in New Issue
Block a user