Skip to content

Commit 7c774c8

Browse files
authored
Merge pull request #505 from TeamNewPipe/meida.ccc.de_search_fix
[media.ccc.de] Fix NPE in search results if they contain a future talk
2 parents c2c4d97 + 2cbc3cc commit 7c774c8

5 files changed

Lines changed: 36 additions & 11 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCParsingHelper.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.schabi.newpipe.extractor.services.media_ccc.extractors;
22

33
import com.grack.nanojson.JsonArray;
4-
import com.grack.nanojson.JsonObject;
54
import com.grack.nanojson.JsonParser;
65
import com.grack.nanojson.JsonParserException;
76
import org.schabi.newpipe.extractor.downloader.Downloader;
@@ -13,9 +12,11 @@
1312
import java.io.IOException;
1413
import java.time.OffsetDateTime;
1514
import java.time.format.DateTimeParseException;
15+
import java.util.Locale;
1616
import java.util.regex.Pattern;
1717

1818
public final class MediaCCCParsingHelper {
19+
private static final Pattern LIVE_STREAM_ID_PATTERN = Pattern.compile("\\w+/\\w+"); // {conference_slug}/{room_slug}
1920
private static JsonArray liveStreams = null;
2021

2122
private MediaCCCParsingHelper() { }
@@ -28,12 +29,28 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate) throw
2829
}
2930
}
3031

31-
public static boolean isLiveStreamId(final String url) {
32-
final String pattern = "\\w+/\\w+";
33-
return Pattern.matches(pattern, url); // {conference_slug}/{room_slug}
32+
/**
33+
* Check whether an id is a live stream id
34+
* @param id the {@code id} to check
35+
* @return returns {@code true} if the {@code id} is formatted like {@code {conference_slug}/{room_slug}};
36+
* {@code false} otherwise
37+
*/
38+
public static boolean isLiveStreamId(final String id) {
39+
return LIVE_STREAM_ID_PATTERN.matcher(id).find();
3440
}
3541

36-
public static JsonArray getLiveStreams(final Downloader downloader, final Localization localization) throws ExtractionException {
42+
/**
43+
* Get currently available live streams from
44+
* <a href="https://streaming.media.ccc.de/streams/v2.json">https://streaming.media.ccc.de/streams/v2.json</a>.
45+
* Use this method to cache requests, because they can get quite big.
46+
* TODO: implement better caching policy (max-age: 3 min)
47+
* @param downloader The downloader to use for making the request
48+
* @param localization The localization to be used. Will most likely be ignored.
49+
* @return {@link JsonArray} containing current conferences and info about their rooms and streams.
50+
* @throws ExtractionException if the data could not be fetched or the retrieved data could not be parsed to a {@link JsonArray}
51+
*/
52+
public static JsonArray getLiveStreams(final Downloader downloader, final Localization localization)
53+
throws ExtractionException {
3754
if (liveStreams == null) {
3855
try {
3956
final String site = downloader.get("https://streaming.media.ccc.de/streams/v2.json",

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCSearchExtractor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,13 @@ public InfoItemsPage<InfoItem> getInitialPage() {
8181
|| getLinkHandler().getContentFilters().isEmpty()) {
8282
JsonArray events = doc.getArray("events");
8383
for (int i = 0; i < events.size(); i++) {
84-
searchItems.commit(new MediaCCCStreamInfoItemExtractor(
85-
events.getObject(i)));
84+
// Ensure only uploaded talks are shown in the search results.
85+
// If the release date is null, the talk has not been held or uploaded yet
86+
// and no streams are going to be available anyway.
87+
if (events.getObject(i).getString("release_date") != null) {
88+
searchItems.commit(new MediaCCCStreamInfoItemExtractor(
89+
events.getObject(i)));
90+
}
8691
}
8792
}
8893
return new InfoItemsPage<>(searchItems, null);
@@ -105,7 +110,7 @@ public void onFetchPage(@Nonnull final Downloader downloader)
105110
try {
106111
doc = JsonParser.object().from(site);
107112
} catch (JsonParserException jpe) {
108-
throw new ExtractionException("Could not parse json.", jpe);
113+
throw new ExtractionException("Could not parse JSON.", jpe);
109114
}
110115
}
111116
if (getLinkHandler().getContentFilters().contains(CONFERENCES)

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCStreamInfoItemExtractor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public String getTextualUploadDate() {
5555
@Nullable
5656
@Override
5757
public DateWrapper getUploadDate() throws ParsingException {
58-
return new DateWrapper(MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate()));
58+
final String date = getTextualUploadDate();
59+
if (date == null) {
60+
return null; // event is in the future...
61+
}
62+
return new DateWrapper(MediaCCCParsingHelper.parseDateFrom(date));
5963
}
6064

6165
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/linkHandler/MediaCCCSearchQueryHandlerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String getUrl(final String query, final List<String> contentFilter,
3333
return "https://media.ccc.de/public/events/search?q="
3434
+ URLEncoder.encode(query, "UTF-8");
3535
} catch (UnsupportedEncodingException e) {
36-
throw new ParsingException("Could not create search string with querry: " + query, e);
36+
throw new ParsingException("Could not create search string with query: " + query, e);
3737
}
3838
}
3939
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCLiveStreamListExtractorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public static void setUpClass() throws Exception {
2727
@Test
2828
public void getConferencesListTest() throws Exception {
2929
final List<InfoItem> a = extractor.getInitialPage().getItems();
30-
assertTrue(a.size() > 2);
3130
for (int i = 0; i < a.size(); i++) {
3231
final InfoItem b = a.get(i);
3332
assertNotNull(a.get(i).getName());

0 commit comments

Comments
 (0)