reindenting (ctrl alt l) on JsonUtils and PeertubeStreamExtractor
This commit is contained in:
parent
b816e48216
commit
341372c0d0
|
@ -40,9 +40,9 @@ import javax.annotation.Nonnull;
|
||||||
public class PeertubeStreamExtractor extends StreamExtractor {
|
public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
|
|
||||||
|
|
||||||
|
private final String baseUrl;
|
||||||
private JsonObject json;
|
private JsonObject json;
|
||||||
private List<SubtitlesStream> subtitles = new ArrayList<>();
|
private List<SubtitlesStream> subtitles = new ArrayList<>();
|
||||||
private final String baseUrl;
|
|
||||||
|
|
||||||
public PeertubeStreamExtractor(StreamingService service, LinkHandler linkHandler) throws ParsingException {
|
public PeertubeStreamExtractor(StreamingService service, LinkHandler linkHandler) throws ParsingException {
|
||||||
super(service, linkHandler);
|
super(service, linkHandler);
|
||||||
|
@ -149,7 +149,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
String value;
|
String value;
|
||||||
try {
|
try {
|
||||||
value = JsonUtils.getString(json, "account.avatar.path");
|
value = JsonUtils.getString(json, "account.avatar.path");
|
||||||
}catch(Exception e) {
|
} catch (Exception e) {
|
||||||
value = "/client/assets/images/default-avatar.png";
|
value = "/client/assets/images/default-avatar.png";
|
||||||
}
|
}
|
||||||
return baseUrl + value;
|
return baseUrl + value;
|
||||||
|
@ -176,8 +176,8 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
List<VideoStream> videoStreams = new ArrayList<>();
|
List<VideoStream> videoStreams = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
JsonArray streams = json.getArray("files", new JsonArray());
|
JsonArray streams = json.getArray("files", new JsonArray());
|
||||||
for(Object s: streams) {
|
for (Object s : streams) {
|
||||||
if(!(s instanceof JsonObject)) continue;
|
if (!(s instanceof JsonObject)) continue;
|
||||||
JsonObject stream = (JsonObject) s;
|
JsonObject stream = (JsonObject) s;
|
||||||
String url = JsonUtils.getString(stream, "fileUrl");
|
String url = JsonUtils.getString(stream, "fileUrl");
|
||||||
String torrentUrl = JsonUtils.getString(stream, "torrentUrl");
|
String torrentUrl = JsonUtils.getString(stream, "torrentUrl");
|
||||||
|
@ -211,8 +211,8 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
@Override
|
@Override
|
||||||
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
|
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
|
||||||
List<SubtitlesStream> filteredSubs = new ArrayList<>();
|
List<SubtitlesStream> filteredSubs = new ArrayList<>();
|
||||||
for(SubtitlesStream sub: subtitles) {
|
for (SubtitlesStream sub : subtitles) {
|
||||||
if(sub.getFormat() == format) {
|
if (sub.getFormat() == format) {
|
||||||
filteredSubs.add(sub);
|
filteredSubs.add(sub);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -234,18 +234,18 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
|
||||||
List<String> tags = getTags();
|
List<String> tags = getTags();
|
||||||
String apiUrl = null;
|
String apiUrl = null;
|
||||||
if(!tags.isEmpty()) {
|
if (!tags.isEmpty()) {
|
||||||
apiUrl = getRelatedStreamsUrl(tags);
|
apiUrl = getRelatedStreamsUrl(tags);
|
||||||
|
|
||||||
}else {
|
} else {
|
||||||
apiUrl = getUploaderUrl() + "/videos?start=0&count=8";
|
apiUrl = getUploaderUrl() + "/videos?start=0&count=8";
|
||||||
}
|
}
|
||||||
if(!StringUtil.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
|
if (!StringUtil.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
|
||||||
return collector;
|
return collector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> getTags(){
|
public List<String> getTags() {
|
||||||
try {
|
try {
|
||||||
return (List) JsonUtils.getArray(json, "tags");
|
return (List) JsonUtils.getArray(json, "tags");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -267,7 +267,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
String url = baseUrl + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT;
|
String url = baseUrl + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT;
|
||||||
StringBuilder params = new StringBuilder();
|
StringBuilder params = new StringBuilder();
|
||||||
params.append("start=0&count=8&sort=-createdAt");
|
params.append("start=0&count=8&sort=-createdAt");
|
||||||
for(String tag : tags) {
|
for (String tag : tags) {
|
||||||
params.append("&tagsOneOf=");
|
params.append("&tagsOneOf=");
|
||||||
params.append(URLEncoder.encode(tag, "UTF-8"));
|
params.append(URLEncoder.encode(tag, "UTF-8"));
|
||||||
}
|
}
|
||||||
|
@ -277,7 +277,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException {
|
private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException {
|
||||||
Response response = getDownloader().get(apiUrl);
|
Response response = getDownloader().get(apiUrl);
|
||||||
JsonObject relatedVideosJson = null;
|
JsonObject relatedVideosJson = null;
|
||||||
if(null != response && !StringUtil.isBlank(response.responseBody())) {
|
if (null != response && !StringUtil.isBlank(response.responseBody())) {
|
||||||
try {
|
try {
|
||||||
relatedVideosJson = JsonParser.object().from(response.responseBody());
|
relatedVideosJson = JsonParser.object().from(response.responseBody());
|
||||||
} catch (JsonParserException e) {
|
} catch (JsonParserException e) {
|
||||||
|
@ -285,7 +285,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(relatedVideosJson != null) {
|
if (relatedVideosJson != null) {
|
||||||
collectStreamsFrom(collector, relatedVideosJson);
|
collectStreamsFrom(collector, relatedVideosJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -294,16 +294,16 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
JsonArray contents;
|
JsonArray contents;
|
||||||
try {
|
try {
|
||||||
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
contents = (JsonArray) JsonUtils.getValue(json, "data");
|
||||||
}catch(Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ParsingException("unable to extract related videos", e);
|
throw new ParsingException("unable to extract related videos", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(Object c: contents) {
|
for (Object c : contents) {
|
||||||
if(c instanceof JsonObject) {
|
if (c instanceof JsonObject) {
|
||||||
final JsonObject item = (JsonObject) c;
|
final JsonObject item = (JsonObject) c;
|
||||||
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
|
||||||
//do not add the same stream in related streams
|
//do not add the same stream in related streams
|
||||||
if(!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
|
if (!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,9 +318,9 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
@Override
|
@Override
|
||||||
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
|
||||||
Response response = downloader.get(getUrl());
|
Response response = downloader.get(getUrl());
|
||||||
if(null != response && null != response.responseBody()) {
|
if (null != response && null != response.responseBody()) {
|
||||||
setInitialData(response.responseBody());
|
setInitialData(response.responseBody());
|
||||||
}else {
|
} else {
|
||||||
throw new ExtractionException("Unable to extract peertube channel data");
|
throw new ExtractionException("Unable to extract peertube channel data");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -333,7 +333,7 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
} catch (JsonParserException e) {
|
} catch (JsonParserException e) {
|
||||||
throw new ExtractionException("Unable to extract peertube stream data", e);
|
throw new ExtractionException("Unable to extract peertube stream data", e);
|
||||||
}
|
}
|
||||||
if(null == json) throw new ExtractionException("Unable to extract peertube stream data");
|
if (null == json) throw new ExtractionException("Unable to extract peertube stream data");
|
||||||
PeertubeParsingHelper.validate(json);
|
PeertubeParsingHelper.validate(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -343,14 +343,15 @@ public class PeertubeStreamExtractor extends StreamExtractor {
|
||||||
Response response = getDownloader().get(getUrl() + "/captions");
|
Response response = getDownloader().get(getUrl() + "/captions");
|
||||||
JsonObject captionsJson = JsonParser.object().from(response.responseBody());
|
JsonObject captionsJson = JsonParser.object().from(response.responseBody());
|
||||||
JsonArray captions = JsonUtils.getArray(captionsJson, "data");
|
JsonArray captions = JsonUtils.getArray(captionsJson, "data");
|
||||||
for(Object c: captions) {
|
for (Object c : captions) {
|
||||||
if(c instanceof JsonObject) {
|
if (c instanceof JsonObject) {
|
||||||
JsonObject caption = (JsonObject)c;
|
JsonObject caption = (JsonObject) c;
|
||||||
String url = baseUrl + JsonUtils.getString(caption, "captionPath");
|
String url = baseUrl + JsonUtils.getString(caption, "captionPath");
|
||||||
String languageCode = JsonUtils.getString(caption, "language.id");
|
String languageCode = JsonUtils.getString(caption, "language.id");
|
||||||
String ext = url.substring(url.lastIndexOf(".") + 1);
|
String ext = url.substring(url.lastIndexOf(".") + 1);
|
||||||
MediaFormat fmt = MediaFormat.getFromSuffix(ext);
|
MediaFormat fmt = MediaFormat.getFromSuffix(ext);
|
||||||
if(fmt != null && languageCode != null) subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
|
if (fmt != null && languageCode != null)
|
||||||
|
subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
@ -18,62 +18,62 @@ public class JsonUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
|
|
||||||
List<String> keys = Arrays.asList(path.split("\\."));
|
List<String> keys = Arrays.asList(path.split("\\."));
|
||||||
object = getObject(object, keys.subList(0, keys.size() - 1));
|
object = getObject(object, keys.subList(0, keys.size() - 1));
|
||||||
if (null == object) throw new ParsingException("Unable to get " + path);
|
if (null == object) throw new ParsingException("Unable to get " + path);
|
||||||
Object result = object.get(keys.get(keys.size() - 1));
|
Object result = object.get(keys.get(keys.size() - 1));
|
||||||
if(null == result) throw new ParsingException("Unable to get " + path);
|
if (null == result) throw new ParsingException("Unable to get " + path);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
Object value = getValue(object, path);
|
Object value = getValue(object, path);
|
||||||
if(value instanceof String) {
|
if (value instanceof String) {
|
||||||
return (String) value;
|
return (String) value;
|
||||||
}else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Unable to get " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
Object value = getValue(object, path);
|
Object value = getValue(object, path);
|
||||||
if(value instanceof Boolean) {
|
if (value instanceof Boolean) {
|
||||||
return (Boolean) value;
|
return (Boolean) value;
|
||||||
}else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Unable to get " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
Object value = getValue(object, path);
|
Object value = getValue(object, path);
|
||||||
if(value instanceof Number) {
|
if (value instanceof Number) {
|
||||||
return (Number) value;
|
return (Number) value;
|
||||||
}else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Unable to get " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
Object value = getValue(object, path);
|
Object value = getValue(object, path);
|
||||||
if(value instanceof JsonObject) {
|
if (value instanceof JsonObject) {
|
||||||
return (JsonObject) value;
|
return (JsonObject) value;
|
||||||
}else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Unable to get " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
|
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
|
||||||
Object value = getValue(object, path);
|
Object value = getValue(object, path);
|
||||||
if(value instanceof JsonArray) {
|
if (value instanceof JsonArray) {
|
||||||
return (JsonArray) value;
|
return (JsonArray) value;
|
||||||
}else {
|
} else {
|
||||||
throw new ParsingException("Unable to get " + path);
|
throw new ParsingException("Unable to get " + path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue