From 78d461ed89b7b96e61d1447ee4ac62a0c50f7ba6 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sat, 23 Sep 2017 15:47:48 +0200 Subject: [PATCH] improve kiosk function improve kiosk function --- .../newpipe/extractor/kiosk/KioskInfo.java | 17 ++++-- .../newpipe/extractor/kiosk/KioskList.java | 57 +++++++++++++++--- .../soundcloud/SoundcloudService.java | 17 +++++- .../services/youtube/YoutubeService.java | 13 ++++- .../SoundcloudChartsExtractorTest.java | 2 +- .../youtube/YoutubeSearchEngineAllTest.java | 1 - .../services/youtube/YoutubeServiceTest.java | 58 +++++++++++++++++++ .../youtube/YoutubeTrendingExtractorTest.java | 5 +- 8 files changed, 144 insertions(+), 26 deletions(-) create mode 100644 src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeServiceTest.java 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 c3fee602..baf9bef4 100644 --- a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java +++ b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java @@ -20,10 +20,7 @@ package org.schabi.newpipe.extractor.kiosk; * along with NewPipe. If not, see . */ -import org.schabi.newpipe.extractor.ListInfo; -import org.schabi.newpipe.extractor.NewPipe; -import org.schabi.newpipe.extractor.ServiceList; -import org.schabi.newpipe.extractor.StreamingService; +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; @@ -33,6 +30,16 @@ import java.io.IOException; public class KioskInfo extends ListInfo { public String type; + public static ListExtractor.NextItemsResult getMoreItems(ServiceList serviceItem, String url, String nextStreamsUrl) throws IOException, ExtractionException { + return getMoreItems(serviceItem.getService(), url, nextStreamsUrl); + } + + public static ListExtractor.NextItemsResult getMoreItems(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException { + KioskList kl = service.getKioskList(); + KioskExtractor extractor = kl.getExtryctorByUrl(url, nextStreamsUrl); + return extractor.getNextStreams(); + } + public static KioskInfo getInfo(String url, String contentCountry) throws IOException, ExtractionException { return getInfo(NewPipe.getServiceByUrl(url), url, contentCountry); @@ -48,7 +55,7 @@ public class KioskInfo extends ListInfo { String url, String contentCountry) throws IOException, ExtractionException { KioskList kl = service.getKioskList(); - KioskExtractor extractor = kl.getExtryctorByUrl(url); + KioskExtractor extractor = kl.getExtryctorByUrl(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 c467a0b0..c2335dd9 100644 --- a/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java +++ b/src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java @@ -1,22 +1,36 @@ package org.schabi.newpipe.extractor.kiosk; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.ServiceList; +import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import java.io.IOError; +import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.Random; import java.util.Set; public class KioskList { + public interface KioskExtractorFactory { + KioskExtractor createNewKiosk(final StreamingService streamingService, + final String url, + final String nextStreamUrl) + throws ExtractionException, IOException; + } + private int service_id; private HashMap kioskList = new HashMap<>(); + private String defaultKiosk = null; private class KioskEntry { - public KioskEntry(KioskExtractor e, UrlIdHandler h) { - extractor = e; + public KioskEntry(KioskExtractorFactory ef, UrlIdHandler h) { + extractorFactory = ef; handler = h; } - KioskExtractor extractor; + KioskExtractorFactory extractorFactory; UrlIdHandler handler; } @@ -24,20 +38,44 @@ public class KioskList { this.service_id = service_id; } - public void addKioskEntry(KioskExtractor extractor, UrlIdHandler handler) + public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler) 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."); } - kioskList.put(extractor.getType(), new KioskEntry(extractor, handler)); + kioskList.put(extractor.getType(), new KioskEntry(extractorFactory, handler)); } - public KioskExtractor getExtractorByType(String kioskType) throws ExtractionException { + public void setDefaultKiosk(String kioskType) { + defaultKiosk = kioskType; + } + + public KioskExtractor getDefaultKioskExtractor(String nextStreamUrl) + throws ExtractionException, IOException { + if(defaultKiosk != null && !defaultKiosk.equals("")) { + return getExtractorByType(defaultKiosk, nextStreamUrl); + } else { + if(!kioskList.isEmpty()) { + // if not set get any entry + Object[] keySet = kioskList.keySet().toArray(); + return getExtractorByType(keySet[0].toString(), nextStreamUrl); + } else { + return null; + } + } + } + + public KioskExtractor getExtractorByType(String kioskType, String nextStreamsUrl) + throws ExtractionException, IOException { KioskEntry ke = kioskList.get(kioskType); if(ke == null) { throw new ExtractionException("No kiosk found with the type: " + kioskType); } else { - return ke.extractor; + return ke.extractorFactory.createNewKiosk(NewPipe.getService(service_id), + ke.handler.getUrl(""), + nextStreamsUrl); } } @@ -45,11 +83,12 @@ public class KioskList { return kioskList.keySet(); } - public KioskExtractor getExtryctorByUrl(String url) throws ExtractionException { + public KioskExtractor getExtryctorByUrl(String url, String nextStreamsUrl) + throws ExtractionException, IOException { for(Map.Entry e : kioskList.entrySet()) { KioskEntry ke = e.getValue(); if(ke.handler.acceptUrl(url)) { - return getExtractorByType(e.getKey()); + return getExtractorByType(e.getKey(), nextStreamsUrl); } } throw new ExtractionException("Could not find a kiosk that fits to the 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 c2bb4f9c..a56e1958 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 @@ -7,6 +7,7 @@ import org.schabi.newpipe.extractor.SuggestionExtractor; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.search.SearchEngine; @@ -64,10 +65,20 @@ public class SoundcloudService extends StreamingService { KioskList list = new KioskList(getServiceId()); // add kiosks here e.g.: - SoundcloudChartsUrlIdHandler h = new SoundcloudChartsUrlIdHandler(); + final SoundcloudChartsUrlIdHandler h = new SoundcloudChartsUrlIdHandler(); try { - list.addKioskEntry(new SoundcloudChartsExtractor(this, h.getUrl("Top 50"), null), h); - list.addKioskEntry(new SoundcloudChartsExtractor(this, h.getUrl("New & hot"), null), h); + 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); + } + }, h); + 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); + } + }, h); } 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 c448d98c..a578f522 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 @@ -5,6 +5,7 @@ import org.schabi.newpipe.extractor.SuggestionExtractor; import org.schabi.newpipe.extractor.UrlIdHandler; import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.kiosk.KioskList; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.search.SearchEngine; @@ -81,13 +82,19 @@ public class YoutubeService extends StreamingService { } @Override - public KioskList getKioskList() throws ExtractionException { + public KioskList getKioskList() + throws ExtractionException { KioskList list = new KioskList(getServiceId()); // add kiosks here e.g.: - YoutubeTrendingUrlIdHandler h = new YoutubeTrendingUrlIdHandler(); try { - list.addKioskEntry(new YoutubeTrendingExtractor(this, h.getUrl(""), null), h); + list.addKioskEntry(new KioskList.KioskExtractorFactory() { + @Override + public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) + throws ExtractionException, IOException { + return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl); + } + }, new YoutubeTrendingUrlIdHandler()); } catch (Exception e) { throw new ExtractionException(e); } diff --git a/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java b/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java index 061c37ff..25357783 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java @@ -25,7 +25,7 @@ public class SoundcloudChartsExtractorTest { NewPipe.init(Downloader.getInstance()); extractor = SoundCloud.getService() .getKioskList() - .getExtractorByType("Top 50"); + .getExtractorByType("Top 50", null); extractor.fetchPage(); } diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java index 640c9770..db0ede95 100644 --- a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java @@ -12,7 +12,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.schabi.newpipe.extractor.ServiceList.YouTube; - /* * Created by Christian Schabesberger on 29.12.15. * diff --git a/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeServiceTest.java b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeServiceTest.java new file mode 100644 index 00000000..a29b13df --- /dev/null +++ b/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeServiceTest.java @@ -0,0 +1,58 @@ +package org.schabi.newpipe.extractor.services.youtube; + +/* + * Created by Christian Schabesberger on 29.12.15. + * + * Copyright (C) Christian Schabesberger 2015 + * YoutubeSearchEngineStreamTest.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.kiosk.KioskList; +import org.schabi.newpipe.extractor.search.SearchEngine; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.schabi.newpipe.extractor.ServiceList.YouTube; + +/** + * Test for {@link YoutubeService} + */ +public class YoutubeServiceTest { + StreamingService service; + KioskList kioskList; + + @Before + public void setUp() throws Exception { + NewPipe.init(Downloader.getInstance()); + service = YouTube.getService(); + kioskList = service.getKioskList(); + } + + @Test + public void testGetKioskAvailableKiosks() throws Exception { + assertFalse("No kiosk got returned", kioskList.getAvailableKisokTypes().isEmpty()); + } + + @Test + public void testGetDefaultKisok() throws Exception { + assertEquals(kioskList.getDefaultKioskExtractor(null).getName(), "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 4dd9879f..d97f052b 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 @@ -23,13 +23,10 @@ package org.schabi.newpipe.extractor.services.youtube; import org.junit.Before; import org.junit.Test; import org.schabi.newpipe.Downloader; -import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItemCollector; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.kiosk.KioskExtractor; -import java.util.List; - import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -49,7 +46,7 @@ public class YoutubeTrendingExtractorTest { NewPipe.init(Downloader.getInstance()); extractor = YouTube.getService() .getKioskList() - .getExtractorByType("Trending"); + .getExtractorByType("Trending", null); extractor.fetchPage(); }