From 7fffef5d4f097a03701434f22bf2ace4357c5f5a Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sat, 23 Sep 2017 17:14:21 +0200 Subject: [PATCH] messing kiosk up in order to make it work --- .../extractor/kiosk/KioskExtractor.java | 9 ++- .../newpipe/extractor/kiosk/KioskInfo.java | 5 +- .../newpipe/extractor/kiosk/KioskList.java | 23 +++---- .../soundcloud/SoundcloudChartsExtractor.java | 5 +- .../soundcloud/SoundcloudService.java | 26 ++++++-- .../services/youtube/YoutubeService.java | 6 +- .../youtube/YoutubeTrendingExtractor.java | 9 +-- .../YoutubeTreindingKioskInfoTest.java | 60 +++++++++++++++++++ .../youtube/YoutubeTrendingExtractorTest.java | 1 - .../YoutubeTrendingUrlIdHandlerTest.java | 13 ++-- 10 files changed, 119 insertions(+), 38 deletions(-) create mode 100644 src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTreindingKioskInfoTest.java diff --git a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java index 0081aa0b6..102ed4bfd 100644 --- a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskExtractor.java @@ -29,13 +29,16 @@ import java.io.IOException; public abstract class KioskExtractor extends ListExtractor { private String contentCountry = null; + private String type = null; public KioskExtractor(StreamingService streamingService, String url, - String nextStreamsUrl) + String nextStreamsUrl, + String type) throws IOException, ExtractionException { super(streamingService, url, nextStreamsUrl); this.contentCountry = contentCountry; + this.type = type; } /** @@ -53,7 +56,9 @@ public abstract class KioskExtractor extends ListExtractor { * eg. Trending, Top & Hot, Top last 24 hours * @return type of kiosk */ - public abstract String getType() throws ParsingException; + public String getType() throws ParsingException { + return type; + } @Override public String getId() throws ParsingException { diff --git a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java index 6bbdfbaa2..121502c6c 100644 --- a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java +++ b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java @@ -22,7 +22,6 @@ package org.schabi.newpipe.extractor.kiosk; import org.schabi.newpipe.extractor.*; import org.schabi.newpipe.extractor.exceptions.ExtractionException; -import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; import java.io.IOException; @@ -40,7 +39,7 @@ public class KioskInfo extends ListInfo { String url, String nextStreamsUrl) throws IOException, ExtractionException { KioskList kl = service.getKioskList(); - KioskExtractor extractor = kl.getExtryctorByUrl(url, nextStreamsUrl); + KioskExtractor extractor = kl.getExtractorByUrl(url, nextStreamsUrl); return extractor.getNextStreams(); } @@ -59,7 +58,7 @@ public class KioskInfo extends ListInfo { String url, String contentCountry) throws IOException, ExtractionException { KioskList kl = service.getKioskList(); - KioskExtractor extractor = kl.getExtryctorByUrl(url, null); + KioskExtractor extractor = kl.getExtractorByUrl(url, null); return getInfo(extractor, contentCountry); } diff --git a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java index eb8043ed1..9960be4b7 100644 --- a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java +++ b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java @@ -17,7 +17,8 @@ public class KioskList { public interface KioskExtractorFactory { KioskExtractor createNewKiosk(final StreamingService streamingService, final String url, - final String nextStreamUrl) + final String nextStreamUrl, + final String type) throws ExtractionException, IOException; } @@ -38,14 +39,12 @@ public class KioskList { this.service_id = service_id; } - public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler) + public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler, String type) throws Exception { - KioskExtractor extractor = - extractorFactory.createNewKiosk(NewPipe.getService(service_id), "", ""); - if(kioskList.get(extractor.getType()) != null) { - throw new Exception("Kiosk with type " + extractor.getType() + " already exists."); + if(kioskList.get(type) != null) { + throw new Exception("Kiosk with type " + type + " already exists."); } - kioskList.put(extractor.getType(), new KioskEntry(extractorFactory, handler)); + kioskList.put(type, new KioskEntry(extractorFactory, handler)); } public void setDefaultKiosk(String kioskType) { @@ -78,8 +77,8 @@ public class KioskList { throw new ExtractionException("No kiosk found with the type: " + kioskType); } else { return ke.extractorFactory.createNewKiosk(NewPipe.getService(service_id), - ke.handler.getUrl(""), - nextStreamsUrl); + ke.handler.getUrl(kioskType), + nextStreamsUrl, kioskType); } } @@ -87,7 +86,7 @@ public class KioskList { return kioskList.keySet(); } - public KioskExtractor getExtryctorByUrl(String url, String nextStreamsUrl) + public KioskExtractor getExtractorByUrl(String url, String nextStreamsUrl) throws ExtractionException, IOException { for(Map.Entry e : kioskList.entrySet()) { KioskEntry ke = e.getValue(); @@ -97,4 +96,8 @@ public class KioskList { } throw new ExtractionException("Could not find a kiosk that fits to the url: " + url); } + + public UrlIdHandler getUrlIdHandlerByType(String type) { + return kioskList.get(type).handler; + } } diff --git a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractor.java index 44f1df552..47977756c 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractor.java @@ -14,8 +14,9 @@ import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector; public class SoundcloudChartsExtractor extends KioskExtractor { private String url; - public SoundcloudChartsExtractor(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException { - super(service, url, nextStreamsUrl); + public SoundcloudChartsExtractor(StreamingService service, String url, String nextStreamsUrl, String type) + throws IOException, ExtractionException { + super(service, url, nextStreamsUrl, type); this.url = url; } diff --git a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudService.java b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudService.java index a56e19580..7a845f3e8 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudService.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudService.java @@ -69,16 +69,30 @@ public class SoundcloudService extends StreamingService { try { list.addKioskEntry(new KioskList.KioskExtractorFactory() { @Override - public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) throws ExtractionException, IOException { - return new SoundcloudChartsExtractor(SoundcloudService.this, h.getUrl("Top 50"), nextStreamUrl); + public KioskExtractor createNewKiosk(StreamingService streamingService, + String url, + String nextStreamUrl, + String type) + throws ExtractionException, IOException { + return new SoundcloudChartsExtractor(SoundcloudService.this, + h.getUrl(type), + nextStreamUrl, + type); } - }, h); + }, h, "Top 50"); list.addKioskEntry(new KioskList.KioskExtractorFactory() { @Override - public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) throws ExtractionException, IOException { - return new SoundcloudChartsExtractor(SoundcloudService.this, h.getUrl("New & hot"), nextStreamUrl); + public KioskExtractor createNewKiosk(StreamingService streamingService, + String url, + String nextStreamUrl, + String type) + throws ExtractionException, IOException { + return new SoundcloudChartsExtractor(SoundcloudService.this, + h.getUrl(type), + nextStreamUrl, + type); } - }, h); + }, h, "New & hot"); } catch (Exception e) { throw new ExtractionException(e); } diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java index 71e1733b5..d4c63b259 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java @@ -90,11 +90,11 @@ public class YoutubeService extends StreamingService { try { list.addKioskEntry(new KioskList.KioskExtractorFactory() { @Override - public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) + public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl, String type) throws ExtractionException, IOException { - return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl); + return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl, type); } - }, new YoutubeTrendingUrlIdHandler()); + }, new YoutubeTrendingUrlIdHandler(), "Trending"); list.setDefaultKiosk("Trending"); } catch (Exception e) { throw new ExtractionException(e); diff --git a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractor.java b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractor.java index 8c71f967a..79baa516b 100644 --- a/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractor.java +++ b/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractor.java @@ -35,9 +35,9 @@ public class YoutubeTrendingExtractor extends KioskExtractor { private Document doc; - public YoutubeTrendingExtractor(StreamingService service, String url, String nextStreamsUrl) + public YoutubeTrendingExtractor(StreamingService service, String url, String nextStreamsUrl, String type) throws IOException, ExtractionException { - super(service, url, nextStreamsUrl); + super(service, url, nextStreamsUrl, type); } @Override @@ -54,11 +54,6 @@ public class YoutubeTrendingExtractor extends KioskExtractor { doc = Jsoup.parse(pageContent, url); } - @Override - public String getType() { - return "Trending"; - } - @Override public UrlIdHandler getUrlIdHandler() { return new YoutubeTrendingUrlIdHandler(); diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTreindingKioskInfoTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTreindingKioskInfoTest.java new file mode 100644 index 000000000..533ffac6d --- /dev/null +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTreindingKioskInfoTest.java @@ -0,0 +1,60 @@ +package org.schabi.newpipe.extractor.services.youtube; + +/* + * Created by Christian Schabesberger on 12.08.17. + * + * Copyright (C) Christian Schabesberger 2017 + * YoutubeTreindingKioskInfoTest.java is part of NewPipe. + * + * NewPipe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPipe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPipe. If not, see . + */ + +import org.junit.Before; +import org.junit.Test; +import org.schabi.newpipe.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.UrlIdHandler; +import org.schabi.newpipe.extractor.kiosk.KioskInfo; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.schabi.newpipe.extractor.ServiceList.YouTube; + +/** + * Test for {@link KioskInfo} + */ +public class YoutubeTreindingKioskInfoTest { + KioskInfo kioskInfo; + + @Before + public void setUp() + throws Exception { + NewPipe.init(Downloader.getInstance()); + StreamingService service = YouTube.getService(); + UrlIdHandler urlIdHandler = service.getKioskList().getUrlIdHandlerByType("Trending"); + + kioskInfo = KioskInfo.getInfo(YouTube.getService(), urlIdHandler.getUrl("Trending"), null); + } + + @Test + public void getStreams() { + assertFalse(kioskInfo.related_streams.isEmpty()); + } + + @Test + public void getType() { + assertEquals(kioskInfo.type, "Trending"); + } +} diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractorTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractorTest.java index d97f052b2..bdb82f7fe 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractorTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractorTest.java @@ -47,7 +47,6 @@ public class YoutubeTrendingExtractorTest { extractor = YouTube.getService() .getKioskList() .getExtractorByType("Trending", null); - extractor.fetchPage(); } @Test diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingUrlIdHandlerTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingUrlIdHandlerTest.java index 303d409d6..0c755903d 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingUrlIdHandlerTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingUrlIdHandlerTest.java @@ -24,6 +24,9 @@ import org.junit.Before; import org.junit.Test; import org.schabi.newpipe.Downloader; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.UrlIdHandler; + +import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; @@ -33,21 +36,23 @@ import static org.junit.Assert.assertTrue; * Test for {@link YoutubeTrendingUrlIdHandler} */ public class YoutubeTrendingUrlIdHandlerTest { - private YoutubeTrendingUrlIdHandler urlIdHandler; + private UrlIdHandler urlIdHandler; @Before public void setUp() throws Exception { - urlIdHandler = new YoutubeTrendingUrlIdHandler(); + urlIdHandler = YouTube.getService().getKioskList().getUrlIdHandlerByType("Trending"); NewPipe.init(Downloader.getInstance()); } @Test - public void getUrl() { + public void getUrl() + throws Exception { assertEquals(urlIdHandler.getUrl(""), "https://www.youtube.com/feed/trending"); } @Test - public void getId() { + public void getId() + throws Exception { assertEquals(urlIdHandler.getId(""), "Trending"); }