Skip to content

Commit

Permalink
remove type from kiosk and make getName() crawl the translated kiosk …
Browse files Browse the repository at this point in the history
…name
  • Loading branch information
theScrabi committed Sep 25, 2017
1 parent 7beb90b commit 466d87c
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@

public abstract class KioskExtractor extends ListExtractor {
private String contentCountry = null;
private String type = null;
private String id = null;

public KioskExtractor(StreamingService streamingService,
String url,
String nextStreamsUrl,
String type)
String kioskId)
throws IOException, ExtractionException {
super(streamingService, url, nextStreamsUrl);
this.contentCountry = contentCountry;
this.type = type;
this.id = kioskId;
}

/**
Expand All @@ -51,24 +50,23 @@ public void setContentCountry(String contentCountry) {
this.contentCountry = contentCountry;
}

/**
* Returns the type of the kiosk.
* eg. Trending, Top & Hot, Top last 24 hours
* @return type of kiosk
*/
public String getType() throws ParsingException {
return type;
}

@Override
public String getId() throws ParsingException {
return getType();
return id;
}

/**
* Id should be the name of the kiosk, tho Id is used for identifing it in the programm,
* so id should be kept in english.
* In order to get the name of the kiosk in the desired language we have to
* crawl if from the website.
* @return the tranlsated version of id
* @throws ParsingException
*/
@Override
public String getName() throws ParsingException {
return getType();
}
public abstract String getName() throws ParsingException;


public String getContentCountry() {
return contentCountry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.IOException;

public class KioskInfo extends ListInfo {
public String type;

public static ListExtractor.NextItemsResult getMoreItems(ServiceList serviceItem,
String url,
Expand Down Expand Up @@ -67,9 +66,9 @@ public static KioskInfo getInfo(KioskExtractor extractor,
KioskInfo info = new KioskInfo();
extractor.setContentCountry(contentCountry);
extractor.fetchPage();
info.type = extractor.getType();
info.name = extractor.getName();
info.id = extractor.getId();
info.url = extractor.getCleanUrl();

try {
StreamInfoItemCollector c = extractor.getStreams();
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/org/schabi/newpipe/extractor/kiosk/KioskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface KioskExtractorFactory {
KioskExtractor createNewKiosk(final StreamingService streamingService,
final String url,
final String nextStreamUrl,
final String type)
final String kioskId)
throws ExtractionException, IOException;
}

Expand All @@ -39,12 +39,12 @@ public KioskList(int service_id) {
this.service_id = service_id;
}

public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler, String type)
public void addKioskEntry(KioskExtractorFactory extractorFactory, UrlIdHandler handler, String id)
throws Exception {
if(kioskList.get(type) != null) {
throw new Exception("Kiosk with type " + type + " already exists.");
if(kioskList.get(id) != null) {
throw new Exception("Kiosk with type " + id + " already exists.");
}
kioskList.put(type, new KioskEntry(extractorFactory, handler));
kioskList.put(id, new KioskEntry(extractorFactory, handler));
}

public void setDefaultKiosk(String kioskType) {
Expand All @@ -54,35 +54,35 @@ public void setDefaultKiosk(String kioskType) {
public KioskExtractor getDefaultKioskExtractor(String nextStreamUrl)
throws ExtractionException, IOException {
if(defaultKiosk != null && !defaultKiosk.equals("")) {
return getExtractorByType(defaultKiosk, nextStreamUrl);
return getExtractorById(defaultKiosk, nextStreamUrl);
} else {
if(!kioskList.isEmpty()) {
// if not set get any entry
Object[] keySet = kioskList.keySet().toArray();
return getExtractorByType(keySet[0].toString(), nextStreamUrl);
return getExtractorById(keySet[0].toString(), nextStreamUrl);
} else {
return null;
}
}
}

public String getDefaultKioskType() {
public String getDefaultKioskId() {
return defaultKiosk;
}

public KioskExtractor getExtractorByType(String kioskType, String nextStreamsUrl)
public KioskExtractor getExtractorById(String kioskId, String nextStreamsUrl)
throws ExtractionException, IOException {
KioskEntry ke = kioskList.get(kioskType);
KioskEntry ke = kioskList.get(kioskId);
if(ke == null) {
throw new ExtractionException("No kiosk found with the type: " + kioskType);
throw new ExtractionException("No kiosk found with the type: " + kioskId);
} else {
return ke.extractorFactory.createNewKiosk(NewPipe.getService(service_id),
ke.handler.getUrl(kioskType),
nextStreamsUrl, kioskType);
ke.handler.getUrl(kioskId),
nextStreamsUrl, kioskId);
}
}

public Set<String> getAvailableKisokTypes() {
public Set<String> getAvailableKisoks() {
return kioskList.keySet();
}

Expand All @@ -91,7 +91,7 @@ public KioskExtractor getExtractorByUrl(String url, String nextStreamsUrl)
for(Map.Entry<String, KioskEntry> e : kioskList.entrySet()) {
KioskEntry ke = e.getValue();
if(ke.handler.acceptUrl(url)) {
return getExtractorByType(e.getKey(), nextStreamsUrl);
return getExtractorById(e.getKey(), nextStreamsUrl);
}
}
throw new ExtractionException("Could not find a kiosk that fits to the url: " + url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
public class SoundcloudChartsExtractor extends KioskExtractor {
private String url;

public SoundcloudChartsExtractor(StreamingService service, String url, String nextStreamsUrl, String type)
public SoundcloudChartsExtractor(StreamingService service, String url, String nextStreamsUrl, String kioskId)
throws IOException, ExtractionException {
super(service, url, nextStreamsUrl, type);
super(service, url, nextStreamsUrl, kioskId);
this.url = url;
}

Expand All @@ -25,8 +25,8 @@ public void fetchPage() {
}

@Override
public String getType() throws ParsingException {
return getUrlIdHandler().getId(url);
public String getName() throws ParsingException {
return "< Implement me (♥_♥) >";
}

@Override
Expand Down Expand Up @@ -54,7 +54,7 @@ public StreamInfoItemCollector getStreams() throws IOException, ExtractionExcept
"?genre=soundcloud:genres:all-music" +
"&client_id=" + SoundcloudParsingHelper.clientId();

if (getType().equals("Top 50")) {
if (getId().equals("Top 50")) {
apiUrl += "&kind=top";
} else {
apiUrl += "&kind=new";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,27 @@ public SuggestionExtractor getSuggestionExtractor() {

@Override
public KioskList getKioskList() throws ExtractionException {
KioskList.KioskExtractorFactory chartsFactory = new KioskList.KioskExtractorFactory() {
@Override
public KioskExtractor createNewKiosk(StreamingService streamingService,
String url,
String nextStreamUrl,
String id)
throws ExtractionException, IOException {
return new SoundcloudChartsExtractor(SoundcloudService.this,
url,
nextStreamUrl,
id);
}
};

KioskList list = new KioskList(getServiceId());

// add kiosks here e.g.:
final SoundcloudChartsUrlIdHandler h = new SoundcloudChartsUrlIdHandler();
try {
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
@Override
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, "Top 50");
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
@Override
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, "New & hot");
list.addKioskEntry(chartsFactory, h, "Top 50");
list.addKioskEntry(chartsFactory, h, "New & hot");
} catch (Exception e) {
throw new ExtractionException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ public KioskList getKioskList()
try {
list.addKioskEntry(new KioskList.KioskExtractorFactory() {
@Override
public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl, String type)
public KioskExtractor createNewKiosk(StreamingService streamingService, String url, String nextStreamUrl, String id)
throws ExtractionException, IOException {
return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl, type);
return new YoutubeTrendingExtractor(YoutubeService.this, url, nextStreamUrl, id);
}
}, new YoutubeTrendingUrlIdHandler(), "Trending");
list.setDefaultKiosk("Trending");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class YoutubeTrendingExtractor extends KioskExtractor {

private Document doc;

public YoutubeTrendingExtractor(StreamingService service, String url, String nextStreamsUrl, String type)
public YoutubeTrendingExtractor(StreamingService service, String url, String nextStreamsUrl, String kioskId)
throws IOException, ExtractionException {
super(service, url, nextStreamsUrl, type);
super(service, url, nextStreamsUrl, kioskId);
}

@Override
Expand All @@ -64,6 +64,18 @@ public ListExtractor.NextItemsResult getNextStreams() {
return null;
}

@Override
public String getName() throws ParsingException {
try {
Element a = doc.select("a[href*=\"/feed/trending\"]").first();
Element span = a.select("span[class*=\"display-name\"]").first();
Element nameSpan = span.select("span").first();
return nameSpan.text();
} catch (Exception e) {
throw new ParsingException("Could not get Trending name", e);
}
}

@Override
public StreamInfoItemCollector getStreams() throws ParsingException {
StreamInfoItemCollector collector = new StreamInfoItemCollector(getServiceId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void setUp() throws Exception {
NewPipe.init(Downloader.getInstance());
extractor = SoundCloud.getService()
.getKioskList()
.getExtractorByType("Top 50", null);
.getExtractorById("Top 50", null);
extractor.fetchPage();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void setUp() throws Exception {

@Test
public void testGetKioskAvailableKiosks() throws Exception {
assertFalse("No kiosk got returned", kioskList.getAvailableKisokTypes().isEmpty());
assertFalse("No kiosk got returned", kioskList.getAvailableKisoks().isEmpty());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public void getStreams() {
}

@Test
public void getType() {
assertEquals(kioskInfo.type, "Trending");
public void getId() {
assertEquals(kioskInfo.id, "Trending");
}

@Test
public void getName() {
assertFalse(kioskInfo.name.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void setUp() throws Exception {
NewPipe.init(Downloader.getInstance());
extractor = YouTube.getService()
.getKioskList()
.getExtractorByType("Trending", null);
.getExtractorById("Trending", null);
}

@Test
Expand All @@ -56,7 +56,8 @@ public void testGetDownloader() throws Exception {

@Test
public void testGetName() throws Exception {
assertEquals(extractor.getName(), "Trending");
System.out.println(extractor.getName());
assertFalse(extractor.getName().isEmpty());
}

@Test
Expand Down

0 comments on commit 466d87c

Please sign in to comment.