From 94a2d5be463704dc551b1dca330110c36846ea7c Mon Sep 17 00:00:00 2001 From: wb9688 Date: Thu, 27 Feb 2020 19:08:46 +0100 Subject: [PATCH] Fix thumbnail URLs starting with // --- .../extractors/YoutubeChannelExtractor.java | 9 +----- .../YoutubeChannelInfoItemExtractor.java | 5 ++-- .../extractors/YoutubePlaylistExtractor.java | 30 ++++++++++++------- .../YoutubePlaylistInfoItemExtractor.java | 14 +++++++-- .../extractors/YoutubeStreamExtractor.java | 13 ++++---- .../YoutubeStreamInfoItemExtractor.java | 12 +++++++- 6 files changed, 54 insertions(+), 29 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java index 452cfbbf..849953cc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelExtractor.java @@ -128,10 +128,6 @@ public class YoutubeChannelExtractor extends ChannelExtractor { String url = initialData.getObject("header").getObject("c4TabbedHeaderRenderer").getObject("avatar") .getArray("thumbnails").getObject(0).getString("url"); - // the first characters of the avatar URLs are different for each channel and some are not even valid URLs - if (url.startsWith("//")) { - url = url.substring(2); - } if (url.startsWith(HTTP)) { url = Utils.replaceHttpWithHttps(url); } else if (!url.startsWith(HTTPS)) { @@ -155,10 +151,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor { if (url == null || url.contains("s.ytimg.com") || url.contains("default_banner")) { return null; } - // the first characters of the banner URLs are different for each channel and some are not even valid URLs - if (url.startsWith("//")) { - url = url.substring(2); - } + if (url.startsWith(HTTP)) { url = Utils.replaceHttpWithHttps(url); } else if (!url.startsWith(HTTPS)) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java index e82c4377..6a571daa 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeChannelInfoItemExtractor.java @@ -42,14 +42,13 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor public String getThumbnailUrl() throws ParsingException { try { String url = channelInfoItem.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); - if (url.startsWith("//")) { - url = url.substring(2); - } + if (url.startsWith(HTTP)) { url = Utils.replaceHttpWithHttps(url); } else if (!url.startsWith(HTTPS)) { url = HTTPS + url; } + return url; } catch (Exception e) { throw new ParsingException("Could not get thumbnail url", e); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java index e32b42ef..66768b9c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistExtractor.java @@ -121,16 +121,30 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { @Override public String getThumbnailUrl() throws ParsingException { + String url = null; + try { - return playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer") + url = playlistInfo.getObject("thumbnailRenderer").getObject("playlistVideoThumbnailRenderer") .getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); } catch (Exception ignored) {} - try { - return initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail") - .getArray("thumbnails").getObject(0).getString("url"); - } catch (Exception e) { - throw new ParsingException("Could not get playlist thumbnail", e); + + if (url == null) { + try { + return initialData.getObject("microformat").getObject("microformatDataRenderer").getObject("thumbnail") + .getArray("thumbnails").getObject(0).getString("url"); + } catch (Exception ignored) {} } + + if (url != null && !url.isEmpty()) { + if (url.startsWith(HTTP)) { + url = Utils.replaceHttpWithHttps(url); + } else if (!url.startsWith(HTTPS)) { + url = HTTPS + url; + } + + return url; + } + throw new ParsingException("Could not get playlist thumbnail"); } @Override @@ -162,10 +176,6 @@ public class YoutubePlaylistExtractor extends PlaylistExtractor { try { String url = getUploaderInfo().getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); - // the first characters of the avatar URLs are different for each channel and some are not even valid URLs - if (url.startsWith("//")) { - url = url.substring(2); - } if (url.startsWith(HTTP)) { url = Utils.replaceHttpWithHttps(url); } else if (!url.startsWith(HTTPS)) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistInfoItemExtractor.java index 03bf9b22..031b3737 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubePlaylistInfoItemExtractor.java @@ -8,6 +8,8 @@ import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylist import org.schabi.newpipe.extractor.utils.Utils; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject; +import static org.schabi.newpipe.extractor.utils.Utils.HTTP; +import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; public class YoutubePlaylistInfoItemExtractor implements PlaylistInfoItemExtractor { private JsonObject playlistInfoItem; @@ -19,8 +21,16 @@ public class YoutubePlaylistInfoItemExtractor implements PlaylistInfoItemExtract @Override public String getThumbnailUrl() throws ParsingException { try { - return playlistInfoItem.getArray("thumbnails").getObject(0).getArray("thumbnails") - .getObject(0).getString("url"); + String url = playlistInfoItem.getArray("thumbnails").getObject(0) + .getArray("thumbnails").getObject(0).getString("url"); + + if (url.startsWith(HTTP)) { + url = Utils.replaceHttpWithHttps(url); + } else if (!url.startsWith(HTTPS)) { + url = HTTPS + url; + } + + return url; } catch (Exception e) { throw new ParsingException("Could not get thumbnail url", e); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index e985d1b9..144a7221 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -187,8 +187,15 @@ public class YoutubeStreamExtractor extends StreamExtractor { try { JsonArray thumbnails = playerResponse.getObject("videoDetails").getObject("thumbnail").getArray("thumbnails"); // the last thumbnail is the one with the highest resolution - return thumbnails.getObject(thumbnails.size() - 1).getString("url"); + String url = thumbnails.getObject(thumbnails.size() - 1).getString("url"); + if (url.startsWith(HTTP)) { + url = Utils.replaceHttpWithHttps(url); + } else if (!url.startsWith(HTTPS)) { + url = HTTPS + url; + } + + return url; } catch (Exception e) { throw new ParsingException("Could not get thumbnail url"); } @@ -362,10 +369,6 @@ public class YoutubeStreamExtractor extends StreamExtractor { String url = getVideoSecondaryInfoRenderer().getObject("owner").getObject("videoOwnerRenderer") .getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url"); - // the first characters of the avatar URLs are different for each channel and some are not even valid URLs - if (url.startsWith("//")) { - url = url.substring(2); - } if (url.startsWith(HTTP)) { url = Utils.replaceHttpWithHttps(url); } else if (!url.startsWith(HTTPS)) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java index d3cee8b0..da22179a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java @@ -16,6 +16,8 @@ import javax.annotation.Nullable; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject; import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint; +import static org.schabi.newpipe.extractor.utils.Utils.HTTP; +import static org.schabi.newpipe.extractor.utils.Utils.HTTPS; /* * Copyright (C) Christian Schabesberger 2016 @@ -196,8 +198,16 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { public String getThumbnailUrl() throws ParsingException { try { // TODO: Don't simply get the first item, but look at all thumbnails and their resolution - return videoInfo.getObject("thumbnail").getArray("thumbnails") + String url = videoInfo.getObject("thumbnail").getArray("thumbnails") .getObject(0).getString("url"); + + if (url.startsWith(HTTP)) { + url = Utils.replaceHttpWithHttps(url); + } else if (!url.startsWith(HTTPS)) { + url = HTTPS + url; + } + + return url; } catch (Exception e) { throw new ParsingException("Could not get thumbnail url", e); }