Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularize youtube-transcript's fetchTranscript API #35

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,54 @@ $ yarn add youtube-transcript

```js
import { YoutubeTranscript } from 'youtube-transcript';

YoutubeTranscript.fetchTranscript('videoId or URL').then(console.log);
```

or override / provide your own implementation of specific methods

```js
import { YoutubeTranscript } from 'youtube-transcript';
import { request } from "obsidian";

class ObsidianYoutubeTranscript extends YoutubeTranscript {
videoId: string;
videoPageBody: string;
transcriptBody: string;

constructor(videoId: string) {
super();
this.videoId = YoutubeTranscript.retrieveVideoId(videoId);
}

public async getPageBody(): Promise<string> {
const videoPageResponse = await request(
`https://www.youtube.com/watch?v=${this.videoId}`
);
this.videoPageBody = videoPageResponse;
return this.videoPageBody;
}

public async getTranscriptResponse(transcriptURL: string): Promise<string> {
const transcriptResponse = await request(transcriptURL);
this.transcriptBody = transcriptResponse;
return this.transcriptBody;
}
}

const ytt = new ObsidianYoutubeTranscript(url);
const transcriptList = await ytt.fetchTranscript();
console.debug("got transcriptList:", transcriptList);
```

### Methods

- fetchTranscript(videoId: string [,options: TranscriptConfig]): Promise<TranscriptResponse[]>;
- async getPageBody(): Promise<string>;
- splitHtml(pageBody: string): string[];
- getCaptionTracks(splittedHTML: string[]): any[];
- getTranscriptURL(captionTracks: any[]): string;
- async getTranscriptResponse(transcriptURL: string): Promise<string>;
- async parseTranscript(transcriptBody: string, captionTracks: any[] ): Promise<TranscriptResponse[]>;

## License

Expand Down
9 changes: 9 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+.tsx?$': ['ts-jest', {}],
},
testMatch: ['**/tests/**/*.test.ts'],
};
Loading