add support for yt content country

This commit is contained in:
Christian Schabesberger 2018-10-06 12:22:37 +02:00
parent 3a865749e8
commit 0c3dc86b07
4 changed files with 7 additions and 31 deletions

View File

@ -30,7 +30,6 @@ import org.schabi.newpipe.extractor.utils.Localization;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> { public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> {
private String contentCountry = null;
private final String id; private final String id;
public KioskExtractor(StreamingService streamingService, public KioskExtractor(StreamingService streamingService,
@ -41,17 +40,6 @@ public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> {
this.id = kioskId; this.id = kioskId;
} }
/**
* For certain Websites the content of a kiosk will be different depending
* on the country you want to poen the website in. Therefore you should
* set the contentCountry.
* @param contentCountry Set the country decoded as Country Code: http://www.1728.org/countries.htm
*/
public void setContentCountry(String contentCountry) {
this.contentCountry = contentCountry;
}
@Nonnull @Nonnull
@Override @Override
public String getId() { public String getId() {
@ -69,9 +57,4 @@ public abstract class KioskExtractor extends ListExtractor<StreamInfoItem> {
@Nonnull @Nonnull
@Override @Override
public abstract String getName() throws ParsingException; public abstract String getName() throws ParsingException;
public String getContentCountry() {
return contentCountry;
}
} }

View File

@ -53,22 +53,16 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Override @Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException { public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
final String site; final String site;
final String url = getUrl(); final String url = getUrl() + "?gl="+ getLocalization().getCountry();
final String contentCountry = getLocalization().getCountry();
//String url = builder.build().toString(); //String url = builder.build().toString();
//if we've been passed a valid language code, append it to the URL //if we've been passed a valid language code, append it to the URL
if (!contentCountry.isEmpty()) { site = downloader.download(url, getLocalization());
//assert Pattern.matches("[a-z]{2}(-([A-Z]{2}|[0-9]{1,3}))?", languageCode);
site = downloader.download(url, getLocalization());
} else {
site = downloader.download(url);
}
doc = Jsoup.parse(site, url); doc = Jsoup.parse(site, url);
} }
@Override @Override
public String getSearchSuggestion() throws ParsingException { public String getSearchSuggestion() {
final Element el = doc.select("div[class*=\"spell-correction\"]").first(); final Element el = doc.select("div[class*=\"spell-correction\"]").first();
if (el != null) { if (el != null) {
return el.select("a").first().text(); return el.select("a").first().text();
@ -79,13 +73,13 @@ public class YoutubeSearchExtractor extends SearchExtractor {
@Nonnull @Nonnull
@Override @Override
public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionException { public InfoItemsPage<InfoItem> getInitialPage() throws ExtractionException {
return new InfoItemsPage<>(collectItems(doc), getNextPageUrl()); return new InfoItemsPage<>(collectItems(doc), getNextPageUrl());
} }
@Override @Override
public String getNextPageUrl() throws ExtractionException { public String getNextPageUrl() throws ExtractionException {
return getUrl() + "&page=" + Integer.toString( 2); return getUrl() + "&page=" + 2;
} }
@Override @Override
@ -104,7 +98,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
.getQuery()) .getQuery())
.get("page")); .get("page"));
return currentUrl.replace("&page=" + Integer.toString( pageNr), return currentUrl.replace("&page=" + pageNr,
"&page=" + Integer.toString(pageNr + 1)); "&page=" + Integer.toString(pageNr + 1));
} }

View File

@ -50,7 +50,7 @@ public class YoutubeTrendingExtractor extends KioskExtractor {
@Override @Override
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException { public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
final String contentCountry = getContentCountry(); final String contentCountry = getLocalization().getCountry();
String url = getUrl(); String url = getUrl();
if(contentCountry != null && !contentCountry.isEmpty()) { if(contentCountry != null && !contentCountry.isEmpty()) {
url += "?gl=" + contentCountry; url += "?gl=" + contentCountry;

View File

@ -51,7 +51,6 @@ public class YoutubeTrendingExtractorTest {
.getKioskList() .getKioskList()
.getExtractorById("Trending", null); .getExtractorById("Trending", null);
extractor.fetchPage(); extractor.fetchPage();
extractor.setContentCountry("de");
} }
@Test @Test