Reimplement YoutubePlaylistInfoItemExtractor
This commit is contained in:
parent
8aea4d445b
commit
4462cbe3f1
3 changed files with 22 additions and 56 deletions
|
@ -37,7 +37,7 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor
|
|||
@Override
|
||||
public String getThumbnailUrl() throws ParsingException {
|
||||
try {
|
||||
return channelInfoItem.getObject("thumbnails").getArray("thumbnails").getObject(0).getString("url");
|
||||
return channelInfoItem.getObject("thumbnail").getArray("thumbnails").getObject(0).getString("url");
|
||||
} catch (Exception 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")
|
||||
.getArray("runs").getObject(0).getString("text")));
|
||||
} 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 {
|
||||
return channelInfoItem.getObject("descriptionSnippet").getArray("runs").getObject(0).getString("text");
|
||||
} catch (Exception e) {
|
||||
throw new ParsingException("Could not get description url", e);
|
||||
throw new ParsingException("Could not get description", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,97 +1,63 @@
|
|||
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.playlist.PlaylistInfoItemExtractor;
|
||||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
|
||||
import org.schabi.newpipe.extractor.utils.Utils;
|
||||
|
||||
public class YoutubePlaylistInfoItemExtractor implements PlaylistInfoItemExtractor {
|
||||
private final Element el;
|
||||
private JsonObject playlistInfoItem;
|
||||
|
||||
public YoutubePlaylistInfoItemExtractor(Element el) {
|
||||
this.el = el;
|
||||
public YoutubePlaylistInfoItemExtractor(JsonObject playlistInfoItem) {
|
||||
this.playlistInfoItem = playlistInfoItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getThumbnailUrl() throws ParsingException {
|
||||
String url;
|
||||
|
||||
try {
|
||||
Element te = el.select("div[class=\"yt-thumb video-thumb\"]").first()
|
||||
.select("img").first();
|
||||
url = te.attr("abs:src");
|
||||
|
||||
if (url.contains(".gif")) {
|
||||
url = te.attr("abs:data-thumb");
|
||||
}
|
||||
return playlistInfoItem.getArray("thumbnails").getObject(0).getArray("thumbnails")
|
||||
.getObject(0).getString("url");
|
||||
} 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
|
||||
public String getName() throws ParsingException {
|
||||
String name;
|
||||
try {
|
||||
final Element title = el.select("[class=\"yt-lockup-title\"]").first()
|
||||
.select("a").first();
|
||||
|
||||
name = title == null ? "" : title.text();
|
||||
return playlistInfoItem.getObject("title").getString("simpleText");
|
||||
} catch (Exception e) {
|
||||
throw new ParsingException("Failed to extract playlist name", e);
|
||||
throw new ParsingException("Could not get name", e);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrl() throws ParsingException {
|
||||
try {
|
||||
final Element a = el.select("div[class=\"yt-lockup-meta\"]")
|
||||
.select("ul[class=\"yt-lockup-meta-info\"]")
|
||||
.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");
|
||||
|
||||
String id = playlistInfoItem.getString("playlistId");
|
||||
return YoutubePlaylistLinkHandlerFactory.getInstance().getUrl(id);
|
||||
} catch (Exception e) {
|
||||
throw new ParsingException("Failed to extract playlist url", e);
|
||||
throw new ParsingException("Could not get url", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUploaderName() throws ParsingException {
|
||||
String name;
|
||||
|
||||
try {
|
||||
final Element div = el.select("div[class=\"yt-lockup-byline\"]").first()
|
||||
.select("a").first();
|
||||
|
||||
name = div.text();
|
||||
return playlistInfoItem.getObject("longBylineText").getArray("runs").getObject(0).getString("text");
|
||||
} catch (Exception e) {
|
||||
throw new ParsingException("Failed to extract playlist uploader", e);
|
||||
throw new ParsingException("Could not get uploader name", e);
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getStreamCount() throws ParsingException {
|
||||
try {
|
||||
final Element count = el.select("span[class=\"formatted-video-count-label\"]").first()
|
||||
.select("b").first();
|
||||
|
||||
return count == null ? 0 : Long.parseLong(Utils.removeNonDigitCharacters(count.text()));
|
||||
return Long.parseLong(Utils.removeNonDigitCharacters(playlistInfoItem.getString("videoCount")));
|
||||
} catch (Exception e) {
|
||||
throw new ParsingException("Failed to extract playlist stream count", e);
|
||||
throw new ParsingException("Could not get stream count", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -132,7 +132,7 @@ public class YoutubeSearchExtractor extends SearchExtractor {
|
|||
} else if (((JsonObject) item).getObject("channelRenderer") != null) {
|
||||
collector.commit(new YoutubeChannelInfoItemExtractor(((JsonObject) item).getObject("channelRenderer")));
|
||||
} 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;
|
||||
|
|
Loading…
Reference in a new issue