Skip to content

Commit bfe3eb1

Browse files
committed
Use suggested try-if code style
1 parent a6a63e9 commit bfe3eb1

4 files changed

Lines changed: 58 additions & 38 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,14 @@ public String getThumbnailUrl() throws ParsingException {
108108

109109
if (url == null) {
110110
try {
111-
return initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
111+
url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
112112
.getArray("thumbnails").getObject(0).getString("url");
113113
} catch (Exception ignored) {}
114-
}
115114

116-
if (url != null && !url.isEmpty()) {
117-
return fixThumbnailUrl(url);
115+
if (url == null) throw new ParsingException("Could not get playlist thumbnail");
118116
}
119-
throw new ParsingException("Could not get playlist thumbnail");
117+
118+
return fixThumbnailUrl(url);
120119
}
121120

122121
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,20 @@ public YoutubeStreamExtractor(StreamingService service, LinkHandler linkHandler)
116116
public String getName() throws ParsingException {
117117
assertPageFetched();
118118
String title = null;
119+
119120
try {
120121
title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
121122
} catch (Exception ignored) {}
123+
122124
if (title == null) {
123125
try {
124126
title = playerResponse.getObject("videoDetails").getString("title");
125127
} catch (Exception ignored) {}
128+
129+
if (title == null) throw new ParsingException("Could not get name");
126130
}
127-
if (title != null) return title;
128-
throw new ParsingException("Could not get name");
131+
132+
return title;
129133
}
130134

131135
@Override
@@ -259,17 +263,21 @@ public long getTimeStamp() throws ParsingException {
259263
public long getViewCount() throws ParsingException {
260264
assertPageFetched();
261265
String views = null;
266+
262267
try {
263268
views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
264269
.getObject("videoViewCountRenderer").getObject("viewCount"));
265270
} catch (Exception ignored) {}
271+
266272
if (views == null) {
267273
try {
268274
views = playerResponse.getObject("videoDetails").getString("viewCount");
269275
} catch (Exception ignored) {}
276+
277+
if (views == null) throw new ParsingException("Could not get view count");
270278
}
271-
if (views != null) return Long.parseLong(Utils.removeNonDigitCharacters(views));
272-
throw new ParsingException("Could not get view count");
279+
280+
return Long.parseLong(Utils.removeNonDigitCharacters(views));
273281
}
274282

275283
@Override
@@ -340,17 +348,21 @@ public String getUploaderUrl() throws ParsingException {
340348
public String getUploaderName() throws ParsingException {
341349
assertPageFetched();
342350
String uploaderName = null;
351+
343352
try {
344353
uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
345354
.getObject("videoOwnerRenderer").getObject("title"));
346355
} catch (Exception ignored) {}
356+
347357
if (uploaderName == null) {
348358
try {
349359
uploaderName = playerResponse.getObject("videoDetails").getString("author");
350360
} catch (Exception ignored) {}
361+
362+
if (uploaderName == null) throw new ParsingException("Could not get uploader name");
351363
}
352-
if (uploaderName != null) return uploaderName;
353-
throw new ParsingException("Could not get uploader name");
364+
365+
return uploaderName;
354366
}
355367

356368
@Nonnull
@@ -910,9 +922,7 @@ private Map<String, ItagItem> getItags(String streamingDataKey, ItagItem.ItagTyp
910922

911923
urlAndItags.put(streamUrl, itagItem);
912924
}
913-
} catch (UnsupportedEncodingException ignored) {
914-
915-
}
925+
} catch (UnsupportedEncodingException ignored) {}
916926
}
917927
}
918928

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ public String getName() throws ParsingException {
8787
@Override
8888
public long getDuration() throws ParsingException {
8989
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
90+
9091
String duration = null;
92+
9193
try {
9294
duration = getTextFromObject(videoInfo.getObject("lengthText"));
9395
} catch (Exception ignored) {}
96+
9497
if (duration == null) {
9598
try {
9699
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
@@ -100,58 +103,64 @@ public long getDuration() throws ParsingException {
100103
}
101104
}
102105
} catch (Exception ignored) {}
106+
107+
if (duration == null) throw new ParsingException("Could not get duration");
103108
}
104-
if (duration != null) return YoutubeParsingHelper.parseDurationString(duration);
105-
throw new ParsingException("Could not get duration");
109+
110+
return YoutubeParsingHelper.parseDurationString(duration);
106111
}
107112

108113
@Override
109114
public String getUploaderName() throws ParsingException {
110115
String name = null;
116+
111117
try {
112118
name = getTextFromObject(videoInfo.getObject("longBylineText"));
113119
} catch (Exception ignored) {}
120+
114121
if (name == null) {
115122
try {
116123
name = getTextFromObject(videoInfo.getObject("ownerText"));
117124
} catch (Exception ignored) {}
125+
126+
if (name == null) {
127+
try {
128+
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
129+
} catch (Exception ignored) {}
130+
131+
if (name == null) throw new ParsingException("Could not get uploader name");
132+
}
118133
}
119-
if (name == null) {
120-
try {
121-
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
122-
} catch (Exception ignored) {}
123-
}
124-
if (name != null && !name.isEmpty()) return name;
125-
throw new ParsingException("Could not get uploader name");
134+
135+
return name;
126136
}
127137

128138
@Override
129139
public String getUploaderUrl() throws ParsingException {
140+
String url = null;
141+
130142
try {
131-
String url = null;
143+
url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
144+
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
145+
} catch (Exception ignored) {}
146+
147+
if (url == null) {
132148
try {
133-
url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
149+
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
134150
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
135151
} catch (Exception ignored) {}
136-
if (url == null) {
137-
try {
138-
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
139-
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
140-
} catch (Exception ignored) {}
141-
}
152+
142153
if (url == null) {
143154
try {
144155
url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText")
145156
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
146157
} catch (Exception ignored) {}
158+
159+
if (url == null) throw new ParsingException("Could not get uploader url");
147160
}
148-
if (url == null || url.isEmpty()) {
149-
throw new IllegalArgumentException("is empty");
150-
}
151-
return url;
152-
} catch (Exception e) {
153-
throw new ParsingException("Could not get uploader url");
154161
}
162+
163+
return url;
155164
}
156165

157166
@Nullable

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.schabi.newpipe.extractor.NewPipe;
77
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
88

9+
import java.io.IOException;
10+
911
import static org.junit.Assert.assertTrue;
1012

1113
public class YoutubeParsingHelperTest {
@@ -15,7 +17,7 @@ public static void setUp() {
1517
}
1618

1719
@Test
18-
public void testIsHardcodedClientVersionValid() {
20+
public void testIsHardcodedClientVersionValid() throws IOException {
1921
assertTrue("Hardcoded client version is not valid anymore",
2022
YoutubeParsingHelper.isHardcodedClientVersionValid());
2123
}

0 commit comments

Comments
 (0)