improve kiosk function

improve kiosk function
This commit is contained in:
Christian Schabesberger 2017-09-23 15:47:48 +02:00
parent 82ae72900a
commit 78d461ed89
8 changed files with 144 additions and 26 deletions

View file

@ -20,10 +20,7 @@ package org.schabi.newpipe.extractor.kiosk;
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}

View file

@ -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<String, KioskEntry> 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<String, KioskEntry> 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);

View file

@ -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);
}

View file

@ -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);
}

View file

@ -25,7 +25,7 @@ public class SoundcloudChartsExtractorTest {
NewPipe.init(Downloader.getInstance());
extractor = SoundCloud.getService()
.getKioskList()
.getExtractorByType("Top 50");
.getExtractorByType("Top 50", null);
extractor.fetchPage();
}

View file

@ -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.
*

View file

@ -0,0 +1,58 @@
package org.schabi.newpipe.extractor.services.youtube;
/*
* Created by Christian Schabesberger on 29.12.15.
*
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
* 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 <http://www.gnu.org/licenses/>.
*/
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");
}
}

View file

@ -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();
}