[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 class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory {
public static final String CONFERENCE_API_ENDPOINT = "https://api.media.ccc.de/public/conferences/"; 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/"; 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 @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 { final String sortFilter) throws ParsingException {
return CONFERENCE_PATH + id; return CONFERENCE_PATH + id;
} }
@Override @Override
public String getId(final String url) throws ParsingException { public String getId(final String url) throws ParsingException {
if (url.startsWith(CONFERENCE_API_ENDPOINT)) { return Parser.matchGroup1(ID_PATTERN, url);
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);
} }
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
try { try {
getId(url); return getId(url) != null;
return true;
} catch (ParsingException e) { } catch (ParsingException e) {
return false; 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.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Parser;
import java.net.MalformedURLException;
import java.net.URL;
public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory { public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
public static final String VIDEO_API_ENDPOINT = "https://api.media.ccc.de/public/events/"; 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 VIDEO_PATH = "https://media.ccc.de/v/";
private static final String ID_PATTERN = "(?:(?:(?:api\\.)?media\\.ccc\\.de/public/events/)|(?:media\\.ccc\\.de/v/))([^/?&#]*)";
@Override @Override
public String getId(final String urlString) throws ParsingException { public String getId(final String url) throws ParsingException {
if (urlString.startsWith("https://media.ccc.de/public/events/") return Parser.matchGroup1(ID_PATTERN, url);
&& !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);
} }
@Override @Override
@ -51,8 +22,7 @@ public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
@Override @Override
public boolean onAcceptUrl(final String url) { public boolean onAcceptUrl(final String url) {
try { try {
getId(url); return getId(url) != null;
return true;
} catch (ParsingException e) { } catch (ParsingException e) {
return false; return false;
} }