[MediaCCC] Use regex to parse stream and conference urls

This commit is contained in:
Stypox 2020-04-11 16:57:31 +02:00
parent 0c980b2d64
commit 07a90d116a
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
2 changed files with 10 additions and 48 deletions

View file

@ -9,32 +9,24 @@ import java.util.List;
public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory {
public static final String CONFERENCE_API_ENDPOINT = "https://api.media.ccc.de/public/conferences/";
public static final String CONFERENCE_PATH = "https://media.ccc.de/c/";
private static final String ID_PATTERN = "(?:(?:(?:api\\.)?media\\.ccc\\.de/public/conferences/)|(?:media\\.ccc\\.de/[bc]/))([^/?&#]*)";
@Override
public String getUrl(final String id, final List<String> contentFilter,
public String getUrl(final String id,
final List<String> contentFilter,
final String sortFilter) throws ParsingException {
return CONFERENCE_PATH + id;
}
@Override
public String getId(final String url) throws ParsingException {
if (url.startsWith(CONFERENCE_API_ENDPOINT)) {
return url.replace(CONFERENCE_API_ENDPOINT, "");
} else if (url.startsWith("https://media.ccc.de/public/conferences/")) {
return url.replace("https://media.ccc.de/public/conferences/", "");
} else if (url.startsWith(CONFERENCE_PATH)) {
return Parser.matchGroup1(CONFERENCE_PATH + "([^?#]*)", url);
} else if (url.startsWith("https://media.ccc.de/b/")) {
return Parser.matchGroup1("https://media.ccc.de/b/([^?#]*)", url);
}
throw new ParsingException("Could not get id from url: " + url);
return Parser.matchGroup1(ID_PATTERN, url);
}
@Override
public boolean onAcceptUrl(final String url) {
try {
getId(url);
return true;
return getId(url) != null;
} catch (ParsingException e) {
return false;
}

View file

@ -2,45 +2,16 @@ package org.schabi.newpipe.extractor.services.media_ccc.linkHandler;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import org.schabi.newpipe.extractor.utils.Parser;
public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
public static final String VIDEO_API_ENDPOINT = "https://api.media.ccc.de/public/events/";
private static final String VIDEO_PATH = "https://media.ccc.de/v/";
private static final String ID_PATTERN = "(?:(?:(?:api\\.)?media\\.ccc\\.de/public/events/)|(?:media\\.ccc\\.de/v/))([^/?&#]*)";
@Override
public String getId(final String urlString) throws ParsingException {
if (urlString.startsWith("https://media.ccc.de/public/events/")
&& !urlString.contains("?q=")) {
return urlString.substring(35); //remove /public/events part
}
if (urlString.startsWith(VIDEO_API_ENDPOINT)
&& !urlString.contains("?q=")) {
return urlString.substring(39); //remove api/public/events part
}
URL url;
try {
url = Utils.stringToURL(urlString);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("The given URL is not valid");
}
String path = url.getPath();
// remove leading "/" of URL-path if URL-path is given
if (!path.isEmpty()) {
path = path.substring(1);
}
if (path.startsWith("v/")) {
return path.substring(2);
}
throw new ParsingException("Could not get id from url: " + url);
public String getId(final String url) throws ParsingException {
return Parser.matchGroup1(ID_PATTERN, url);
}
@Override
@ -51,8 +22,7 @@ public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
@Override
public boolean onAcceptUrl(final String url) {
try {
getId(url);
return true;
return getId(url) != null;
} catch (ParsingException e) {
return false;
}