From 279f175693fdbca0e545b365bfd4532c16e0208c Mon Sep 17 00:00:00 2001 From: yausername <13ritvik@gmail.com> Date: Fri, 22 Nov 2019 22:59:14 +0530 Subject: [PATCH] validate peertube instance --- .../services/peertube/PeertubeInstance.java | 31 ++++++------------- .../services/peertube/PeertubeService.java | 24 ++------------ .../PeertubeChannelExtractorTest.java | 4 +-- .../PeertubeStreamExtractorDefaultTest.java | 2 +- .../PeertubeTrendingExtractorTest.java | 2 +- ...eertubeTrendingLinkHandlerFactoryTest.java | 2 +- .../PeertubeSearchExtractorDefaultTest.java | 3 +- .../peertube/search/PeertubeSearchQHTest.java | 3 +- 8 files changed, 21 insertions(+), 50 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeInstance.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeInstance.java index 132262faf..992b8eb6d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeInstance.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeInstance.java @@ -20,10 +20,9 @@ public class PeertubeInstance { private String name; public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube"); - public PeertubeInstance(String url) throws IOException { + public PeertubeInstance(String url) { this.url = url; - String response = validateInstance(url); - setInstanceMetaData(response); + this.name = "PeerTube"; } public PeertubeInstance(String url , String name) { @@ -35,37 +34,25 @@ public class PeertubeInstance { return url; } - private String validateInstance(String url) throws IOException { + public void fetchInstanceMetaData() throws Exception { Downloader downloader = NewPipe.getDownloader(); Response response = null; try { response = downloader.get(url + "/api/v1/config"); } catch (ReCaptchaException | IOException e) { - throw new IOException("unable to configure instance " + url, e); + throw new Exception("unable to configure instance " + url, e); } if(null == response || StringUtil.isBlank(response.responseBody())) { - throw new IOException("unable to configure instance " + url); + throw new Exception("unable to configure instance " + url); } - return response.responseBody(); - } - - private void setInstanceMetaData(String responseBody) { - JsonObject json; - try { - json = JsonParser.object().from(responseBody); - } catch (JsonParserException e) { - return; - } - - if(null == json) return; - - try { + try { + JsonObject json = JsonParser.object().from(response.responseBody()); this.name = JsonUtils.getString(json, "instance.name"); - } catch (ParsingException e) { - return; + } catch (JsonParserException | ParsingException e) { + throw new Exception("unable to parse instance config", e); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeService.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeService.java index 52fa5bf85..3c42cd396 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeService.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeService.java @@ -7,7 +7,6 @@ import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCap import java.io.IOException; import java.util.List; -import org.jsoup.helper.StringUtil; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability; import org.schabi.newpipe.extractor.channel.ChannelExtractor; @@ -51,10 +50,6 @@ public class PeertubeService extends StreamingService { this.instance = instance; } - public PeertubeService(int id, String name, List capabilities) { - super(id, name, capabilities); - } - @Override public LinkHandlerFactory getStreamLHFactory() { return PeertubeStreamLinkHandlerFactory.getInstance(); @@ -127,24 +122,11 @@ public class PeertubeService extends StreamingService { return instance.getUrl(); } - public void setInstance(String url) throws IOException { - this.instance = new PeertubeInstance(url); - if(!StringUtil.isBlank(instance.getName())) { - this.getServiceInfo().setName(instance.getName()); - }else { - this.getServiceInfo().setName("PeerTube"); - } + public void setInstance(PeertubeInstance instance) throws IOException { + this.instance = instance; + this.getServiceInfo().setName(instance.getName()); } - public void setInstance(String url, String name) { - this.instance = new PeertubeInstance(url, name); - if(!StringUtil.isBlank(instance.getName())) { - this.getServiceInfo().setName(instance.getName()); - }else { - this.getServiceInfo().setName("PeerTube"); - } - } - @Override public KioskList getKioskList() throws ExtractionException { KioskList.KioskExtractorFactory kioskFactory = new KioskList.KioskExtractorFactory() { diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java index 348474957..3d8bdff33 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java @@ -31,7 +31,7 @@ public class PeertubeChannelExtractorTest { public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); extractor = (PeertubeChannelExtractor) PeerTube .getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/kde"); extractor.fetchPage(); @@ -118,7 +118,7 @@ public class PeertubeChannelExtractorTest { public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); extractor = (PeertubeChannelExtractor) PeerTube .getChannelExtractor("https://peertube.mastodon.host/accounts/booteille"); extractor.fetchPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamExtractorDefaultTest.java index a6303b793..75a692d42 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamExtractorDefaultTest.java @@ -34,7 +34,7 @@ public class PeertubeStreamExtractorDefaultTest { public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); extractor = (PeertubeStreamExtractor) PeerTube.getStreamExtractor("https://peertube.mastodon.host/videos/watch/afe5bf12-c58b-4efd-b56e-29c5a59e04bc"); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java index f271d109f..c693e25be 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java @@ -28,7 +28,7 @@ public class PeertubeTrendingExtractorTest { public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); extractor = PeerTube .getKioskList() .getExtractorById("Trending", null); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingLinkHandlerFactoryTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingLinkHandlerFactoryTest.java index 0cc6fa013..0b89aa1aa 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingLinkHandlerFactoryTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingLinkHandlerFactoryTest.java @@ -21,7 +21,7 @@ public class PeertubeTrendingLinkHandlerFactoryTest { @BeforeClass public static void setUp() throws Exception { // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); LinkHandlerFactory = new PeertubeTrendingLinkHandlerFactory(); NewPipe.init(DownloaderTestImpl.getInstance()); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchExtractorDefaultTest.java index 5608faf28..dd9c6deb3 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchExtractorDefaultTest.java @@ -11,6 +11,7 @@ import org.schabi.newpipe.DownloaderTestImpl; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance; import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSearchExtractor; import org.schabi.newpipe.extractor.stream.StreamInfoItem; @@ -23,7 +24,7 @@ public class PeertubeSearchExtractorDefaultTest extends PeertubeSearchExtractorB public static void setUpClass() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); extractor = (PeertubeSearchExtractor) PeerTube.getSearchExtractor("kde"); extractor.fetchPage(); itemsPage = extractor.getInitialPage(); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java index 5f5a91f39..23e3100ce 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java @@ -5,13 +5,14 @@ import static org.schabi.newpipe.extractor.ServiceList.PeerTube; import org.junit.BeforeClass; import org.junit.Test; +import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance; public class PeertubeSearchQHTest { @BeforeClass public static void setUpClass() throws Exception { // setting instance might break test when running in parallel - PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"); + PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host")); } @Test