|
9 | 9 |
|
10 | 10 | ### This library uses undocumented YouTube API, so it's possible that it will stop working at any time. Use at your own risk.
|
11 | 11 |
|
| 12 | +> **Note:** If you want to use this library on Android platform, refer to |
| 13 | +> [Android compatibility](#android-compatibility). |
| 14 | +
|
12 | 15 | ## 📖 Introduction
|
13 | 16 |
|
14 | 17 | Java library which allows you to retrieve subtitles/transcripts for a YouTube video.
|
15 | 18 | It supports manual and automatically generated subtitles, bulk transcript retrieval for all videos in the playlist or
|
16 | 19 | on the channel and does not use headless browser for scraping.
|
17 | 20 | Inspired by [Python library](https://github.com/jdepoix/youtube-transcript-api).
|
18 | 21 |
|
19 |
| -## 🤖 Features |
| 22 | +## ☑️ Features |
20 | 23 |
|
21 | 24 | ✅ Manual transcripts retrieval
|
22 | 25 |
|
@@ -146,6 +149,13 @@ TranscriptContent transcriptContent = youtubeTranscriptApi.getTranscript("videoI
|
146 | 149 |
|
147 | 150 | For bulk transcript retrieval see [Bulk Transcript Retrieval](#bulk-transcript-retrieval).
|
148 | 151 |
|
| 152 | +## 🤖 Android compatibility |
| 153 | +This library uses Java 11 HttpClient for making YouTube request by default, it was done so it depends on minimal amount |
| 154 | +of 3rd party libraries. Since Android SDK doesn't include Java 11 HttpClient, you will have to implement |
| 155 | +your own `YoutubeClient` for it to work. |
| 156 | + |
| 157 | +You can check example implementation in [YoutubeClient Customization and Proxy](#youtubeclient-customization-and-proxy). |
| 158 | + |
149 | 159 | ## 🔧 Detailed Usage
|
150 | 160 |
|
151 | 161 | ### Use fallback language
|
@@ -248,15 +258,59 @@ String formattedContent = jsonFormatter.format(transcriptContent);
|
248 | 258 |
|
249 | 259 | By default, `YoutubeTranscriptApi` uses Java 11 HttpClient for making requests to YouTube, if you want to use a
|
250 | 260 | different client or use a proxy,
|
251 |
| -you can create your own YouTube client by implementing the `YoutubeClient` interface and passing it to |
252 |
| -the `TranscriptApiFactory` `createWithClient` method. |
| 261 | +you can create your own YouTube client by implementing the `YoutubeClient` interface. |
| 262 | + |
| 263 | +Here is example implementation using OkHttp: |
253 | 264 |
|
254 | 265 | ```java
|
255 |
| -// Create a new custom YoutubeClient |
256 |
| -YoutubeClient youtubeClient = new MyCustomYoutubeClient(); |
| 266 | +public class OkHttpYoutubeClient implements YoutubeClient { |
| 267 | + |
| 268 | + private final OkHttpClient client; |
| 269 | + |
| 270 | + public OkHttpYoutubeClient(OkHttpClient client) { |
| 271 | + this.client = client; |
| 272 | + } |
| 273 | + |
| 274 | + @Override |
| 275 | + public String get(String url, Map<String, String> headers) throws TranscriptRetrievalException { |
| 276 | + Request request = new Request.Builder() |
| 277 | + .headers(Headers.of(headers)) |
| 278 | + .url(url) |
| 279 | + .build(); |
| 280 | + |
| 281 | + return sendGetRequest(request); |
| 282 | + } |
257 | 283 |
|
258 |
| -// Create YoutubeTranscriptApi instance with custom YouTubeClient |
259 |
| -YoutubeTranscriptApi youtubeTranscriptApi = TranscriptApiFactory.createWithClient(youtubeClient); |
| 284 | + @Override |
| 285 | + public String get(YtApiV3Endpoint endpoint, Map<String, String> params) throws TranscriptRetrievalException { |
| 286 | + Request request = new Request.Builder() |
| 287 | + .url(endpoint.url(params)) |
| 288 | + .build(); |
| 289 | + |
| 290 | + return sendGetRequest(request); |
| 291 | + } |
| 292 | + |
| 293 | + private String sendGetRequest(Request request) throws TranscriptRetrievalException { |
| 294 | + try (Response response = client.newCall(request).execute()) { |
| 295 | + if (response.isSuccessful()) { |
| 296 | + ResponseBody body = response.body(); |
| 297 | + if (body == null) { |
| 298 | + throw new TranscriptRetrievalException("Response body is null"); |
| 299 | + } |
| 300 | + return body.string(); |
| 301 | + } |
| 302 | + } catch (IOException e) { |
| 303 | + throw new TranscriptRetrievalException("Failed to retrieve data from YouTube", e); |
| 304 | + } |
| 305 | + throw new TranscriptRetrievalException("Failed to retrieve data from YouTube"); |
| 306 | + } |
| 307 | +} |
| 308 | +``` |
| 309 | +After implementing your custom `YouTubeClient` you will need to pass it to `TranscriptApiFactory` `createWithClient` method. |
| 310 | + |
| 311 | +```java |
| 312 | +YoutubeClient okHttpClient = new OkHttpYoutubeClient(); |
| 313 | +YoutubeTranscriptApi youtubeTranscriptApi = TranscriptApiFactory.createWithClient(okHttpClient); |
260 | 314 | ```
|
261 | 315 |
|
262 | 316 | ### Cookies
|
|
0 commit comments