code optimization

This commit is contained in:
bopol 2020-02-12 21:24:50 +01:00
parent 9701c7d800
commit f742a6bd3e
4 changed files with 9 additions and 38 deletions

View File

@ -1,13 +1,9 @@
package org.schabi.newpipe.extractor.services.media_ccc.linkHandler; package org.schabi.newpipe.extractor.services.media_ccc.linkHandler;
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List; import java.util.List;
public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory { public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory {
@ -18,32 +14,15 @@ public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory
} }
@Override @Override
public String getId(String urlString) throws ParsingException { public String getId(String url) throws ParsingException {
if (urlString.startsWith("https://api.media.ccc.de/public/conferences/")) { if (url.startsWith("https://api.media.ccc.de/public/conferences/")) {
return urlString.replace("https://api.media.ccc.de/public/conferences/", ""); return url.replace("https://api.media.ccc.de/public/conferences/", "");
} else if (urlString.startsWith("https://media.ccc.de/c/")) { } else if (url.startsWith("https://media.ccc.de/c/")) {
return Parser.matchGroup1("https://media.ccc.de/c/([^?#]*)", urlString); return Parser.matchGroup1("https://media.ccc.de/c/([^?#]*)", url);
} else if (url.startsWith("https://media.ccc.de/b/")) {
return Parser.matchGroup1("https://media.ccc.de/b/([^?#]*)", url);
} }
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.contains("b/")) {
return path.substring(2);
}
throw new ParsingException("Could not get id from url: " + url); throw new ParsingException("Could not get id from url: " + url);
} }
@Override @Override
@ -51,8 +30,6 @@ public class MediaCCCConferenceLinkHandlerFactory extends ListLinkHandlerFactory
try { try {
getId(url); getId(url);
return true; return true;
} catch (FoundAdException fe) {
throw fe;
} catch (ParsingException e) { } catch (ParsingException e) {
return false; return false;
} }

View File

@ -30,7 +30,7 @@ public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
path = path.substring(1); path = path.substring(1);
} }
if (path.contains("v/")) { if (path.startsWith("v/")) {
return path.substring(2); return path.substring(2);
} }
@ -47,8 +47,6 @@ public class MediaCCCStreamLinkHandlerFactory extends LinkHandlerFactory {
try { try {
getId(url); getId(url);
return true; return true;
} catch (FoundAdException fe) {
throw fe;
} catch (ParsingException e) { } catch (ParsingException e) {
return false; return false;
} }

View File

@ -7,7 +7,7 @@ import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceExtractor; import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCConferenceExtractor;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC; import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
/** /**

View File

@ -1,23 +1,19 @@
package org.schabi.newpipe.extractor.services.media_ccc; package org.schabi.newpipe.extractor.services.media_ccc;
import com.grack.nanojson.JsonObject;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl; import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCStreamExtractor; import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCStreamExtractor;
import java.io.IOException;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import static java.util.Objects.requireNonNull; import static java.util.Objects.requireNonNull;
import static junit.framework.TestCase.assertEquals; import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.schabi.newpipe.extractor.ServiceList.MediaCCC; import static org.schabi.newpipe.extractor.ServiceList.MediaCCC;
/** /**