Generate and verify Https links
This commit is contained in:
parent
5f2d0cf6b5
commit
fe2583f3d7
|
@ -0,0 +1,35 @@
|
||||||
|
package org.schabi.newpipe.extractor.services.soundcloud;
|
||||||
|
|
||||||
|
import com.grack.nanojson.JsonObject;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
public class SoundcloudExtractorHelper {
|
||||||
|
|
||||||
|
private static final String HTTP = "http://";
|
||||||
|
private static final String HTTPS = "https://";
|
||||||
|
|
||||||
|
|
||||||
|
private static String replaceHttpWithHttps(final String url) {
|
||||||
|
if(!url.isEmpty() && url.startsWith(HTTP)) {
|
||||||
|
return HTTPS + url.substring(HTTP.length());
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
static String getUploaderUrl(JsonObject object) {
|
||||||
|
String url = object.getObject("user").getString("permalink_url", "");
|
||||||
|
return replaceHttpWithHttps(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
static String getAvatarUrl(JsonObject object) {
|
||||||
|
String url = object.getObject("user", new JsonObject()).getString("avatar_url", "");
|
||||||
|
return replaceHttpWithHttps(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getUploaderName(JsonObject object) {
|
||||||
|
return object.getObject("user").getString("username", "");
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ import com.grack.nanojson.JsonObject;
|
||||||
import com.grack.nanojson.JsonParser;
|
import com.grack.nanojson.JsonParser;
|
||||||
import com.grack.nanojson.JsonParserException;
|
import com.grack.nanojson.JsonParserException;
|
||||||
import org.schabi.newpipe.extractor.Downloader;
|
import org.schabi.newpipe.extractor.Downloader;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
|
||||||
import org.schabi.newpipe.extractor.StreamingService;
|
import org.schabi.newpipe.extractor.StreamingService;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
|
@ -69,17 +68,17 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderUrl() {
|
public String getUploaderUrl() {
|
||||||
return playlist.getObject("user").getString("permalink_url", "");
|
return SoundcloudExtractorHelper.getUploaderUrl(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderName() {
|
public String getUploaderName() {
|
||||||
return playlist.getObject("user").getString("username", "");
|
return SoundcloudExtractorHelper.getUploaderName(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderAvatarUrl() {
|
public String getUploaderAvatarUrl() {
|
||||||
return playlist.getObject("user", new JsonObject()).getString("avatar_url", "");
|
return SoundcloudExtractorHelper.getAvatarUrl(playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -102,19 +102,19 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderUrl() {
|
public String getUploaderUrl() {
|
||||||
return track.getObject("user").getString("permalink_url", "");
|
return SoundcloudExtractorHelper.getUploaderUrl(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderName() {
|
public String getUploaderName() {
|
||||||
return track.getObject("user").getString("username", "");
|
return SoundcloudExtractorHelper.getUploaderName(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
@Override
|
@Override
|
||||||
public String getUploaderAvatarUrl() {
|
public String getUploaderAvatarUrl() {
|
||||||
return track.getObject("user", new JsonObject()).getString("avatar_url", "");
|
return SoundcloudExtractorHelper.getAvatarUrl(track);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package org.schabi.newpipe.extractor;
|
package org.schabi.newpipe.extractor;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class ExtractorAsserts {
|
public class ExtractorAsserts {
|
||||||
public static void assertEmptyErrors(String message, List<Throwable> errors) {
|
public static void assertEmptyErrors(String message, List<Throwable> errors) {
|
||||||
if(!errors.isEmpty()) {
|
if(!errors.isEmpty()) {
|
||||||
|
@ -14,11 +17,21 @@ public class ExtractorAsserts {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void assertIsValidUrl(String url) {
|
@Nonnull
|
||||||
|
private static URL urlFromString(String url) {
|
||||||
try {
|
try {
|
||||||
new URL(url);
|
return new URL(url);
|
||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
throw new AssertionError("Invalid url: " + url, e);
|
throw new AssertionError("Invalid url: " + url, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void assertIsValidUrl(String url) {
|
||||||
|
urlFromString(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void assertIsSecureUrl(String urlToCheck) {
|
||||||
|
URL url = urlFromString(urlToCheck);
|
||||||
|
assertEquals("Protocol of URL is not secure", "https", url.getProtocol());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.NewPipe;
|
||||||
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
|
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -43,7 +44,7 @@ public class SoundcloudChannelExtractorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetAvatarUrl() throws Exception {
|
public void testGetAvatarUrl() throws Exception {
|
||||||
assertTrue(extractor.getAvatarUrl().contains("https://"));
|
assertIsSecureUrl(extractor.getAvatarUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -8,6 +8,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,12 +43,13 @@ public class SoundcloudPlaylistExtractorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetThumbnailUrl() throws Exception {
|
public void testGetThumbnailUrl() throws Exception {
|
||||||
assertTrue(extractor.getThumbnailUrl(), extractor.getThumbnailUrl().contains("https://"));
|
assertIsSecureUrl(extractor.getThumbnailUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploaderUrl() throws Exception {
|
public void testGetUploaderUrl() throws Exception {
|
||||||
assertEquals(extractor.getUploaderUrl(), "http://soundcloud.com/liluzivert");
|
assertIsSecureUrl(extractor.getUploaderUrl());
|
||||||
|
assertEquals(extractor.getUploaderUrl(), "https://soundcloud.com/liluzivert");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -57,7 +59,7 @@ public class SoundcloudPlaylistExtractorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploaderAvatarUrl() throws Exception {
|
public void testGetUploaderAvatarUrl() throws Exception {
|
||||||
assertTrue(extractor.getUploaderAvatarUrl(), extractor.getUploaderAvatarUrl().contains("https://"));
|
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -14,6 +14,7 @@ import org.schabi.newpipe.extractor.stream.SubtitlesFormat;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,22 +70,23 @@ public class SoundcloudStreamExtractorDefaultTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploadDate() throws ParsingException {
|
public void testGetUploadDate() throws ParsingException {
|
||||||
assertEquals(extractor.getUploadDate(), "2016-07-31");
|
assertEquals("2016-07-31", extractor.getUploadDate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploaderUrl() throws ParsingException {
|
public void testGetUploaderUrl() throws ParsingException {
|
||||||
assertEquals(extractor.getUploaderUrl(), "http://soundcloud.com/liluzivert");
|
assertIsSecureUrl(extractor.getUploaderUrl());
|
||||||
|
assertEquals("https://soundcloud.com/liluzivert", extractor.getUploaderUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetThumbnailUrl() throws ParsingException {
|
public void testGetThumbnailUrl() throws ParsingException {
|
||||||
assertTrue(extractor.getThumbnailUrl(), extractor.getThumbnailUrl().contains("https://"));
|
assertIsSecureUrl(extractor.getThumbnailUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploaderAvatarUrl() throws ParsingException {
|
public void testGetUploaderAvatarUrl() throws ParsingException {
|
||||||
assertTrue(extractor.getUploaderAvatarUrl(), extractor.getUploaderAvatarUrl().contains("https://"));
|
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor.services.youtube;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.schabi.newpipe.Downloader;
|
import org.schabi.newpipe.Downloader;
|
||||||
|
import org.schabi.newpipe.extractor.ExtractorAsserts;
|
||||||
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.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
|
@ -12,6 +13,7 @@ import org.schabi.newpipe.extractor.utils.Utils;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -38,7 +40,6 @@ import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
||||||
* Test for {@link StreamExtractor}
|
* Test for {@link StreamExtractor}
|
||||||
*/
|
*/
|
||||||
public class YoutubeStreamExtractorDefaultTest {
|
public class YoutubeStreamExtractorDefaultTest {
|
||||||
public static final String HTTPS = "https://";
|
|
||||||
private static YoutubeStreamExtractor extractor;
|
private static YoutubeStreamExtractor extractor;
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
|
@ -102,14 +103,12 @@ public class YoutubeStreamExtractorDefaultTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetThumbnailUrl() throws ParsingException {
|
public void testGetThumbnailUrl() throws ParsingException {
|
||||||
assertTrue(extractor.getThumbnailUrl(),
|
assertIsSecureUrl(extractor.getThumbnailUrl());
|
||||||
extractor.getThumbnailUrl().contains(HTTPS));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetUploaderAvatarUrl() throws ParsingException {
|
public void testGetUploaderAvatarUrl() throws ParsingException {
|
||||||
assertTrue(extractor.getUploaderAvatarUrl(),
|
assertIsSecureUrl(extractor.getUploaderAvatarUrl());
|
||||||
extractor.getUploaderAvatarUrl().contains(HTTPS));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -120,8 +119,7 @@ public class YoutubeStreamExtractorDefaultTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetVideoStreams() throws IOException, ExtractionException {
|
public void testGetVideoStreams() throws IOException, ExtractionException {
|
||||||
for (VideoStream s : extractor.getVideoStreams()) {
|
for (VideoStream s : extractor.getVideoStreams()) {
|
||||||
assertTrue(s.url,
|
assertIsSecureUrl(s.url);
|
||||||
s.url.contains(HTTPS));
|
|
||||||
assertTrue(s.resolution.length() > 0);
|
assertTrue(s.resolution.length() > 0);
|
||||||
assertTrue(Integer.toString(s.getFormatId()),
|
assertTrue(Integer.toString(s.getFormatId()),
|
||||||
0 <= s.getFormatId() && s.getFormatId() <= 4);
|
0 <= s.getFormatId() && s.getFormatId() <= 4);
|
||||||
|
|
Loading…
Reference in New Issue