Skip to content

Commit 5c66b9d

Browse files
committed
docs: update README.md
1 parent 619d187 commit 5c66b9d

File tree

1 file changed

+61
-7
lines changed

1 file changed

+61
-7
lines changed

README.md

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99

1010
### This library uses undocumented YouTube API, so it's possible that it will stop working at any time. Use at your own risk.
1111

12+
> **Note:** If you want to use this library on Android platform, refer to
13+
> [Android compatibility](#android-compatibility).
14+
1215
## 📖 Introduction
1316

1417
Java library which allows you to retrieve subtitles/transcripts for a YouTube video.
1518
It supports manual and automatically generated subtitles, bulk transcript retrieval for all videos in the playlist or
1619
on the channel and does not use headless browser for scraping.
1720
Inspired by [Python library](https://github.com/jdepoix/youtube-transcript-api).
1821

19-
## 🤖 Features
22+
## ☑️ Features
2023

2124
✅ Manual transcripts retrieval
2225

@@ -146,6 +149,13 @@ TranscriptContent transcriptContent = youtubeTranscriptApi.getTranscript("videoI
146149

147150
For bulk transcript retrieval see [Bulk Transcript Retrieval](#bulk-transcript-retrieval).
148151

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+
149159
## 🔧 Detailed Usage
150160

151161
### Use fallback language
@@ -248,15 +258,59 @@ String formattedContent = jsonFormatter.format(transcriptContent);
248258

249259
By default, `YoutubeTranscriptApi` uses Java 11 HttpClient for making requests to YouTube, if you want to use a
250260
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:
253264

254265
```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+
}
257283

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);
260314
```
261315

262316
### Cookies

0 commit comments

Comments
 (0)