Skip to content

Commit 279f175

Browse files
committed
validate peertube instance
1 parent 4e0adbe commit 279f175

8 files changed

Lines changed: 21 additions & 50 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeInstance.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ public class PeertubeInstance {
2020
private String name;
2121
public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube");
2222

23-
public PeertubeInstance(String url) throws IOException {
23+
public PeertubeInstance(String url) {
2424
this.url = url;
25-
String response = validateInstance(url);
26-
setInstanceMetaData(response);
25+
this.name = "PeerTube";
2726
}
2827

2928
public PeertubeInstance(String url , String name) {
@@ -35,37 +34,25 @@ public String getUrl() {
3534
return url;
3635
}
3736

38-
private String validateInstance(String url) throws IOException {
37+
public void fetchInstanceMetaData() throws Exception {
3938
Downloader downloader = NewPipe.getDownloader();
4039
Response response = null;
4140

4241
try {
4342
response = downloader.get(url + "/api/v1/config");
4443
} catch (ReCaptchaException | IOException e) {
45-
throw new IOException("unable to configure instance " + url, e);
44+
throw new Exception("unable to configure instance " + url, e);
4645
}
4746

4847
if(null == response || StringUtil.isBlank(response.responseBody())) {
49-
throw new IOException("unable to configure instance " + url);
48+
throw new Exception("unable to configure instance " + url);
5049
}
5150

52-
return response.responseBody();
53-
}
54-
55-
private void setInstanceMetaData(String responseBody) {
56-
JsonObject json;
57-
try {
58-
json = JsonParser.object().from(responseBody);
59-
} catch (JsonParserException e) {
60-
return;
61-
}
62-
63-
if(null == json) return;
64-
65-
try {
51+
try {
52+
JsonObject json = JsonParser.object().from(response.responseBody());
6653
this.name = JsonUtils.getString(json, "instance.name");
67-
} catch (ParsingException e) {
68-
return;
54+
} catch (JsonParserException | ParsingException e) {
55+
throw new Exception("unable to parse instance config", e);
6956
}
7057
}
7158

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeService.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import java.io.IOException;
88
import java.util.List;
99

10-
import org.jsoup.helper.StringUtil;
1110
import org.schabi.newpipe.extractor.StreamingService;
1211
import org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability;
1312
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
@@ -51,10 +50,6 @@ public PeertubeService(int id, PeertubeInstance instance) {
5150
this.instance = instance;
5251
}
5352

54-
public PeertubeService(int id, String name, List<MediaCapability> capabilities) {
55-
super(id, name, capabilities);
56-
}
57-
5853
@Override
5954
public LinkHandlerFactory getStreamLHFactory() {
6055
return PeertubeStreamLinkHandlerFactory.getInstance();
@@ -127,24 +122,11 @@ public String getBaseUrl() {
127122
return instance.getUrl();
128123
}
129124

130-
public void setInstance(String url) throws IOException {
131-
this.instance = new PeertubeInstance(url);
132-
if(!StringUtil.isBlank(instance.getName())) {
133-
this.getServiceInfo().setName(instance.getName());
134-
}else {
135-
this.getServiceInfo().setName("PeerTube");
136-
}
125+
public void setInstance(PeertubeInstance instance) throws IOException {
126+
this.instance = instance;
127+
this.getServiceInfo().setName(instance.getName());
137128
}
138129

139-
public void setInstance(String url, String name) {
140-
this.instance = new PeertubeInstance(url, name);
141-
if(!StringUtil.isBlank(instance.getName())) {
142-
this.getServiceInfo().setName(instance.getName());
143-
}else {
144-
this.getServiceInfo().setName("PeerTube");
145-
}
146-
}
147-
148130
@Override
149131
public KioskList getKioskList() throws ExtractionException {
150132
KioskList.KioskExtractorFactory kioskFactory = new KioskList.KioskExtractorFactory() {

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static class KDE implements BaseChannelExtractorTest {
3131
public static void setUp() throws Exception {
3232
NewPipe.init(DownloaderTestImpl.getInstance());
3333
// setting instance might break test when running in parallel
34-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
34+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
3535
extractor = (PeertubeChannelExtractor) PeerTube
3636
.getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/kde");
3737
extractor.fetchPage();
@@ -118,7 +118,7 @@ public static class Booteille implements BaseChannelExtractorTest {
118118
public static void setUp() throws Exception {
119119
NewPipe.init(DownloaderTestImpl.getInstance());
120120
// setting instance might break test when running in parallel
121-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
121+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
122122
extractor = (PeertubeChannelExtractor) PeerTube
123123
.getChannelExtractor("https://peertube.mastodon.host/accounts/booteille");
124124
extractor.fetchPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeStreamExtractorDefaultTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class PeertubeStreamExtractorDefaultTest {
3434
public static void setUp() throws Exception {
3535
NewPipe.init(DownloaderTestImpl.getInstance());
3636
// setting instance might break test when running in parallel
37-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
37+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
3838
extractor = (PeertubeStreamExtractor) PeerTube.getStreamExtractor("https://peertube.mastodon.host/videos/watch/afe5bf12-c58b-4efd-b56e-29c5a59e04bc");
3939
extractor.fetchPage();
4040
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class PeertubeTrendingExtractorTest {
2828
public static void setUp() throws Exception {
2929
NewPipe.init(DownloaderTestImpl.getInstance());
3030
// setting instance might break test when running in parallel
31-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
31+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
3232
extractor = PeerTube
3333
.getKioskList()
3434
.getExtractorById("Trending", null);

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingLinkHandlerFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class PeertubeTrendingLinkHandlerFactoryTest {
2121
@BeforeClass
2222
public static void setUp() throws Exception {
2323
// setting instance might break test when running in parallel
24-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
24+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
2525
LinkHandlerFactory = new PeertubeTrendingLinkHandlerFactory();
2626
NewPipe.init(DownloaderTestImpl.getInstance());
2727
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchExtractorDefaultTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.schabi.newpipe.extractor.InfoItem;
1212
import org.schabi.newpipe.extractor.ListExtractor;
1313
import org.schabi.newpipe.extractor.NewPipe;
14+
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
1415
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeSearchExtractor;
1516
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1617

@@ -23,7 +24,7 @@ public class PeertubeSearchExtractorDefaultTest extends PeertubeSearchExtractorB
2324
public static void setUpClass() throws Exception {
2425
NewPipe.init(DownloaderTestImpl.getInstance());
2526
// setting instance might break test when running in parallel
26-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
27+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
2728
extractor = (PeertubeSearchExtractor) PeerTube.getSearchExtractor("kde");
2829
extractor.fetchPage();
2930
itemsPage = extractor.getInitialPage();

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/PeertubeSearchQHTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
import org.junit.BeforeClass;
77
import org.junit.Test;
8+
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
89

910
public class PeertubeSearchQHTest {
1011

1112
@BeforeClass
1213
public static void setUpClass() throws Exception {
1314
// setting instance might break test when running in parallel
14-
PeerTube.setInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host");
15+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
1516
}
1617

1718
@Test

0 commit comments

Comments
 (0)