Created
July 24, 2020 16:44
-
-
Save alidaoud/f30adf12e2d48ec477ef699a8db3fed1 to your computer and use it in GitHub Desktop.
class creates a thumbnail url of a youtube video using its youtube url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * this class creates a thumbnail url of a youtube video using its url | |
| * | |
| * supports the below link formats: | |
| * => the original format of the youtube url e.g. https://www.youtube.com/watch?v=<watchId>&feature=youtu.be | |
| * => the shortcut format of the youtube url e.g. https://youtu.be/<watchId> | |
| */ | |
| public class ThumbnailUrlExtractor { | |
| private static final String LINK_FORMAT_1 = "watch?v="; | |
| private static final String LINK_FORMAT_2 = "youtu.be/"; | |
| private static final String SUFFIX_DEFAULT = "/default.jpg"; //120x90 | |
| private static final String SUFFIX_HD = "/hqdefault.jpg"; //480x360 | |
| private static final String SUFFIX_MEDIUM = "/mqdefault.jpg"; //320x180 | |
| private static final String SUFFIX_STANDARD = "/sddefault.jpg"; //640x480 | |
| private static final String SUFFIX_MAX_RES = "/maxresdefault.jpg"; //1920x1080 | |
| public static final int QUALITY_DEFAULT = 0; | |
| public static final int QUALITY_HD = 1; | |
| public static final int QUALITY_MEDIUM = 2; | |
| public static final int QUALITY_STANDARD = 3; | |
| public static final int QUALITY_MAX_RES = 4; | |
| public static String extractThumbnailUrl(String youtubeUrl, int thumbnailQuality) { | |
| if (youtubeUrl.contains(LINK_FORMAT_1)) | |
| return createThumbnailUrlFormat1(youtubeUrl, thumbnailQuality); | |
| if (youtubeUrl.contains(LINK_FORMAT_2)) | |
| return createThumbnailUrlFormat2(youtubeUrl, thumbnailQuality); | |
| return null; | |
| } | |
| /** | |
| * this method extract the watch id of a youtube video from its link which have the following format >> https://www.youtube.com/watch?v=<watchId>&feature=youtu.be | |
| * | |
| * @param youtubeUrl the youtube url of the video | |
| * @param thumbnailQuality int value holds the quality of the thumbnail url | |
| * | |
| * @return the watch id to be sent to buildThumbnailUrl in order to build the final url | |
| */ | |
| private static String createThumbnailUrlFormat1(String youtubeUrl, int thumbnailQuality) { | |
| int startIndex, endIndex = 0; | |
| String youtubeWatchId; | |
| startIndex = youtubeUrl.indexOf(LINK_FORMAT_1); | |
| for (int i = startIndex; i < youtubeUrl.length(); i++) { | |
| if (youtubeUrl.charAt(i) == '&') { | |
| break; | |
| } | |
| endIndex = i; | |
| } | |
| //get the youtube watch id which comes between watch?v= and the & symbol | |
| youtubeWatchId = youtubeUrl.substring(startIndex + LINK_FORMAT_1.length(), endIndex + 1); | |
| return buildThumbnailUrl(youtubeWatchId, thumbnailQuality); | |
| } | |
| /** | |
| * this method extract the watch id of a youtube video from its link which have the following format >> https://youtu.be/<watchId> | |
| * | |
| * @param youtubeUrl the youtube url of the video | |
| * @param thumbnailQuality int value holds the quality of the thumbnail url | |
| * @return the watch id to be sent to buildThumbnailUrl in order to build the final url | |
| */ | |
| private static String createThumbnailUrlFormat2(String youtubeUrl, int thumbnailQuality) { | |
| int startIndex; | |
| String youtubeWatchId; | |
| //set the start index to start from the / after the LINK_FORMAT_2 | |
| //the original link would look like >> https://youtu.be/<watchId> | |
| startIndex = youtubeUrl.indexOf(LINK_FORMAT_2); | |
| //get the youtube watch id which comes after the youtu.be/ | |
| youtubeWatchId = youtubeUrl.substring(startIndex + LINK_FORMAT_2.length()); | |
| return buildThumbnailUrl(youtubeWatchId, thumbnailQuality); | |
| } | |
| /** | |
| * this method build the final url after extracting the watch id from the youtube url | |
| * | |
| * @param youtubeWatchId youtube unique watch id | |
| * @param thumbnailQuality int value holds the quality of the thumbnail | |
| * | |
| * @return the final url of the thumbnail | |
| */ | |
| private static String buildThumbnailUrl(String youtubeWatchId, int thumbnailQuality){ | |
| StringBuilder builder = new StringBuilder(); | |
| //build the final url of the thumbnail which has the below format | |
| //https://img.youtube.com/vi/<youtube-video-id-here>/<thumbnail-quality-here>.jpg | |
| builder.append("https://img.youtube.com/vi/"); | |
| builder.append(youtubeWatchId); | |
| //append the corresponding quality suffix | |
| switch (thumbnailQuality) { | |
| case QUALITY_DEFAULT: | |
| builder.append(SUFFIX_DEFAULT); | |
| break; | |
| case QUALITY_HD: | |
| builder.append(SUFFIX_HD); | |
| break; | |
| case QUALITY_MEDIUM: | |
| builder.append(SUFFIX_MEDIUM); | |
| break; | |
| case QUALITY_STANDARD: | |
| builder.append(SUFFIX_STANDARD); | |
| break; | |
| case QUALITY_MAX_RES: | |
| builder.append(SUFFIX_MAX_RES); | |
| break; | |
| } | |
| return builder.toString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment