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;
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);
}
}

View file

@ -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<MediaCapability> 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() {

View file

@ -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();

View file

@ -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();
}

View file

@ -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);

View file

@ -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());
}

View file

@ -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();

View file

@ -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