validate peertube instance

This commit is contained in:
yausername 2019-11-22 22:59:14 +05:30
parent 4e0adbefbc
commit 279f175693
8 changed files with 21 additions and 50 deletions

View File

@ -20,10 +20,9 @@ public class PeertubeInstance {
private String name; private String name;
public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube"); public static final PeertubeInstance defaultInstance = new PeertubeInstance("https://framatube.org", "FramaTube");
public PeertubeInstance(String url) throws IOException { public PeertubeInstance(String url) {
this.url = url; this.url = url;
String response = validateInstance(url); this.name = "PeerTube";
setInstanceMetaData(response);
} }
public PeertubeInstance(String url , String name) { public PeertubeInstance(String url , String name) {
@ -35,37 +34,25 @@ public class PeertubeInstance {
return url; return url;
} }
private String validateInstance(String url) throws IOException { public void fetchInstanceMetaData() throws Exception {
Downloader downloader = NewPipe.getDownloader(); Downloader downloader = NewPipe.getDownloader();
Response response = null; Response response = null;
try { try {
response = downloader.get(url + "/api/v1/config"); response = downloader.get(url + "/api/v1/config");
} catch (ReCaptchaException | IOException e) { } 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())) { 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(); try {
} JsonObject json = JsonParser.object().from(response.responseBody());
private void setInstanceMetaData(String responseBody) {
JsonObject json;
try {
json = JsonParser.object().from(responseBody);
} catch (JsonParserException e) {
return;
}
if(null == json) return;
try {
this.name = JsonUtils.getString(json, "instance.name"); this.name = JsonUtils.getString(json, "instance.name");
} catch (ParsingException e) { } catch (JsonParserException | ParsingException e) {
return; throw new Exception("unable to parse instance config", e);
} }
} }

View File

@ -7,7 +7,6 @@ import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCap
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.jsoup.helper.StringUtil;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability; import org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability;
import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.channel.ChannelExtractor;
@ -51,10 +50,6 @@ public class PeertubeService extends StreamingService {
this.instance = instance; this.instance = instance;
} }
public PeertubeService(int id, String name, List<MediaCapability> capabilities) {
super(id, name, capabilities);
}
@Override @Override
public LinkHandlerFactory getStreamLHFactory() { public LinkHandlerFactory getStreamLHFactory() {
return PeertubeStreamLinkHandlerFactory.getInstance(); return PeertubeStreamLinkHandlerFactory.getInstance();
@ -127,24 +122,11 @@ public class PeertubeService extends StreamingService {
return instance.getUrl(); return instance.getUrl();
} }
public void setInstance(String url) throws IOException { public void setInstance(PeertubeInstance instance) throws IOException {
this.instance = new PeertubeInstance(url); this.instance = instance;
if(!StringUtil.isBlank(instance.getName())) { this.getServiceInfo().setName(instance.getName());
this.getServiceInfo().setName(instance.getName());
}else {
this.getServiceInfo().setName("PeerTube");
}
} }
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 @Override
public KioskList getKioskList() throws ExtractionException { public KioskList getKioskList() throws ExtractionException {
KioskList.KioskExtractorFactory kioskFactory = new KioskList.KioskExtractorFactory() { KioskList.KioskExtractorFactory kioskFactory = new KioskList.KioskExtractorFactory() {

View File

@ -31,7 +31,7 @@ public class PeertubeChannelExtractorTest {
public static void setUp() throws Exception { public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel // 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 extractor = (PeertubeChannelExtractor) PeerTube
.getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/kde"); .getChannelExtractor("https://peertube.mastodon.host/api/v1/accounts/kde");
extractor.fetchPage(); extractor.fetchPage();
@ -118,7 +118,7 @@ public class PeertubeChannelExtractorTest {
public static void setUp() throws Exception { public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel // 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 extractor = (PeertubeChannelExtractor) PeerTube
.getChannelExtractor("https://peertube.mastodon.host/accounts/booteille"); .getChannelExtractor("https://peertube.mastodon.host/accounts/booteille");
extractor.fetchPage(); extractor.fetchPage();

View File

@ -34,7 +34,7 @@ public class PeertubeStreamExtractorDefaultTest {
public static void setUp() throws Exception { public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel // 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 = (PeertubeStreamExtractor) PeerTube.getStreamExtractor("https://peertube.mastodon.host/videos/watch/afe5bf12-c58b-4efd-b56e-29c5a59e04bc");
extractor.fetchPage(); extractor.fetchPage();
} }

View File

@ -28,7 +28,7 @@ public class PeertubeTrendingExtractorTest {
public static void setUp() throws Exception { public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel // 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 extractor = PeerTube
.getKioskList() .getKioskList()
.getExtractorById("Trending", null); .getExtractorById("Trending", null);

View File

@ -21,7 +21,7 @@ public class PeertubeTrendingLinkHandlerFactoryTest {
@BeforeClass @BeforeClass
public static void setUp() throws Exception { public static void setUp() throws Exception {
// setting instance might break test when running in parallel // 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(); LinkHandlerFactory = new PeertubeTrendingLinkHandlerFactory();
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
} }

View File

@ -11,6 +11,7 @@ import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe; 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.services.peertube.extractors.PeertubeSearchExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
@ -23,7 +24,7 @@ public class PeertubeSearchExtractorDefaultTest extends PeertubeSearchExtractorB
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance()); NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel // 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 = (PeertubeSearchExtractor) PeerTube.getSearchExtractor("kde");
extractor.fetchPage(); extractor.fetchPage();
itemsPage = extractor.getInitialPage(); itemsPage = extractor.getInitialPage();

View File

@ -5,13 +5,14 @@ import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.schabi.newpipe.extractor.services.peertube.PeertubeInstance;
public class PeertubeSearchQHTest { public class PeertubeSearchQHTest {
@BeforeClass @BeforeClass
public static void setUpClass() throws Exception { public static void setUpClass() throws Exception {
// setting instance might break test when running in parallel // 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 @Test