add adblocker for youtube red stuff

This commit is contained in:
Christian Schabesberger 2017-02-28 13:05:20 +01:00
parent 8f734737f0
commit 552c70bb0d
5 changed files with 45 additions and 13 deletions

View file

@ -169,6 +169,15 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
return AbstractStreamInfo.StreamType.VIDEO_STREAM; return AbstractStreamInfo.StreamType.VIDEO_STREAM;
} }
@Override
public boolean isAd() throws ParsingException {
if(!li.select("span[class*=\"icon-not-available\"]").isEmpty()) {
return true;
} else {
return false;
}
}
@Override @Override
public String getWebPageUrl() throws ParsingException { public String getWebPageUrl() throws ParsingException {
try { try {
@ -214,9 +223,14 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
@Override @Override
public String getUploadDate() throws ParsingException { public String getUploadDate() throws ParsingException {
try { try {
return li.select("div[class=\"yt-lockup-meta\"]").first() Element meta = li.select("div[class=\"yt-lockup-meta\"]").first();
.select("li").first() Element li = meta.select("li").first();
.text(); if (li == null && meta != null) {
//this means we have a youtube red video
return "";
}else {
return li.text();
}
} catch(Exception e) { } catch(Exception e) {
throw new ParsingException("Could not get uplaod date", e); throw new ParsingException("Could not get uplaod date", e);
} }
@ -231,13 +245,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
.select("li").get(1) .select("li").get(1)
.text(); .text();
} catch (IndexOutOfBoundsException e) { } catch (IndexOutOfBoundsException e) {
if(isLiveStream(li)) {
// -1 for no view count
return -1; return -1;
} else {
throw new ParsingException(
"Could not parse yt-lockup-meta although available: " + getTitle(), e);
}
} }
output = Parser.matchGroup1("([0-9,\\. ]*)", input) output = Parser.matchGroup1("([0-9,\\. ]*)", input)

View file

@ -10,6 +10,7 @@ import org.mozilla.javascript.Function;
import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.ScriptableObject;
import org.schabi.newpipe.extractor.AbstractStreamInfo; import org.schabi.newpipe.extractor.AbstractStreamInfo;
import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.stream_info.AudioStream; import org.schabi.newpipe.extractor.stream_info.AudioStream;
@ -725,7 +726,16 @@ public class YoutubeStreamExtractor extends StreamExtractor {
return new StreamInfoItemExtractor() { return new StreamInfoItemExtractor() {
@Override @Override
public AbstractStreamInfo.StreamType getStreamType() throws ParsingException { public AbstractStreamInfo.StreamType getStreamType() throws ParsingException {
return null; return AbstractStreamInfo.StreamType.VIDEO_STREAM;
}
@Override
public boolean isAd() throws ParsingException {
if(!li.select("span[class*=\"icon-not-available\"]").isEmpty()) {
return true;
} else {
return false;
}
} }
@Override @Override

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.youtube;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.AbstractStreamInfo; import org.schabi.newpipe.extractor.AbstractStreamInfo;
import org.schabi.newpipe.extractor.Parser; import org.schabi.newpipe.extractor.Parser;
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor;
@ -28,7 +29,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
private final Element item; private final Element item;
public YoutubeStreamInfoItemExtractor(Element item) { public YoutubeStreamInfoItemExtractor(Element item) throws FoundAdException {
this.item = item; this.item = item;
} }
@ -161,6 +162,15 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
} }
} }
@Override
public boolean isAd() throws ParsingException {
if(!item.select("span[class*=\"icon-not-available\"]").isEmpty()) {
return true;
} else {
return false;
}
}
private boolean isLiveStream(Element item) { private boolean isLiveStream(Element item) {
Element bla = item.select("span[class*=\"yt-badge-live\"]").first(); Element bla = item.select("span[class*=\"yt-badge-live\"]").first();

View file

@ -43,6 +43,9 @@ public class StreamInfoItemCollector extends InfoItemCollector {
} }
public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws Exception { public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws Exception {
if(extractor.isAd()) {
throw new FoundAdException("Found ad");
}
StreamInfoItem resultItem = new StreamInfoItem(); StreamInfoItem resultItem = new StreamInfoItem();
// importand information // importand information
@ -91,7 +94,7 @@ public class StreamInfoItemCollector extends InfoItemCollector {
try { try {
addItem(extract(extractor)); addItem(extract(extractor));
} catch(FoundAdException ae) { } catch(FoundAdException ae) {
System.out.println("AD_WARNING: " + ae.getMessage()); //System.out.println("AD_WARNING: " + ae.getMessage());
} catch (Exception e) { } catch (Exception e) {
addError(e); addError(e);
} }

View file

@ -32,4 +32,5 @@ public interface StreamInfoItemExtractor {
String getUploadDate() throws ParsingException; String getUploadDate() throws ParsingException;
long getViewCount() throws ParsingException; long getViewCount() throws ParsingException;
String getThumbnailUrl() throws ParsingException; String getThumbnailUrl() throws ParsingException;
boolean isAd() throws ParsingException;
} }