Skip to content

Commit a876c9b

Browse files
committed
refactor: move bulk transcript retrieval to YoutubeTranscriptApi, cleanup code
1 parent 3236fb9 commit a876c9b

15 files changed

+546
-567
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,8 @@ TranscriptContent transcriptContent = youtubeTranscriptApi.getTranscriptWithCook
282282

283283
### Bulk Transcript Retrieval
284284

285-
All bulk transcript retrieval operations are done via the `PlaylistsTranscriptApi` interface. Same as with the
286-
`YoutubeTranscriptApi`,
287-
you can create a new instance of the PlaylistsTranscriptApi by calling the `createDefaultPlaylistsApi` method of the
288-
`TranscriptApiFactory`.
285+
There are a few methods for bulk transcript retrieval in `YoutubeTranscriptApi`
286+
289287
Playlists and channels information is retrieved from
290288
the [YouTube V3 API](https://developers.google.com/youtube/v3/docs/),
291289
so you will need to provide API key for all methods.
@@ -304,30 +302,30 @@ All methods return a map which contains the video ID as a key and the correspond
304302

305303
```java
306304
// Create a new default PlaylistsTranscriptApi instance
307-
PlaylistsTranscriptApi playlistsTranscriptApi = TranscriptApiFactory.createDefaultPlaylistsApi();
305+
YoutubeTranscriptApi youtubeTranscriptApi = TranscriptApiFactory.createDefault();
308306

309307
//Create request object
310308
TranscriptRequest request = new TranscriptRequest("apiKey");
311309

312310
// Retrieve all available transcripts for a given playlist
313-
Map<String, TranscriptList> transcriptLists = playlistsTranscriptApi.listTranscriptsForPlaylist("playlistId", request);
311+
Map<String, TranscriptList> transcriptLists = youtubeTranscriptApi.listTranscriptsForPlaylist("playlistId", request);
314312

315313
// Retrieve all available transcripts for a given channel
316-
Map<String, TranscriptList> transcriptLists = playlistsTranscriptApi.listTranscriptsForChannel("channelName", request);
314+
Map<String, TranscriptList> transcriptLists = youtubeTranscriptApi.listTranscriptsForChannel("channelName", request);
317315
```
318316

319-
Same as with the `YoutubeTranscriptApi`, you can also fetch transcript content directly
317+
Same as with the `getTranscript` method, you can also fetch transcript content directly
320318
using [fallback languages](#use-fallback-language) if needed.
321319

322320
```java
323321
//Create request object
324322
TranscriptRequest request = new TranscriptRequest("apiKey");
325323

326324
// Retrieve transcript content for all videos in a playlist
327-
Map<String, TranscriptContent> transcriptLists = playlistsTranscriptApi.getTranscriptsForPlaylist("playlistId", request);
325+
Map<String, TranscriptContent> transcriptLists = youtubeTranscriptApi.getTranscriptsForPlaylist("playlistId", request);
328326

329327
// Retrieve transcript content for all videos in a channel
330-
Map<String, TranscriptContent> transcriptLists = playlistsTranscriptApi.getTranscriptsForChannel("channelName", request, "en", "de");
328+
Map<String, TranscriptContent> transcriptLists = youtubeTranscriptApi.getTranscriptsForChannel("channelName", request, "en", "de");
331329
```
332330

333331
> **Note:** If you want to get transcript content in a different format, refer

lib/src/main/java/io/github/thoroldvix/api/PlaylistsTranscriptApi.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

lib/src/main/java/io/github/thoroldvix/api/YoutubeTranscriptApi.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import io.github.thoroldvix.internal.TranscriptApiFactory;
44

5+
import java.util.Map;
6+
57
/**
68
* This is the main interface for the YouTube Transcript API.
79
* <p>
8-
* It provides functionality for retrieving all available transcripts or retrieving actual transcript content from YouTube.
10+
* It provides functionality for retrieving all available transcripts or retrieving actual transcript content for a single video, playlist, or channel.
911
* </p>
1012
* <p>
1113
* To instantiate this API, you should use {@link TranscriptApiFactory}.
@@ -94,4 +96,61 @@ public interface YoutubeTranscriptApi {
9496
* @throws IllegalArgumentException If the video ID is invalid
9597
*/
9698
TranscriptContent getTranscript(String videoId, String... languageCodes) throws TranscriptRetrievalException;
99+
100+
/**
101+
* Retrieves transcript lists for all videos in the specified playlist.
102+
*
103+
* @param playlistId The ID of the playlist
104+
* @param request {@link TranscriptRequest} request object containing API key, cookies file path, and stop on error flag
105+
* @return A map of video IDs to {@link TranscriptList} objects
106+
* @throws TranscriptRetrievalException If the retrieval of the transcript lists fails
107+
*/
108+
Map<String, TranscriptList> listTranscriptsForPlaylist(String playlistId, TranscriptRequest request) throws TranscriptRetrievalException;
109+
110+
111+
/**
112+
* Retrieves transcript lists for all videos for the specified channel.
113+
*
114+
* @param channelName The name of the channel
115+
* @param request {@link TranscriptRequest} request object containing API key, cookies file path, and stop on error flag
116+
* @return A map of video IDs to {@link TranscriptList} objects
117+
* @throws TranscriptRetrievalException If the retrieval of the transcript lists fails
118+
*/
119+
Map<String, TranscriptList> listTranscriptsForChannel(String channelName, TranscriptRequest request) throws TranscriptRetrievalException;
120+
121+
122+
/**
123+
* Retrieves transcript content for all videos in the specified playlist.
124+
*
125+
* @param playlistId The ID of the playlist
126+
* @param request {@link TranscriptRequest} request object containing API key, cookies file path, and stop on error flag
127+
* @param languageCodes A varargs list of language codes in descending priority.
128+
* <p>
129+
* For example:
130+
* </p>
131+
* If this is set to {@code ("de", "en")}, it will first attempt to fetch the German transcript ("de"), and then fetch the English
132+
* transcript ("en") if the former fails. If no language code is provided, it uses English as the default language.
133+
* @return A map of video IDs to {@link TranscriptContent} objects
134+
* @throws TranscriptRetrievalException If the retrieval of the transcript fails
135+
*/
136+
Map<String, TranscriptContent> getTranscriptsForPlaylist(String playlistId,
137+
TranscriptRequest request,
138+
String... languageCodes) throws TranscriptRetrievalException;
139+
140+
141+
/**
142+
* Retrieves transcript content for all videos for the specified channel.
143+
*
144+
* @param channelName The name of the channel
145+
* @param request {@link TranscriptRequest} request object containing API key, cookies file path, and stop on error flag
146+
* @param languageCodes A varargs list of language codes in descending priority.
147+
* <p>
148+
* For example:
149+
* </p>
150+
* If this is set to {@code ("de", "en")}, it will first attempt to fetch the German transcript ("de"), and then fetch the English
151+
* transcript ("en") if the former fails. If no language code is provided, it uses English as the default language.
152+
* @return A map of video IDs to {@link TranscriptContent} objects
153+
* @throws TranscriptRetrievalException If the retrieval of the transcript fails
154+
*/
155+
Map<String, TranscriptContent> getTranscriptsForChannel(String channelName, TranscriptRequest request, String... languageCodes) throws TranscriptRetrievalException;
97156
}

lib/src/main/java/io/github/thoroldvix/internal/DefaultPlaylistsTranscriptApi.java

Lines changed: 0 additions & 189 deletions
This file was deleted.

0 commit comments

Comments
 (0)