[Youtube] Extract getThumbnailUrl into method and change getUploaderName

This commit is contained in:
Xiang Rong Lin 2020-02-02 18:04:15 +01:00 committed by XiangRongLin
parent 0efb854d27
commit d74265c846
1 changed files with 21 additions and 16 deletions

View File

@ -1,12 +1,8 @@
package org.schabi.newpipe.extractor.services.youtube.extractors; package org.schabi.newpipe.extractor.services.youtube.extractors;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import java.io.IOException; import java.io.IOException;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.StreamingService;
@ -50,7 +46,16 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
return doc.select("ol[class*=\"playlist-videos-list\"] li").first().attr("data-thumbnail-url"); Element li = doc.select("ol[class*=\"playlist-videos-list\"] li").first();
String videoId = li.attr("data-video-id");
if (videoId != null && !videoId.isEmpty()) {
//higher quality
return getThumbnailUrlFromId(videoId);
} else {
//lower quality
return doc.select("ol[class*=\"playlist-videos-list\"] li").first()
.attr("data-thumbnail-url");
}
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get playlist thumbnail", e); throw new ParsingException("Could not get playlist thumbnail", e);
} }
@ -146,21 +151,18 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
} }
@Override @Override
public long getDuration() throws ParsingException { public long getDuration() {
//Not present in doc //Not present in doc
return 0; return 0;
} }
@Override @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
try { String uploaderName = li.attr("data-video-username");
return li.select( if (uploaderName == null || uploaderName.isEmpty()) {
"div[class=\"playlist-video-description\"]" throw new ParsingException("Could not get uploader name");
+ "span[class=\"video-uploader-byline\"]") } else {
.first() return uploaderName;
.text();
} catch (Exception e) {
throw new ParsingException("Could not get uploader", e);
} }
} }
@ -184,8 +186,7 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
return "https://i.ytimg.com/vi/" + streamLinkHandlerFactory.fromUrl(getUrl()).getId() return getThumbnailUrlFromId(streamLinkHandlerFactory.fromUrl(getUrl()).getId());
+ "/hqdefault.jpg";
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }
@ -193,4 +194,8 @@ public class YoutubeMixPlaylistExtractor extends PlaylistExtractor {
}); });
} }
} }
private String getThumbnailUrlFromId(String videoId) {
return "https://i.ytimg.com/vi/" + videoId + "/hqdefault.jpg";
}
} }