Skip to content

Commit

Permalink
Fix: Fix fails for some Id's (#4)
Browse files Browse the repository at this point in the history
fixes #3 

* Add tests, and test runner

* Add failing test

* Add flowconfig

* Fix #3: Fails for `en` and videoId `JueUvj6X3DA`
  • Loading branch information
syzer authored and Haroenv committed Aug 31, 2018
1 parent e344fdd commit de7581a
Show file tree
Hide file tree
Showing 5 changed files with 1,250 additions and 37 deletions.
11 changes: 11 additions & 0 deletions .flowconfig
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
11 changes: 10 additions & 1 deletion package.json
Expand Up @@ -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",
Expand All @@ -43,5 +46,11 @@
"he": "^1.1.1",
"lodash": "^4.17.4",
"striptags": "^3.1.0"
},
"ava": {
"babel": "inherit",
"require": [
"babel-register"
]
}
}
5 changes: 3 additions & 2 deletions src/index.js
Expand Up @@ -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}`
Expand All @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions test/index.test.js
@@ -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,');
});

0 comments on commit de7581a

Please sign in to comment.