2017-08-12 17:29:28 +02:00
|
|
|
package org.schabi.newpipe.extractor.kiosk;
|
|
|
|
|
2017-09-23 15:47:48 +02:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
2017-08-12 17:29:28 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
2019-04-28 22:03:16 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
|
|
|
|
import org.schabi.newpipe.extractor.localization.ContentCountry;
|
|
|
|
import org.schabi.newpipe.extractor.localization.Localization;
|
2017-08-12 17:29:28 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
import javax.annotation.Nullable;
|
2017-09-23 15:47:48 +02:00
|
|
|
import java.io.IOException;
|
2017-08-12 17:29:28 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2017-08-12 21:10:21 +02:00
|
|
|
public class KioskList {
|
2019-04-28 22:03:16 +02:00
|
|
|
|
2017-09-23 15:47:48 +02:00
|
|
|
public interface KioskExtractorFactory {
|
|
|
|
KioskExtractor createNewKiosk(final StreamingService streamingService,
|
2018-09-15 21:47:53 +02:00
|
|
|
final String url,
|
2019-04-28 22:03:16 +02:00
|
|
|
final String kioskId)
|
2017-09-23 15:47:48 +02:00
|
|
|
throws ExtractionException, IOException;
|
|
|
|
}
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
private final StreamingService service;
|
2017-11-30 11:34:26 +01:00
|
|
|
private final HashMap<String, KioskEntry> kioskList = new HashMap<>();
|
2017-09-23 15:47:48 +02:00
|
|
|
private String defaultKiosk = null;
|
2017-08-12 17:29:28 +02:00
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
@Nullable private Localization forcedLocalization;
|
|
|
|
@Nullable private ContentCountry forcedContentCountry;
|
|
|
|
|
2017-08-12 17:29:28 +02:00
|
|
|
private class KioskEntry {
|
2018-07-13 18:02:40 +02:00
|
|
|
public KioskEntry(KioskExtractorFactory ef, ListLinkHandlerFactory h) {
|
2017-09-23 15:47:48 +02:00
|
|
|
extractorFactory = ef;
|
2018-07-01 18:50:21 +02:00
|
|
|
handlerFactory = h;
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
2017-11-30 11:34:26 +01:00
|
|
|
final KioskExtractorFactory extractorFactory;
|
2018-07-13 18:02:40 +02:00
|
|
|
final ListLinkHandlerFactory handlerFactory;
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 22:03:16 +02:00
|
|
|
public KioskList(StreamingService service) {
|
|
|
|
this.service = service;
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-13 18:02:40 +02:00
|
|
|
public void addKioskEntry(KioskExtractorFactory extractorFactory, ListLinkHandlerFactory handlerFactory, String id)
|
2017-08-12 17:29:28 +02:00
|
|
|
throws Exception {
|
2017-09-25 12:43:40 +02:00
|
|
|
if(kioskList.get(id) != null) {
|
|
|
|
throw new Exception("Kiosk with type " + id + " already exists.");
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
2018-07-08 15:27:35 +02:00
|
|
|
kioskList.put(id, new KioskEntry(extractorFactory, handlerFactory));
|
2017-09-23 15:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setDefaultKiosk(String kioskType) {
|
|
|
|
defaultKiosk = kioskType;
|
|
|
|
}
|
|
|
|
|
2019-01-28 12:18:16 +01:00
|
|
|
public KioskExtractor getDefaultKioskExtractor()
|
|
|
|
throws ExtractionException, IOException {
|
|
|
|
return getDefaultKioskExtractor("");
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
public KioskExtractor getDefaultKioskExtractor(String nextPageUrl)
|
2017-09-23 15:47:48 +02:00
|
|
|
throws ExtractionException, IOException {
|
2018-10-05 16:01:10 +02:00
|
|
|
return getDefaultKioskExtractor(nextPageUrl, NewPipe.getPreferredLocalization());
|
|
|
|
}
|
|
|
|
|
|
|
|
public KioskExtractor getDefaultKioskExtractor(String nextPageUrl, Localization localization)
|
|
|
|
throws ExtractionException, IOException {
|
2017-09-23 15:47:48 +02:00
|
|
|
if(defaultKiosk != null && !defaultKiosk.equals("")) {
|
2018-10-05 16:01:10 +02:00
|
|
|
return getExtractorById(defaultKiosk, nextPageUrl, localization);
|
2017-09-23 15:47:48 +02:00
|
|
|
} else {
|
|
|
|
if(!kioskList.isEmpty()) {
|
|
|
|
// if not set get any entry
|
|
|
|
Object[] keySet = kioskList.keySet().toArray();
|
2018-10-05 16:01:10 +02:00
|
|
|
return getExtractorById(keySet[0].toString(), nextPageUrl, localization);
|
2017-09-23 15:47:48 +02:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-25 12:43:40 +02:00
|
|
|
public String getDefaultKioskId() {
|
2017-09-23 15:59:00 +02:00
|
|
|
return defaultKiosk;
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
public KioskExtractor getExtractorById(String kioskId, String nextPageUrl)
|
2017-09-23 15:47:48 +02:00
|
|
|
throws ExtractionException, IOException {
|
2018-10-05 16:01:10 +02:00
|
|
|
return getExtractorById(kioskId, nextPageUrl, NewPipe.getPreferredLocalization());
|
|
|
|
}
|
|
|
|
|
|
|
|
public KioskExtractor getExtractorById(String kioskId, String nextPageUrl, Localization localization)
|
|
|
|
throws ExtractionException, IOException {
|
2017-09-25 12:43:40 +02:00
|
|
|
KioskEntry ke = kioskList.get(kioskId);
|
2017-08-12 17:29:28 +02:00
|
|
|
if(ke == null) {
|
2017-09-25 12:43:40 +02:00
|
|
|
throw new ExtractionException("No kiosk found with the type: " + kioskId);
|
2017-08-12 17:29:28 +02:00
|
|
|
} else {
|
2019-04-28 22:03:16 +02:00
|
|
|
final KioskExtractor kioskExtractor = ke.extractorFactory.createNewKiosk(service,
|
|
|
|
ke.handlerFactory.fromId(kioskId).getUrl(), kioskId);
|
|
|
|
|
|
|
|
if (forcedLocalization != null) kioskExtractor.forceLocalization(forcedLocalization);
|
|
|
|
if (forcedContentCountry != null) kioskExtractor.forceContentCountry(forcedContentCountry);
|
|
|
|
|
|
|
|
return kioskExtractor;
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 21:44:24 +02:00
|
|
|
public Set<String> getAvailableKiosks() {
|
2017-08-12 17:29:28 +02:00
|
|
|
return kioskList.keySet();
|
|
|
|
}
|
|
|
|
|
2018-02-24 22:20:50 +01:00
|
|
|
public KioskExtractor getExtractorByUrl(String url, String nextPageUrl)
|
2018-10-05 16:01:10 +02:00
|
|
|
throws ExtractionException, IOException{
|
|
|
|
return getExtractorByUrl(url, nextPageUrl, NewPipe.getPreferredLocalization());
|
|
|
|
}
|
|
|
|
|
|
|
|
public KioskExtractor getExtractorByUrl(String url, String nextPageUrl, Localization localization)
|
2017-09-23 15:47:48 +02:00
|
|
|
throws ExtractionException, IOException {
|
2017-08-12 17:29:28 +02:00
|
|
|
for(Map.Entry<String, KioskEntry> e : kioskList.entrySet()) {
|
|
|
|
KioskEntry ke = e.getValue();
|
2018-07-01 18:50:21 +02:00
|
|
|
if(ke.handlerFactory.acceptUrl(url)) {
|
2018-12-26 08:20:43 +01:00
|
|
|
return getExtractorById(ke.handlerFactory.getId(url), nextPageUrl, localization);
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new ExtractionException("Could not find a kiosk that fits to the url: " + url);
|
|
|
|
}
|
2017-09-23 17:14:21 +02:00
|
|
|
|
2018-07-13 19:08:50 +02:00
|
|
|
public ListLinkHandlerFactory getListLinkHandlerFactoryByType(String type) {
|
2018-07-01 18:50:21 +02:00
|
|
|
return kioskList.get(type).handlerFactory;
|
2017-09-23 17:14:21 +02:00
|
|
|
}
|
2019-04-28 22:03:16 +02:00
|
|
|
|
|
|
|
public void forceLocalization(@Nullable Localization localization) {
|
|
|
|
this.forcedLocalization = localization;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void forceContentCountry(@Nullable ContentCountry contentCountry) {
|
|
|
|
this.forcedContentCountry = contentCountry;
|
|
|
|
}
|
2017-08-12 17:29:28 +02:00
|
|
|
}
|