From 552c70bb0dd46190f73743d3438d1f8cdc46730c Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Tue, 28 Feb 2017 13:05:20 +0100 Subject: [PATCH] add adblocker for youtube red stuff --- .../youtube/YoutubeChannelExtractor.java | 28 ++++++++++++------- .../youtube/YoutubeStreamExtractor.java | 12 +++++++- .../YoutubeStreamInfoItemExtractor.java | 12 +++++++- .../stream_info/StreamInfoItemCollector.java | 5 +++- .../stream_info/StreamInfoItemExtractor.java | 1 + 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java index 518769ba1..0b8fda717 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java @@ -169,6 +169,15 @@ public class YoutubeChannelExtractor extends ChannelExtractor { 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 public String getWebPageUrl() throws ParsingException { try { @@ -214,9 +223,14 @@ public class YoutubeChannelExtractor extends ChannelExtractor { @Override public String getUploadDate() throws ParsingException { try { - return li.select("div[class=\"yt-lockup-meta\"]").first() - .select("li").first() - .text(); + Element meta = li.select("div[class=\"yt-lockup-meta\"]").first(); + Element li = meta.select("li").first(); + if (li == null && meta != null) { + //this means we have a youtube red video + return ""; + }else { + return li.text(); + } } catch(Exception e) { throw new ParsingException("Could not get uplaod date", e); } @@ -231,13 +245,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { .select("li").get(1) .text(); } catch (IndexOutOfBoundsException e) { - if(isLiveStream(li)) { - // -1 for no view count - return -1; - } else { - throw new ParsingException( - "Could not parse yt-lockup-meta although available: " + getTitle(), e); - } + return -1; } output = Parser.matchGroup1("([0-9,\\. ]*)", input) diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index 73e6a55d5..52a42f645 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -10,6 +10,7 @@ import org.mozilla.javascript.Function; import org.mozilla.javascript.ScriptableObject; import org.schabi.newpipe.extractor.AbstractStreamInfo; 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.ReCaptchaException; import org.schabi.newpipe.extractor.stream_info.AudioStream; @@ -725,7 +726,16 @@ public class YoutubeStreamExtractor extends StreamExtractor { return new StreamInfoItemExtractor() { @Override 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 diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamInfoItemExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamInfoItemExtractor.java index 1afb8f57d..44d606e2d 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamInfoItemExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamInfoItemExtractor.java @@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.youtube; import org.jsoup.nodes.Element; import org.schabi.newpipe.extractor.AbstractStreamInfo; 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.stream_info.StreamInfoItemExtractor; @@ -28,7 +29,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { private final Element item; - public YoutubeStreamInfoItemExtractor(Element item) { + public YoutubeStreamInfoItemExtractor(Element item) throws FoundAdException { 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) { Element bla = item.select("span[class*=\"yt-badge-live\"]").first(); diff --git a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java index 5c2a8e74d..03ff7bd83 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemCollector.java @@ -43,6 +43,9 @@ public class StreamInfoItemCollector extends InfoItemCollector { } public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws Exception { + if(extractor.isAd()) { + throw new FoundAdException("Found ad"); + } StreamInfoItem resultItem = new StreamInfoItem(); // importand information @@ -91,7 +94,7 @@ public class StreamInfoItemCollector extends InfoItemCollector { try { addItem(extract(extractor)); } catch(FoundAdException ae) { - System.out.println("AD_WARNING: " + ae.getMessage()); + //System.out.println("AD_WARNING: " + ae.getMessage()); } catch (Exception e) { addError(e); } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemExtractor.java index 4a4080a88..b5432b42d 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/stream_info/StreamInfoItemExtractor.java @@ -32,4 +32,5 @@ public interface StreamInfoItemExtractor { String getUploadDate() throws ParsingException; long getViewCount() throws ParsingException; String getThumbnailUrl() throws ParsingException; + boolean isAd() throws ParsingException; }