This commit is contained in:
kapodamy 2018-06-27 02:17:22 +00:00 committed by GitHub
commit 0a935fd69c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 76 additions and 14 deletions

View File

@ -23,13 +23,18 @@ package org.schabi.newpipe.extractor;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import java.util.List;
import java.util.Locale;
import javax.annotation.Nullable;
/**
* Provides access to streaming services supported by NewPipe.
*/
public class NewPipe {
private static Downloader downloader = null;
private static String countryLanguage = "";
private static String languageCode = "";
private NewPipe() {
}
@ -95,4 +100,67 @@ public class NewPipe {
return "<unknown>";
}
}
/**
* Sets the language for HTTP requests and which locale variant is preferred.
*
* @param locale Android application context to obtain the language.
* If this parameter is NULL, the device country/language will be used.
* @param defaultCountry Default country code to be used if not possible
* determine the device country code. This parameter can be NULL.
*/
public static void setCountryLanguage(@Nullable Locale locale, @Nullable String defaultCountry) {
if (locale == null) {
// Use device defaults country/language.
// NOTE: the use of "Locale.Category.DISPLAY" is only available on API 24 or higher.
locale = Locale.getDefault();
}
languageCode = locale.getLanguage();
String countryCode = locale.getCountry();
if (isNullorEmpty(countryCode) && !isNullorEmpty(defaultCountry)) {
countryCode = defaultCountry;// Just use a default value
}
StringBuilder language = new StringBuilder(25);
language.append(languageCode);
if (!isNullorEmpty(countryCode)) {
language.append('-')
.append(countryCode)
.append(',')
.append(languageCode)
.append(";q=8.0");
}
countryLanguage = language.append(",*;q=0.3").toString();
}
/**
* Sets the language for HTTP requests and which locale variant is preferred.
*
* @param defaultCountry Default country code to be used if not possible
* determine the device country code. This parameter can be NULL.
*/
public static void setCountryLanguage(@Nullable String defaultCountry) {
setCountryLanguage(null, defaultCountry);
}
/**
* Get the language for HTTP requests
*
* @return The "accept-language" value
*/
public static String getCountryLanguage() {
return countryLanguage;
}
public static String getLanguage() {
return languageCode;
}
private static boolean isNullorEmpty(String str) {
return str == null || str.isEmpty();
}
}

View File

@ -55,7 +55,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
@Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
String channelUrl = super.getUrl() + CHANNEL_URL_PARAMETERS;
String pageContent = downloader.download(channelUrl);
String pageContent = downloader.download(channelUrl, NewPipe.getCountryLanguage());
doc = Jsoup.parse(pageContent, channelUrl);
}
@ -169,7 +169,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
JsonObject ajaxJson;
try {
ajaxJson = JsonParser.object().from(NewPipe.getDownloader().download(pageUrl));
ajaxJson = JsonParser.object().from(NewPipe.getDownloader().download(pageUrl, NewPipe.getCountryLanguage()));
} catch (JsonParserException pe) {
throw new ParsingException("Could not parse json data for next streams", pe);
}

View File

@ -30,7 +30,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
@Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
String pageContent = downloader.download(getUrl());
String pageContent = downloader.download(getUrl(), NewPipe.getCountryLanguage());
doc = Jsoup.parse(pageContent, getUrl());
}
@ -132,7 +132,7 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
JsonObject pageJson;
try {
pageJson = JsonParser.object().from(NewPipe.getDownloader().download(pageUrl));
pageJson = JsonParser.object().from(NewPipe.getDownloader().download(pageUrl, NewPipe.getCountryLanguage()));
} catch (JsonParserException pe) {
throw new ParsingException("Could not parse ajax json", pe);
}

View File

@ -68,15 +68,8 @@ public class YoutubeSearchEngine extends SearchEngine {
break;
}
String site;
//String url = builder.build().toString();
//if we've been passed a valid language code, append it to the URL
if (!languageCode.isEmpty()) {
//assert Pattern.matches("[a-z]{2}(-([A-Z]{2}|[0-9]{1,3}))?", languageCode);
site = downloader.download(url, languageCode);
} else {
site = downloader.download(url);
}
String site = downloader.download(url, NewPipe.getCountryLanguage());
Document doc = Jsoup.parse(site, url);
Element list = doc.select("ol[class=\"item-section\"]").first();

View File

@ -24,6 +24,7 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.Downloader;
import org.schabi.newpipe.extractor.ListUrlIdHandler;
import org.schabi.newpipe.extractor.StreamingService;
@ -55,7 +56,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor {
url += "?gl=" + contentCountry;
}
String pageContent = downloader.download(url);
String pageContent = downloader.download(url, NewPipe.getCountryLanguage());
doc = Jsoup.parse(pageContent, url);
}