Skip to content

Commit 78d461e

Browse files
committed
improve kiosk function
improve kiosk function
1 parent 82ae729 commit 78d461e

8 files changed

Lines changed: 144 additions & 26 deletions

File tree

src/main/java/org/schabi/newpipe/extractor/kiosk/KioskInfo.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2121
*/
2222

23-
import org.schabi.newpipe.extractor.ListInfo;
24-
import org.schabi.newpipe.extractor.NewPipe;
25-
import org.schabi.newpipe.extractor.ServiceList;
26-
import org.schabi.newpipe.extractor.StreamingService;
23+
import org.schabi.newpipe.extractor.*;
2724
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2825
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2926
import org.schabi.newpipe.extractor.stream.StreamInfoItemCollector;
@@ -33,6 +30,16 @@
3330
public class KioskInfo extends ListInfo {
3431
public String type;
3532

33+
public static ListExtractor.NextItemsResult getMoreItems(ServiceList serviceItem, String url, String nextStreamsUrl) throws IOException, ExtractionException {
34+
return getMoreItems(serviceItem.getService(), url, nextStreamsUrl);
35+
}
36+
37+
public static ListExtractor.NextItemsResult getMoreItems(StreamingService service, String url, String nextStreamsUrl) throws IOException, ExtractionException {
38+
KioskList kl = service.getKioskList();
39+
KioskExtractor extractor = kl.getExtryctorByUrl(url, nextStreamsUrl);
40+
return extractor.getNextStreams();
41+
}
42+
3643
public static KioskInfo getInfo(String url,
3744
String contentCountry) throws IOException, ExtractionException {
3845
return getInfo(NewPipe.getServiceByUrl(url), url, contentCountry);
@@ -48,7 +55,7 @@ public static KioskInfo getInfo(StreamingService service,
4855
String url,
4956
String contentCountry) throws IOException, ExtractionException {
5057
KioskList kl = service.getKioskList();
51-
KioskExtractor extractor = kl.getExtryctorByUrl(url);
58+
KioskExtractor extractor = kl.getExtryctorByUrl(url, null);
5259
return getInfo(extractor, contentCountry);
5360
}
5461

src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,94 @@
11
package org.schabi.newpipe.extractor.kiosk;
22

3+
import org.schabi.newpipe.extractor.NewPipe;
4+
import org.schabi.newpipe.extractor.ServiceList;
5+
import org.schabi.newpipe.extractor.StreamingService;
36
import org.schabi.newpipe.extractor.UrlIdHandler;
47
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
58

9+
import java.io.IOError;
10+
import java.io.IOException;
611
import java.util.HashMap;
712
import java.util.Map;
13+
import java.util.Random;
814
import java.util.Set;
915

1016
public class KioskList {
17+
public interface KioskExtractorFactory {
18+
KioskExtractor createNewKiosk(final StreamingService streamingService,
19+
final String url,
20+
final String nextStreamUrl)
21+
throws ExtractionException, IOException;
22+
}
23+
1124
private int service_id;
1225
private HashMap<String, KioskEntry> kioskList = new HashMap<>();
26+
private String defaultKiosk = null;
1327

1428
private class KioskEntry {
15-
public KioskEntry(KioskExtractor e, UrlIdHandler h) {
16-
extractor = e;
29+
public KioskEntry(KioskExtractorFactory ef, UrlIdHandler h) {
30+
extractorFactory = ef;
1731
handler = h;
1832
}
19-
KioskExtractor extractor;
33+
KioskExtractorFactory extractorFactory;
2034
UrlIdHandler handler;
2135
}
2236

2337
public KioskList(int service_id) {
2438
this.service_id = service_id;
2539
}
2640

27-
public void addKioskEntry(KioskExtractor extractor, UrlIdHandler handler)
41+
public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler)
2842
throws Exception {
43+
KioskExtractor extractor =
44+
extractorFactory.createNewKiosk(NewPipe.getService(service_id), "", "");
2945
if(kioskList.get(extractor.getType()) != null) {
3046
throw new Exception("Kiosk with type " + extractor.getType() + " already exists.");
3147
}
32-
kioskList.put(extractor.getType(), new KioskEntry(extractor, handler));
48+
kioskList.put(extractor.getType(), new KioskEntry(extractorFactory, handler));
49+
}
50+
51+
public void setDefaultKiosk(String kioskType) {
52+
defaultKiosk = kioskType;
53+
}
54+
55+
public KioskExtractor getDefaultKioskExtractor(String nextStreamUrl)
56+
throws ExtractionException, IOException {
57+
if(defaultKiosk != null && !defaultKiosk.equals("")) {
58+
return getExtractorByType(defaultKiosk, nextStreamUrl);
59+
} else {
60+
if(!kioskList.isEmpty()) {
61+
// if not set get any entry
62+
Object[] keySet = kioskList.keySet().toArray();
63+
return getExtractorByType(keySet[0].toString(), nextStreamUrl);
64+
} else {
65+
return null;
66+
}
67+
}
3368
}
3469

35-
public KioskExtractor getExtractorByType(String kioskType) throws ExtractionException {
70+
public KioskExtractor getExtractorByType(String kioskType, String nextStreamsUrl)
71+
throws ExtractionException, IOException {
3672
KioskEntry ke = kioskList.get(kioskType);
3773
if(ke == null) {
3874
throw new ExtractionException("No kiosk found with the type: " + kioskType);
3975
} else {
40-
return ke.extractor;
76+
return ke.extractorFactory.createNewKiosk(NewPipe.getService(service_id),
77+
ke.handler.getUrl(""),
78+
nextStreamsUrl);
4179
}
4280
}
4381

4482
public Set<String> getAvailableKisokTypes() {
4583
return kioskList.keySet();
4684
}
4785

48-
public KioskExtractor getExtryctorByUrl(String url) throws ExtractionException {
86+
public KioskExtractor getExtryctorByUrl(String url, String nextStreamsUrl)
87+
throws ExtractionException, IOException {
4988
for(Map.Entry<String, KioskEntry> e : kioskList.entrySet()) {
5089
KioskEntry ke = e.getValue();
5190
if(ke.handler.acceptUrl(url)) {
52-
return getExtractorByType(e.getKey());
91+
return getExtractorByType(e.getKey(), nextStreamsUrl);
5392
}
5493
}
5594
throw new ExtractionException("Could not find a kiosk that fits to the url: " + url);

src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudService.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.schabi.newpipe.extractor.UrlIdHandler;
88
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
99
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
10+
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
1011
import org.schabi.newpipe.extractor.kiosk.KioskList;
1112
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
1213
import org.schabi.newpipe.extractor.search.SearchEngine;
@@ -64,10 +65,20 @@ public KioskList getKioskList() throws ExtractionException {
6465
KioskList list = new KioskList(getServiceId());
6566

6667
// add kiosks here e.g.:
67-
SoundcloudChartsUrlIdHandler h = new SoundcloudChartsUrlIdHandler();
68+
final SoundcloudChartsUrlIdHandler h = new SoundcloudChartsUrlIdHandler();
6869
try {
69-
list.addKioskEntry(new SoundcloudChartsExtractor(this, h.getUrl("Top 50"), null), h);
70-
list.addKioskEntry(new SoundcloudChartsExtractor(this, h.getUrl("New & hot"), null), h);
70+
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
71+
@Override
72+
public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) throws ExtractionException, IOException {
73+
return new SoundcloudChartsExtractor(SoundcloudService.this, h.getUrl("Top 50"), nextStreamUrl);
74+
}
75+
}, h);
76+
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
77+
@Override
78+
public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl) throws ExtractionException, IOException {
79+
return new SoundcloudChartsExtractor(SoundcloudService.this, h.getUrl("New & hot"), nextStreamUrl);
80+
}
81+
}, h);
7182
} catch (Exception e) {
7283
throw new ExtractionException(e);
7384
}

src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeService.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.schabi.newpipe.extractor.UrlIdHandler;
66
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
77
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
8+
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
89
import org.schabi.newpipe.extractor.kiosk.KioskList;
910
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
1011
import org.schabi.newpipe.extractor.search.SearchEngine;
@@ -81,13 +82,19 @@ public SuggestionExtractor getSuggestionExtractor() {
8182
}
8283

8384
@Override
84-
public KioskList getKioskList() throws ExtractionException {
85+
public KioskList getKioskList()
86+
throws ExtractionException {
8587
KioskList list = new KioskList(getServiceId());
8688

8789
// add kiosks here e.g.:
88-
YoutubeTrendingUrlIdHandler h = new YoutubeTrendingUrlIdHandler();
8990
try {
90-
list.addKioskEntry(new YoutubeTrendingExtractor(this, h.getUrl(""), null), h);
91+
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
92+
@Override
93+
public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl)
94+
throws ExtractionException, IOException {
95+
return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl);
96+
}
97+
}, new YoutubeTrendingUrlIdHandler());
9198
} catch (Exception e) {
9299
throw new ExtractionException(e);
93100
}

src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChartsExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void setUp() throws Exception {
2525
NewPipe.init(Downloader.getInstance());
2626
extractor = SoundCloud.getService()
2727
.getKioskList()
28-
.getExtractorByType("Top 50");
28+
.getExtractorByType("Top 50", null);
2929
extractor.fetchPage();
3030
}
3131

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import static org.junit.Assert.assertTrue;
1313
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
1414

15-
1615
/*
1716
* Created by Christian Schabesberger on 29.12.15.
1817
*
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package org.schabi.newpipe.extractor.services.youtube;
2+
3+
/*
4+
* Created by Christian Schabesberger on 29.12.15.
5+
*
6+
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
7+
* YoutubeSearchEngineStreamTest.java is part of NewPipe.
8+
*
9+
* NewPipe is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* NewPipe is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
21+
*/
22+
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.schabi.newpipe.Downloader;
26+
import org.schabi.newpipe.extractor.NewPipe;
27+
import org.schabi.newpipe.extractor.StreamingService;
28+
import org.schabi.newpipe.extractor.kiosk.KioskList;
29+
import org.schabi.newpipe.extractor.search.SearchEngine;
30+
31+
import static org.junit.Assert.assertEquals;
32+
import static org.junit.Assert.assertFalse;
33+
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
34+
35+
/**
36+
* Test for {@link YoutubeService}
37+
*/
38+
public class YoutubeServiceTest {
39+
StreamingService service;
40+
KioskList kioskList;
41+
42+
@Before
43+
public void setUp() throws Exception {
44+
NewPipe.init(Downloader.getInstance());
45+
service = YouTube.getService();
46+
kioskList = service.getKioskList();
47+
}
48+
49+
@Test
50+
public void testGetKioskAvailableKiosks() throws Exception {
51+
assertFalse("No kiosk got returned", kioskList.getAvailableKisokTypes().isEmpty());
52+
}
53+
54+
@Test
55+
public void testGetDefaultKisok() throws Exception {
56+
assertEquals(kioskList.getDefaultKioskExtractor(null).getName(), "Trending");
57+
}
58+
}

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeTrendingExtractorTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,10 @@
2323
import org.junit.Before;
2424
import org.junit.Test;
2525
import org.schabi.newpipe.Downloader;
26-
import org.schabi.newpipe.extractor.InfoItem;
2726
import org.schabi.newpipe.extractor.InfoItemCollector;
2827
import org.schabi.newpipe.extractor.NewPipe;
2928
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
3029

31-
import java.util.List;
32-
3330
import static junit.framework.TestCase.assertFalse;
3431
import static org.junit.Assert.assertEquals;
3532
import static org.junit.Assert.assertNotNull;
@@ -49,7 +46,7 @@ public void setUp() throws Exception {
4946
NewPipe.init(Downloader.getInstance());
5047
extractor = YouTube.getService()
5148
.getKioskList()
52-
.getExtractorByType("Trending");
49+
.getExtractorByType("Trending", null);
5350
extractor.fetchPage();
5451
}
5552

0 commit comments

Comments
 (0)