NewPipe requires a Downloader implementation to fetch web pages. Here’s a complete implementation using OkHttp:
DownloaderImpl.java
import org.schabi.newpipe.extractor.downloader.Downloader;import org.schabi.newpipe.extractor.downloader.Request;import org.schabi.newpipe.extractor.downloader.Response;import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;import java.io.IOException;import java.util.List;import java.util.Map;import java.util.concurrent.TimeUnit;import okhttp3.ConnectionSpec;import okhttp3.OkHttpClient;import okhttp3.RequestBody;import okhttp3.ResponseBody;public class DownloaderImpl extends Downloader { private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0"; private final OkHttpClient client; public DownloaderImpl() { this.client = new OkHttpClient.Builder() .readTimeout(30, TimeUnit.SECONDS) // Required for certain services like Bandcamp .connectionSpecs(List.of(ConnectionSpec.RESTRICTED_TLS)) .build(); } @Override public Response execute(Request request) throws IOException, ReCaptchaException { final String httpMethod = request.httpMethod(); final String url = request.url(); final Map<String, List<String>> headers = request.headers(); final byte[] dataToSend = request.dataToSend(); RequestBody requestBody = null; if (dataToSend != null) { requestBody = RequestBody.create(dataToSend); } final okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder() .method(httpMethod, requestBody) .url(url) .addHeader("User-Agent", USER_AGENT); headers.forEach((headerName, headerValueList) -> { requestBuilder.removeHeader(headerName); headerValueList.forEach(headerValue -> requestBuilder.addHeader(headerName, headerValue)); }); try (okhttp3.Response response = client.newCall(requestBuilder.build()).execute()) { if (response.code() == 429) { throw new ReCaptchaException("reCaptcha Challenge requested", url); } String responseBodyToReturn = null; try (ResponseBody body = response.body()) { if (body != null) { responseBodyToReturn = body.string(); } } return new Response( response.code(), response.message(), response.headers().toMultimap(), responseBodyToReturn, response.request().url().toString()); } }}
Don’t forget to add OkHttp to your project dependencies: implementation("com.squareup.okhttp3:okhttp:4.12.0")
2
Initialize NewPipe
Before extracting any data, initialize NewPipe with your downloader implementation:
import org.schabi.newpipe.extractor.NewPipe;import org.schabi.newpipe.extractor.localization.Localization;import org.schabi.newpipe.extractor.localization.ContentCountry;// Basic initializationNewPipe.init(new DownloaderImpl());// Or with localization (optional)NewPipe.init( new DownloaderImpl(), Localization.fromLocale(Locale.US), new ContentCountry("US"));
You only need to initialize NewPipe once in your application lifecycle, typically at startup.
3
Extract Your First Stream
Now you can extract information from any supported URL:
import org.schabi.newpipe.extractor.localization.Localization;import org.schabi.newpipe.extractor.localization.ContentCountry;import java.util.Locale;// Initialize with localizationNewPipe.init( new DownloaderImpl(), Localization.fromLocale(Locale.GERMANY), new ContentCountry("DE"));// Or update localization after initializationNewPipe.setupLocalization( Localization.fromLocale(Locale.FRANCE), new ContentCountry("FR"));// Get current localization settingsLocalization currentLocale = NewPipe.getPreferredLocalization();ContentCountry currentCountry = NewPipe.getPreferredContentCountry();System.out.println("Language: " + currentLocale.getLanguageCode());System.out.println("Country: " + currentCountry.getCountryCode());
Localization affects the language of metadata (titles, descriptions) and can influence search results and trending content. Not all services support all localizations.