Skip to content

Commit 341372c

Browse files
committed
reindenting (ctrl alt l) on JsonUtils and PeertubeStreamExtractor
1 parent b816e48 commit 341372c

2 files changed

Lines changed: 61 additions & 60 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@
3939

4040
public class PeertubeStreamExtractor extends StreamExtractor {
4141

42-
42+
43+
private final String baseUrl;
4344
private JsonObject json;
4445
private List<SubtitlesStream> subtitles = new ArrayList<>();
45-
private final String baseUrl;
46-
46+
4747
public PeertubeStreamExtractor(StreamingService service, LinkHandler linkHandler) throws ParsingException {
4848
super(service, linkHandler);
4949
this.baseUrl = getBaseUrl();
5050
}
51-
51+
5252
@Override
5353
public String getTextualUploadDate() throws ParsingException {
5454
return JsonUtils.getString(json, "publishedAt");
@@ -64,7 +64,7 @@ public DateWrapper getUploadDate() throws ParsingException {
6464

6565
return new DateWrapper(PeertubeParsingHelper.parseDateFrom(textualUploadDate));
6666
}
67-
67+
6868
@Override
6969
public String getThumbnailUrl() throws ParsingException {
7070
return baseUrl + JsonUtils.getString(json, "previewPath");
@@ -149,7 +149,7 @@ public String getUploaderAvatarUrl() throws ParsingException {
149149
String value;
150150
try {
151151
value = JsonUtils.getString(json, "account.avatar.path");
152-
}catch(Exception e) {
152+
} catch (Exception e) {
153153
value = "/client/assets/images/default-avatar.png";
154154
}
155155
return baseUrl + value;
@@ -176,8 +176,8 @@ public List<VideoStream> getVideoStreams() throws IOException, ExtractionExcepti
176176
List<VideoStream> videoStreams = new ArrayList<>();
177177
try {
178178
JsonArray streams = json.getArray("files", new JsonArray());
179-
for(Object s: streams) {
180-
if(!(s instanceof JsonObject)) continue;
179+
for (Object s : streams) {
180+
if (!(s instanceof JsonObject)) continue;
181181
JsonObject stream = (JsonObject) s;
182182
String url = JsonUtils.getString(stream, "fileUrl");
183183
String torrentUrl = JsonUtils.getString(stream, "torrentUrl");
@@ -211,8 +211,8 @@ public List<SubtitlesStream> getSubtitlesDefault() throws IOException, Extractio
211211
@Override
212212
public List<SubtitlesStream> getSubtitles(final MediaFormat format) throws IOException, ExtractionException {
213213
List<SubtitlesStream> filteredSubs = new ArrayList<>();
214-
for(SubtitlesStream sub: subtitles) {
215-
if(sub.getFormat() == format) {
214+
for (SubtitlesStream sub : subtitles) {
215+
if (sub.getFormat() == format) {
216216
filteredSubs.add(sub);
217217
}
218218
}
@@ -234,18 +234,18 @@ public StreamInfoItemsCollector getRelatedStreams() throws IOException, Extracti
234234
StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
235235
List<String> tags = getTags();
236236
String apiUrl = null;
237-
if(!tags.isEmpty()) {
237+
if (!tags.isEmpty()) {
238238
apiUrl = getRelatedStreamsUrl(tags);
239-
240-
}else {
239+
240+
} else {
241241
apiUrl = getUploaderUrl() + "/videos?start=0&count=8";
242242
}
243-
if(!StringUtil.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
243+
if (!StringUtil.isBlank(apiUrl)) getStreamsFromApi(collector, apiUrl);
244244
return collector;
245245
}
246246

247247
@Override
248-
public List<String> getTags(){
248+
public List<String> getTags() {
249249
try {
250250
return (List) JsonUtils.getArray(json, "tags");
251251
} catch (Exception e) {
@@ -267,7 +267,7 @@ private String getRelatedStreamsUrl(List<String> tags) throws UnsupportedEncodin
267267
String url = baseUrl + PeertubeSearchQueryHandlerFactory.SEARCH_ENDPOINT;
268268
StringBuilder params = new StringBuilder();
269269
params.append("start=0&count=8&sort=-createdAt");
270-
for(String tag : tags) {
270+
for (String tag : tags) {
271271
params.append("&tagsOneOf=");
272272
params.append(URLEncoder.encode(tag, "UTF-8"));
273273
}
@@ -277,38 +277,38 @@ private String getRelatedStreamsUrl(List<String> tags) throws UnsupportedEncodin
277277
private void getStreamsFromApi(StreamInfoItemsCollector collector, String apiUrl) throws ReCaptchaException, IOException, ParsingException {
278278
Response response = getDownloader().get(apiUrl);
279279
JsonObject relatedVideosJson = null;
280-
if(null != response && !StringUtil.isBlank(response.responseBody())) {
280+
if (null != response && !StringUtil.isBlank(response.responseBody())) {
281281
try {
282282
relatedVideosJson = JsonParser.object().from(response.responseBody());
283283
} catch (JsonParserException e) {
284284
throw new ParsingException("Could not parse json data for related videos", e);
285285
}
286286
}
287-
288-
if(relatedVideosJson != null) {
287+
288+
if (relatedVideosJson != null) {
289289
collectStreamsFrom(collector, relatedVideosJson);
290290
}
291291
}
292-
292+
293293
private void collectStreamsFrom(StreamInfoItemsCollector collector, JsonObject json) throws ParsingException {
294294
JsonArray contents;
295295
try {
296296
contents = (JsonArray) JsonUtils.getValue(json, "data");
297-
}catch(Exception e) {
297+
} catch (Exception e) {
298298
throw new ParsingException("unable to extract related videos", e);
299299
}
300-
301-
for(Object c: contents) {
302-
if(c instanceof JsonObject) {
300+
301+
for (Object c : contents) {
302+
if (c instanceof JsonObject) {
303303
final JsonObject item = (JsonObject) c;
304304
PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
305305
//do not add the same stream in related streams
306-
if(!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
306+
if (!extractor.getUrl().equals(getUrl())) collector.commit(extractor);
307307
}
308308
}
309-
309+
310310
}
311-
311+
312312

313313
@Override
314314
public String getErrorMessage() {
@@ -318,12 +318,12 @@ public String getErrorMessage() {
318318
@Override
319319
public void onFetchPage(Downloader downloader) throws IOException, ExtractionException {
320320
Response response = downloader.get(getUrl());
321-
if(null != response && null != response.responseBody()) {
321+
if (null != response && null != response.responseBody()) {
322322
setInitialData(response.responseBody());
323-
}else {
323+
} else {
324324
throw new ExtractionException("Unable to extract peertube channel data");
325325
}
326-
326+
327327
loadSubtitles();
328328
}
329329

@@ -333,24 +333,25 @@ private void setInitialData(String responseBody) throws ExtractionException {
333333
} catch (JsonParserException e) {
334334
throw new ExtractionException("Unable to extract peertube stream data", e);
335335
}
336-
if(null == json) throw new ExtractionException("Unable to extract peertube stream data");
336+
if (null == json) throw new ExtractionException("Unable to extract peertube stream data");
337337
PeertubeParsingHelper.validate(json);
338338
}
339-
339+
340340
private void loadSubtitles() {
341341
if (subtitles.isEmpty()) {
342342
try {
343-
Response response = getDownloader().get(getUrl() + "/captions");
343+
Response response = getDownloader().get(getUrl() + "/captions");
344344
JsonObject captionsJson = JsonParser.object().from(response.responseBody());
345345
JsonArray captions = JsonUtils.getArray(captionsJson, "data");
346-
for(Object c: captions) {
347-
if(c instanceof JsonObject) {
348-
JsonObject caption = (JsonObject)c;
346+
for (Object c : captions) {
347+
if (c instanceof JsonObject) {
348+
JsonObject caption = (JsonObject) c;
349349
String url = baseUrl + JsonUtils.getString(caption, "captionPath");
350350
String languageCode = JsonUtils.getString(caption, "language.id");
351351
String ext = url.substring(url.lastIndexOf(".") + 1);
352352
MediaFormat fmt = MediaFormat.getFromSuffix(ext);
353-
if(fmt != null && languageCode != null) subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
353+
if (fmt != null && languageCode != null)
354+
subtitles.add(new SubtitlesStream(fmt, languageCode, url, false));
354355
}
355356
}
356357
} catch (Exception e) {

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,71 +16,71 @@ public class JsonUtils {
1616

1717
private JsonUtils() {
1818
}
19-
19+
2020
@Nonnull
21-
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
21+
public static Object getValue(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
2222

2323
List<String> keys = Arrays.asList(path.split("\\."));
2424
object = getObject(object, keys.subList(0, keys.size() - 1));
2525
if (null == object) throw new ParsingException("Unable to get " + path);
2626
Object result = object.get(keys.get(keys.size() - 1));
27-
if(null == result) throw new ParsingException("Unable to get " + path);
27+
if (null == result) throw new ParsingException("Unable to get " + path);
2828
return result;
2929
}
30-
30+
3131
@Nonnull
32-
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
32+
public static String getString(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
3333
Object value = getValue(object, path);
34-
if(value instanceof String) {
34+
if (value instanceof String) {
3535
return (String) value;
36-
}else {
36+
} else {
3737
throw new ParsingException("Unable to get " + path);
3838
}
3939
}
4040

4141
@Nonnull
42-
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
42+
public static Boolean getBoolean(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
4343
Object value = getValue(object, path);
44-
if(value instanceof Boolean) {
44+
if (value instanceof Boolean) {
4545
return (Boolean) value;
46-
}else {
46+
} else {
4747
throw new ParsingException("Unable to get " + path);
4848
}
4949
}
50-
50+
5151
@Nonnull
52-
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
52+
public static Number getNumber(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
5353
Object value = getValue(object, path);
54-
if(value instanceof Number) {
54+
if (value instanceof Number) {
5555
return (Number) value;
56-
}else {
56+
} else {
5757
throw new ParsingException("Unable to get " + path);
5858
}
5959
}
60-
60+
6161
@Nonnull
62-
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
62+
public static JsonObject getObject(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
6363
Object value = getValue(object, path);
64-
if(value instanceof JsonObject) {
64+
if (value instanceof JsonObject) {
6565
return (JsonObject) value;
66-
}else {
66+
} else {
6767
throw new ParsingException("Unable to get " + path);
6868
}
6969
}
70-
70+
7171
@Nonnull
72-
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException{
72+
public static JsonArray getArray(@Nonnull JsonObject object, @Nonnull String path) throws ParsingException {
7373
Object value = getValue(object, path);
74-
if(value instanceof JsonArray) {
74+
if (value instanceof JsonArray) {
7575
return (JsonArray) value;
76-
}else {
76+
} else {
7777
throw new ParsingException("Unable to get " + path);
7878
}
7979
}
8080

8181
@Nonnull
8282
public static List<Object> getValues(@Nonnull JsonArray array, @Nonnull String path) throws ParsingException {
83-
83+
8484
List<Object> result = new ArrayList<>();
8585
for (int i = 0; i < array.size(); i++) {
8686
JsonObject obj = array.getObject(i);

0 commit comments

Comments
 (0)