Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

public abstract class Extractor {
Expand All @@ -24,7 +23,7 @@ public abstract class Extractor {
private final LinkHandler linkHandler;

@Nullable
private Localization forcedLocalization = null;
private Locale forcedLocale = null;
@Nullable
private ContentCountry forcedContentCountry = null;

Expand Down Expand Up @@ -128,17 +127,17 @@ public Downloader getDownloader() {
// Localization
//////////////////////////////////////////////////////////////////////////*/

public void forceLocalization(final Localization localization) {
this.forcedLocalization = localization;
public void forceLocale(final Locale locale) {
this.forcedLocale = locale;
}

public void forceContentCountry(final ContentCountry contentCountry) {
this.forcedContentCountry = contentCountry;
}

@Nonnull
public Localization getExtractorLocalization() {
return forcedLocalization == null ? getService().getLocalization() : forcedLocalization;
public Locale getExtractorLocale() {
return forcedLocale == null ? getService().getLocale() : forcedLocale;
}

@Nonnull
Expand All @@ -149,6 +148,6 @@ public ContentCountry getExtractorContentCountry() {

@Nonnull
public TimeAgoParser getTimeAgoParser() {
return getService().getTimeAgoParser(getExtractorLocalization());
return getService().getTimeAgoParser(getExtractorLocale());
}
}
50 changes: 20 additions & 30 deletions extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,34 @@
import org.schabi.newpipe.extractor.downloader.Downloader;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Locale;

/**
* Provides access to streaming services supported by NewPipe.
*/
public final class NewPipe {
private static Downloader downloader;
private static Localization preferredLocalization;
private static Locale preferredLocale;
private static ContentCountry preferredContentCountry;

private NewPipe() {
}

public static void init(final Downloader d) {
init(d, Localization.DEFAULT);
init(d, Locale.UK);
}

public static void init(final Downloader d, final Localization l) {
init(d, l, l.getCountryCode().isEmpty()
? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()));
public static void init(final Downloader d, final Locale l) {
init(d, l, ContentCountry.fromLocale(l));
}

public static void init(final Downloader d, final Localization l, final ContentCountry c) {
public static void init(final Downloader d, final Locale l, final ContentCountry c) {
downloader = d;
preferredLocalization = l;
preferredLocale = l;
preferredContentCountry = c;
}

Expand Down Expand Up @@ -97,39 +95,31 @@ public static StreamingService getServiceByUrl(final String url) throws Extracti
// Localization
//////////////////////////////////////////////////////////////////////////*/

public static void setupLocalization(final Localization thePreferredLocalization) {
setupLocalization(thePreferredLocalization, null);
public static void setupLocalization(final Locale locale) {
setupLocalization(locale, null);
}

public static void setupLocalization(
final Localization thePreferredLocalization,
@Nullable final ContentCountry thePreferredContentCountry) {
NewPipe.preferredLocalization = thePreferredLocalization;

if (thePreferredContentCountry != null) {
NewPipe.preferredContentCountry = thePreferredContentCountry;
} else {
NewPipe.preferredContentCountry = thePreferredLocalization.getCountryCode().isEmpty()
? ContentCountry.DEFAULT
: new ContentCountry(thePreferredLocalization.getCountryCode());
}
public static void setupLocalization(final Locale locale,
@Nullable final ContentCountry country) {
preferredLocale = locale;
preferredContentCountry = country != null ? country : ContentCountry.fromLocale(locale);
}

@Nonnull
public static Localization getPreferredLocalization() {
return preferredLocalization == null ? Localization.DEFAULT : preferredLocalization;
public static Locale getPreferredLocale() {
return preferredLocale == null ? Locale.UK : preferredLocale;
}

public static void setPreferredLocalization(final Localization preferredLocalization) {
NewPipe.preferredLocalization = preferredLocalization;
public static void setPreferredLocale(final Locale locale) {
preferredLocale = locale;
}

@Nonnull
public static ContentCountry getPreferredContentCountry() {
return preferredContentCountry == null ? ContentCountry.DEFAULT : preferredContentCountry;
}

public static void setPreferredContentCountry(final ContentCountry preferredContentCountry) {
NewPipe.preferredContentCountry = preferredContentCountry;
public static void setPreferredContentCountry(final ContentCountry contentCountry) {
preferredContentCountry = contentCountry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
Expand All @@ -27,6 +26,7 @@
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/*
* Copyright (C) 2018 Christian Schabesberger <[email protected]>
Expand Down Expand Up @@ -344,44 +344,40 @@ public final LinkType getLinkTypeByUrl(final String url) throws ParsingException
/**
* Returns a list of localizations that this service supports.
*/
public List<Localization> getSupportedLocalizations() {
return Collections.singletonList(Localization.DEFAULT);
public List<Locale> getSupportedLocales() {
return List.of(Locale.UK);
}

/**
* Returns a list of countries that this service supports.<br>
*/
public List<ContentCountry> getSupportedCountries() {
return Collections.singletonList(ContentCountry.DEFAULT);
return List.of(ContentCountry.DEFAULT);
}

/**
* Returns the localization that should be used in this service. It will get which localization
* the user prefer (using {@link NewPipe#getPreferredLocalization()}), then it will:
* Returns the localization that should be used in this service. It will get which locale
* the user prefers (using {@link NewPipe#getPreferredLocale()}), then it will:
* <ul>
* <li>Check if the exactly localization is supported by this service.</li>
* <li>Check if the exact locale is supported by this service.</li>
* <li>If not, check if a less specific localization is available, using only the language
* code.</li>
* <li>Fallback to the {@link Localization#DEFAULT default} localization.</li>
* <li>Fallback to the {@link Locale#UK default} locale.</li>
* </ul>
*/
public Localization getLocalization() {
final Localization preferredLocalization = NewPipe.getPreferredLocalization();

// Check the localization's language and country
if (getSupportedLocalizations().contains(preferredLocalization)) {
return preferredLocalization;
}

// Fallback to the first supported language that matches the preferred language
for (final Localization supportedLanguage : getSupportedLocalizations()) {
if (supportedLanguage.getLanguageCode()
.equals(preferredLocalization.getLanguageCode())) {
return supportedLanguage;
}
public Locale getLocale() {
final var preferredLocale = NewPipe.getPreferredLocale();
final var supportedLocales = getSupportedLocales();
if (supportedLocales.contains(preferredLocale)) {
return preferredLocale;
} else {
return supportedLocales.stream()
// Fallback to the first supported language that matches the preferred
// language
.filter(locale ->
preferredLocale.getLanguage().equals(locale.getLanguage()))
.findFirst().orElse(Locale.UK);
}

return Localization.DEFAULT;
}

/**
Expand All @@ -405,32 +401,18 @@ public ContentCountry getContentCountry() {
/**
* Get an instance of the time ago parser using the patterns related to the passed localization.
* <br><br>
* Just like {@link #getLocalization()}, it will also try to fallback to a less specific
* Just like {@link #getLocale()}, it will also try to fallback to a less specific
* localization if the exact one is not available/supported.
*
* @throws IllegalArgumentException if the localization is not supported (parsing patterns are
* not present).
*/
public TimeAgoParser getTimeAgoParser(final Localization localization) {
final TimeAgoParser targetParser = TimeAgoPatternsManager.getTimeAgoParserFor(localization);

public TimeAgoParser getTimeAgoParser(final Locale locale) {
final var targetParser = TimeAgoPatternsManager.getTimeAgoParserFor(locale);
if (targetParser != null) {
return targetParser;
}

if (!localization.getCountryCode().isEmpty()) {
final Localization lessSpecificLocalization
= new Localization(localization.getLanguageCode());
final TimeAgoParser lessSpecificParser
= TimeAgoPatternsManager.getTimeAgoParserFor(lessSpecificLocalization);

if (lessSpecificParser != null) {
return lessSpecificParser;
}
}

throw new IllegalArgumentException(
"Localization is not supported (\"" + localization + "\")");
throw new IllegalArgumentException("Locale is not supported (\"" + locale + "\")");
}

}
Loading
Loading