Skip to content

Commit

Permalink
Merge pull request #1061 from ChunkyProgrammer/develop/extract-yt-pla…
Browse files Browse the repository at this point in the history
…ylist-descrption

Extract playlists description
  • Loading branch information
TobiGr authored May 17, 2023
2 parents a9ca5c4 + c70bb83 commit 3c036a9
Show file tree
Hide file tree
Showing 14 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;

import javax.annotation.Nonnull;
Expand All @@ -21,6 +22,9 @@ public PlaylistExtractor(final StreamingService service, final ListLinkHandler l

public abstract long getStreamCount() throws ParsingException;

@Nonnull
public abstract Description getDescription() throws ParsingException;

@Nonnull
public String getThumbnailUrl() throws ParsingException {
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.utils.ExtractorHelper;

Expand Down Expand Up @@ -102,6 +103,11 @@ public static PlaylistInfo getInfo(final PlaylistExtractor extractor)
} catch (final Exception e) {
info.addError(e);
}
try {
info.setDescription(extractor.getDescription());
} catch (final Exception e) {
info.addError(e);
}
try {
info.setThumbnailUrl(extractor.getThumbnailUrl());
} catch (final Exception e) {
Expand Down Expand Up @@ -174,6 +180,7 @@ public static PlaylistInfo getInfo(final PlaylistExtractor extractor)
private String subChannelName;
private String subChannelAvatarUrl;
private long streamCount = 0;
private Description description;
private PlaylistType playlistType;

public String getThumbnailUrl() {
Expand Down Expand Up @@ -248,6 +255,14 @@ public void setStreamCount(final long streamCount) {
this.streamCount = streamCount;
}

public Description getDescription() {
return description;
}

public void setDescription(final Description description) {
this.description = description;
}

public PlaylistType getPlaylistType() {
return playlistType;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.schabi.newpipe.extractor.playlist;

import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.stream.Description;

import javax.annotation.Nullable;

Expand All @@ -13,6 +14,7 @@ public class PlaylistInfoItem extends InfoItem {
* How many streams this playlist have
*/
private long streamCount = 0;
private Description description;
private PlaylistInfo.PlaylistType playlistType;

public PlaylistInfoItem(final int serviceId, final String url, final String name) {
Expand Down Expand Up @@ -52,6 +54,14 @@ public void setStreamCount(final long streamCount) {
this.streamCount = streamCount;
}

public Description getDescription() {
return description;
}

public void setDescription(final Description description) {
this.description = description;
}

public PlaylistInfo.PlaylistType getPlaylistType() {
return playlistType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.schabi.newpipe.extractor.InfoItemExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.Description;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -31,6 +32,16 @@ public interface PlaylistInfoItemExtractor extends InfoItemExtractor {
*/
long getStreamCount() throws ParsingException;

/**
* Get the description of the playlist if there is any.
* Otherwise, an {@link Description#EMPTY_DESCRIPTION EMPTY_DESCRIPTION} is returned.
* @return the playlist's description
*/
@Nonnull
default Description getDescription() throws ParsingException {
return Description.EMPTY_DESCRIPTION;
}

/**
* @return the type of this playlist, see {@link PlaylistInfo.PlaylistType} for a description
* of types. If not overridden always returns {@link PlaylistInfo.PlaylistType#NORMAL}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public PlaylistInfoItem extract(final PlaylistInfoItemExtractor extractor)
} catch (final Exception e) {
addError(e);
}
try {
resultItem.setDescription(extractor.getDescription());
} catch (final Exception e) {
addError(e);
}
try {
resultItem.setPlaylistType(extractor.getPlaylistType());
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
Expand All @@ -20,10 +22,12 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem.BandcampPlaylistStreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import java.io.IOException;
import java.util.Objects;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -108,6 +112,32 @@ public long getStreamCount() {
return trackInfo.size();
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
final Element tInfo = document.getElementById("trackInfo");
if (tInfo == null) {
throw new ParsingException("Could not find trackInfo in document");
}
final Elements about = tInfo.getElementsByClass("tralbum-about");
final Elements credits = tInfo.getElementsByClass("tralbum-credits");
final Element license = document.getElementById("license");
if (about.isEmpty() && credits.isEmpty() && license == null) {
return Description.EMPTY_DESCRIPTION;
}
final StringBuilder sb = new StringBuilder();
if (!about.isEmpty()) {
sb.append(Objects.requireNonNull(about.first()).html());
}
if (!credits.isEmpty()) {
sb.append(Objects.requireNonNull(credits.first()).html());
}
if (license != null) {
sb.append(license.html());
}
return new Description(sb.toString(), Description.HTML);
}

@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws ExtractionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;
Expand Down Expand Up @@ -65,6 +66,16 @@ public long getStreamCount() {
return playlistInfo.getLong("videosLength");
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
final String description = playlistInfo.getString("description");
if (isNullOrEmpty(description)) {
return Description.EMPTY_DESCRIPTION;
}
return new Description(description, Description.PLAIN_TEXT);
}

@Nonnull
@Override
public String getSubChannelName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.Description;

import javax.annotation.Nonnull;

import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public class PeertubePlaylistInfoItemExtractor implements PlaylistInfoItemExtractor {

final JsonObject item;
Expand Down Expand Up @@ -54,4 +57,14 @@ public boolean isUploaderVerified() throws ParsingException {
public long getStreamCount() throws ParsingException {
return item.getInt("videosLength");
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
final String description = item.getString("description");
if (isNullOrEmpty(description)) {
return Description.EMPTY_DESCRIPTION;
}
return new Description(description, Description.PLAIN_TEXT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

Expand Down Expand Up @@ -118,6 +119,16 @@ public long getStreamCount() {
return playlist.getLong("track_count");
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
final String description = playlist.getString("description");
if (isNullOrEmpty(description)) {
return Description.EMPTY_DESCRIPTION;
}
return new Description(description, Description.PLAIN_TEXT);
}

@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.JsonUtils;
Expand Down Expand Up @@ -169,6 +170,12 @@ public long getStreamCount() {
return ListExtractor.ITEM_COUNT_INFINITE;
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
return Description.EMPTY_DESCRIPTION;
}

@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
import org.schabi.newpipe.extractor.utils.Utils;
Expand Down Expand Up @@ -294,6 +295,17 @@ public long getStreamCount() throws ParsingException {
return ITEM_COUNT_UNKNOWN;
}

@Nonnull
@Override
public Description getDescription() throws ParsingException {
final String description = getTextFromObject(
getPlaylistInfo().getObject("description"),
true
);

return new Description(description, Description.HTML);
}

@Nonnull
@Override
public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, ExtractionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
import org.schabi.newpipe.extractor.services.BasePlaylistExtractorTest;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampPlaylistExtractor;
import org.schabi.newpipe.extractor.stream.Description;
import org.schabi.newpipe.extractor.stream.StreamInfoItem;

import java.io.IOException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertContains;
import static org.schabi.newpipe.extractor.ServiceList.Bandcamp;

/**
Expand Down Expand Up @@ -135,6 +137,16 @@ public void testStreamCount() throws ParsingException {
assertEquals(5, extractor.getStreamCount());
}

@Test
public void testDescription() throws ParsingException {
final Description description = extractor.getDescription();
assertNotEquals(Description.EMPTY_DESCRIPTION, description);
assertContains("Artwork by Shona Radcliffe", description.getContent()); // about
assertContains("All tracks written, produced and recorded by Mac Benson",
description.getContent()); // credits
assertContains("all rights reserved", description.getContent()); // license
}

@Override
public void testUploaderVerified() throws Exception {
assertFalse(extractor.isUploaderVerified());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ void testGetStreamCount() {
ExtractorAsserts.assertGreaterOrEqual(39, extractor.getStreamCount());
}

@Test
void testGetDescription() throws ParsingException {
ExtractorAsserts.assertContains("épisodes de Shocking", extractor.getDescription().getContent());
}

@Test
void testGetSubChannelUrl() {
assertEquals("https://skeptikon.fr/video-channels/metadechoc_channel", extractor.getSubChannelUrl());
Expand Down
Loading

0 comments on commit 3c036a9

Please sign in to comment.