Reimplement YoutubePlaylistInfoItemExtractor

This commit is contained in:
wb9688 2020-02-23 19:45:45 +01:00 committed by TobiGr
parent 8aea4d445b
commit 4462cbe3f1
3 changed files with 22 additions and 56 deletions

View File

@ -37,7 +37,7 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
try { try {
return channelInfoItem.getObject("thumbnails").getArray("thumbnails").getObject(0).getString("url"); return channelInfoItem.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }
@ -74,7 +74,7 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor
return Long.parseLong(Utils.removeNonDigitCharacters(channelInfoItem.getObject("videoCountText") return Long.parseLong(Utils.removeNonDigitCharacters(channelInfoItem.getObject("videoCountText")
.getArray("runs").getObject(0).getString("text"))); .getArray("runs").getObject(0).getString("text")));
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get name", e); throw new ParsingException("Could not get stream count", e);
} }
} }
@ -83,7 +83,7 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor
try { try {
return channelInfoItem.getObject("descriptionSnippet").getArray("runs").getObject(0).getString("text"); return channelInfoItem.getObject("descriptionSnippet").getArray("runs").getObject(0).getString("text");
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Could not get description url", e); throw new ParsingException("Could not get description", e);
} }
} }
} }

View File

@ -1,97 +1,63 @@
package org.schabi.newpipe.extractor.services.youtube.extractors; package org.schabi.newpipe.extractor.services.youtube.extractors;
import org.jsoup.nodes.Element; import com.grack.nanojson.JsonObject;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Utils; import org.schabi.newpipe.extractor.utils.Utils;
public class YoutubePlaylistInfoItemExtractor implements PlaylistInfoItemExtractor { public class YoutubePlaylistInfoItemExtractor implements PlaylistInfoItemExtractor {
private final Element el; private JsonObject playlistInfoItem;
public YoutubePlaylistInfoItemExtractor(Element el) { public YoutubePlaylistInfoItemExtractor(JsonObject playlistInfoItem) {
this.el = el; this.playlistInfoItem = playlistInfoItem;
} }
@Override @Override
public String getThumbnailUrl() throws ParsingException { public String getThumbnailUrl() throws ParsingException {
String url;
try { try {
Element te = el.select("div[class=\"yt-thumb video-thumb\"]").first() return playlistInfoItem.getArray("thumbnails").getObject(0).getArray("thumbnails")
.select("img").first(); .getObject(0).getString("url");
url = te.attr("abs:src");
if (url.contains(".gif")) {
url = te.attr("abs:data-thumb");
}
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Failed to extract playlist thumbnail url", e); throw new ParsingException("Could not get thumbnail url", e);
} }
return url;
} }
@Override @Override
public String getName() throws ParsingException { public String getName() throws ParsingException {
String name;
try { try {
final Element title = el.select("[class=\"yt-lockup-title\"]").first() return playlistInfoItem.getObject("title").getString("simpleText");
.select("a").first();
name = title == null ? "" : title.text();
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Failed to extract playlist name", e); throw new ParsingException("Could not get name", e);
} }
return name;
} }
@Override @Override
public String getUrl() throws ParsingException { public String getUrl() throws ParsingException {
try { try {
final Element a = el.select("div[class=\"yt-lockup-meta\"]") String id = playlistInfoItem.getString("playlistId");
.select("ul[class=\"yt-lockup-meta-info\"]") return YoutubePlaylistLinkHandlerFactory.getInstance().getUrl(id);
.select("li").select("a").first();
if (a != null) {
return a.attr("abs:href");
}
// this is for yt premium playlists
return el.select("h3[class=\"yt-lockup-title\"").first()
.select("a").first()
.attr("abs:href");
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Failed to extract playlist url", e); throw new ParsingException("Could not get url", e);
} }
} }
@Override @Override
public String getUploaderName() throws ParsingException { public String getUploaderName() throws ParsingException {
String name;
try { try {
final Element div = el.select("div[class=\"yt-lockup-byline\"]").first() return playlistInfoItem.getObject("longBylineText").getArray("runs").getObject(0).getString("text");
.select("a").first();
name = div.text();
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Failed to extract playlist uploader", e); throw new ParsingException("Could not get uploader name", e);
} }
return name;
} }
@Override @Override
public long getStreamCount() throws ParsingException { public long getStreamCount() throws ParsingException {
try { try {
final Element count = el.select("span[class=\"formatted-video-count-label\"]").first() return Long.parseLong(Utils.removeNonDigitCharacters(playlistInfoItem.getString("videoCount")));
.select("b").first();
return count == null ? 0 : Long.parseLong(Utils.removeNonDigitCharacters(count.text()));
} catch (Exception e) { } catch (Exception e) {
throw new ParsingException("Failed to extract playlist stream count", e); throw new ParsingException("Could not get stream count", e);
} }
} }
} }

View File

@ -132,7 +132,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
} else if (((JsonObject) item).getObject("channelRenderer") != null) { } else if (((JsonObject) item).getObject("channelRenderer") != null) {
collector.commit(new YoutubeChannelInfoItemExtractor(((JsonObject) item).getObject("channelRenderer"))); collector.commit(new YoutubeChannelInfoItemExtractor(((JsonObject) item).getObject("channelRenderer")));
} else if (((JsonObject) item).getObject("playlistRenderer") != null) { } else if (((JsonObject) item).getObject("playlistRenderer") != null) {
// collector.commit(new YoutubePlaylistInfoItemExtractor(((JsonObject) item).getObject("playlistRenderer"))); collector.commit(new YoutubePlaylistInfoItemExtractor(((JsonObject) item).getObject("playlistRenderer")));
} }
} }
return collector; return collector;