Use suggested try-if code style

This commit is contained in:
wb9688 2020-02-29 17:18:50 +01:00
parent a6a63e9570
commit bfe3eb1409
4 changed files with 58 additions and 38 deletions

View file

@ -108,15 +108,14 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor {
if (url == null) { if (url == null) {
try { try {
return initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail") url = initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail")
.getArray("thumbnails").getObject(0).getString("url"); .getArray("thumbnails").getObject(0).getString("url");
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (url == null) throw new ParsingException("Could not get playlist thumbnail");
} }
if (url != null && !url.isEmpty()) { return fixThumbnailUrl(url);
return fixThumbnailUrl(url);
}
throw new ParsingException("Could not get playlist thumbnail");
} }
@Override @Override

View file

@ -116,16 +116,20 @@ public class YoutubeStreamExtractor extends StreamExtractor {
public String getName() throws ParsingException { public String getName() throws ParsingException {
assertPageFetched(); assertPageFetched();
String title = null; String title = null;
try { try {
title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title")); title = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("title"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (title == null) { if (title == null) {
try { try {
title = playerResponse.getObject("videoDetails").getString("title"); title = playerResponse.getObject("videoDetails").getString("title");
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (title == null) throw new ParsingException("Could not get name");
} }
if (title != null) return title;
throw new ParsingException("Could not get name"); return title;
} }
@Override @Override
@ -259,17 +263,21 @@ public class YoutubeStreamExtractor extends StreamExtractor {
public long getViewCount() throws ParsingException { public long getViewCount() throws ParsingException {
assertPageFetched(); assertPageFetched();
String views = null; String views = null;
try { try {
views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount") views = getTextFromObject(getVideoPrimaryInfoRenderer().getObject("viewCount")
.getObject("videoViewCountRenderer").getObject("viewCount")); .getObject("videoViewCountRenderer").getObject("viewCount"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (views == null) { if (views == null) {
try { try {
views = playerResponse.getObject("videoDetails").getString("viewCount"); views = playerResponse.getObject("videoDetails").getString("viewCount");
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (views == null) throw new ParsingException("Could not get view count");
} }
if (views != null) return Long.parseLong(Utils.removeNonDigitCharacters(views));
throw new ParsingException("Could not get view count"); return Long.parseLong(Utils.removeNonDigitCharacters(views));
} }
@Override @Override
@ -340,17 +348,21 @@ public class YoutubeStreamExtractor extends StreamExtractor {
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
assertPageFetched(); assertPageFetched();
String uploaderName = null; String uploaderName = null;
try { try {
uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner") uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner")
.getObject("videoOwnerRenderer").getObject("title")); .getObject("videoOwnerRenderer").getObject("title"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (uploaderName == null) { if (uploaderName == null) {
try { try {
uploaderName = playerResponse.getObject("videoDetails").getString("author"); uploaderName = playerResponse.getObject("videoDetails").getString("author");
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (uploaderName == null) throw new ParsingException("Could not get uploader name");
} }
if (uploaderName != null) return uploaderName;
throw new ParsingException("Could not get uploader name"); return uploaderName;
} }
@Nonnull @Nonnull
@ -910,9 +922,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
urlAndItags.put(streamUrl, itagItem); urlAndItags.put(streamUrl, itagItem);
} }
} catch (UnsupportedEncodingException ignored) { } catch (UnsupportedEncodingException ignored) {}
}
} }
} }

View file

@ -87,10 +87,13 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
@Override @Override
public long getDuration() throws ParsingException { public long getDuration() throws ParsingException {
if (getStreamType() == StreamType.LIVE_STREAM) return -1; if (getStreamType() == StreamType.LIVE_STREAM) return -1;
String duration = null; String duration = null;
try { try {
duration = getTextFromObject(videoInfo.getObject("lengthText")); duration = getTextFromObject(videoInfo.getObject("lengthText"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (duration == null) { if (duration == null) {
try { try {
for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) { for (Object thumbnailOverlay : videoInfo.getArray("thumbnailOverlays")) {
@ -100,58 +103,64 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
} }
} }
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (duration == null) throw new ParsingException("Could not get duration");
} }
if (duration != null) return YoutubeParsingHelper.parseDurationString(duration);
throw new ParsingException("Could not get duration"); return YoutubeParsingHelper.parseDurationString(duration);
} }
@Override @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
String name = null; String name = null;
try { try {
name = getTextFromObject(videoInfo.getObject("longBylineText")); name = getTextFromObject(videoInfo.getObject("longBylineText"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (name == null) { if (name == null) {
try { try {
name = getTextFromObject(videoInfo.getObject("ownerText")); name = getTextFromObject(videoInfo.getObject("ownerText"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (name == null) {
try {
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
} catch (Exception ignored) {}
if (name == null) throw new ParsingException("Could not get uploader name");
}
} }
if (name == null) {
try { return name;
name = getTextFromObject(videoInfo.getObject("shortBylineText"));
} catch (Exception ignored) {}
}
if (name != null && !name.isEmpty()) return name;
throw new ParsingException("Could not get uploader name");
} }
@Override @Override
public String getUploaderUrl() throws ParsingException { public String getUploaderUrl() throws ParsingException {
String url = null;
try { try {
String url = null; url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText")
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
} catch (Exception ignored) {}
if (url == null) {
try { try {
url = getUrlFromNavigationEndpoint(videoInfo.getObject("longBylineText") url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
.getArray("runs").getObject(0).getObject("navigationEndpoint")); .getArray("runs").getObject(0).getObject("navigationEndpoint"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (url == null) {
try {
url = getUrlFromNavigationEndpoint(videoInfo.getObject("ownerText")
.getArray("runs").getObject(0).getObject("navigationEndpoint"));
} catch (Exception ignored) {}
}
if (url == null) { if (url == null) {
try { try {
url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText") url = getUrlFromNavigationEndpoint(videoInfo.getObject("shortBylineText")
.getArray("runs").getObject(0).getObject("navigationEndpoint")); .getArray("runs").getObject(0).getObject("navigationEndpoint"));
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (url == null) throw new ParsingException("Could not get uploader url");
} }
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("is empty");
}
return url;
} catch (Exception e) {
throw new ParsingException("Could not get uploader url");
} }
return url;
} }
@Nullable @Nullable

View file

@ -6,6 +6,8 @@ import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
import java.io.IOException;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class YoutubeParsingHelperTest { public class YoutubeParsingHelperTest {
@ -15,7 +17,7 @@ public class YoutubeParsingHelperTest {
} }
@Test @Test
public void testIsHardcodedClientVersionValid() { public void testIsHardcodedClientVersionValid() throws IOException {
assertTrue("Hardcoded client version is not valid anymore", assertTrue("Hardcoded client version is not valid anymore",
YoutubeParsingHelper.isHardcodedClientVersionValid()); YoutubeParsingHelper.isHardcodedClientVersionValid());
} }