[media.ccc.de] Use Pattern for getting the ive stream id

This commit is contained in:
TobiGr 2020-12-31 16:58:40 +01:00
parent edf8dd0e92
commit a896ec41b9
1 changed files with 21 additions and 4 deletions

View File

@ -1,7 +1,6 @@
package org.schabi.newpipe.extractor.services.media_ccc.extractors;
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.schabi.newpipe.extractor.downloader.Downloader;
@ -13,9 +12,11 @@ import org.schabi.newpipe.extractor.localization.Localization;
import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeParseException;
import java.util.Locale;
import java.util.regex.Pattern;
public final class MediaCCCParsingHelper {
private static final Pattern LIVE_STREAM_ID_PATTERN = Pattern.compile("\\w+/\\w+"); // {conference_slug}/{room_slug}
private static JsonArray liveStreams = null;
private MediaCCCParsingHelper() { }
@ -28,12 +29,28 @@ public final class MediaCCCParsingHelper {
}
}
/**
* Check whether an id is a live stream id
* @param id the {@code id} to check
* @return returns {@code true} if the {@code id} is formatted like {@code {conference_slug}/{room_slug}};
* {@code false} otherwise
*/
public static boolean isLiveStreamId(final String id) {
final String pattern = "\\w+/\\w+";
return Pattern.matches(pattern, id); // {conference_slug}/{room_slug}
return LIVE_STREAM_ID_PATTERN.matcher(id).find();
}
public static JsonArray getLiveStreams(final Downloader downloader, final Localization localization) throws ExtractionException {
/**
* Get currently available live streams from
* <a href="https://streaming.media.ccc.de/streams/v2.json">https://streaming.media.ccc.de/streams/v2.json</a>.
* Use this method to cache requests, because they can get quite big.
* TODO: implement better caching policy (max-age: 3 min)
* @param downloader The downloader to use for making the request
* @param localization The localization to be used. Will most likely be ignored.
* @return {@link JsonArray} containing current conferences and info about their rooms and streams.
* @throws ExtractionException if the data could not be fetched or the retrieved data could not be parsed to a {@link JsonArray}
*/
public static JsonArray getLiveStreams(final Downloader downloader, final Localization localization)
throws ExtractionException {
if (liveStreams == null) {
try {
final String site = downloader.get("https://streaming.media.ccc.de/streams/v2.json",