From e12ddaef7f7ab8c447df2310490e5a37d23bd84a Mon Sep 17 00:00:00 2001 From: Fynn Godau Date: Sun, 22 Dec 2019 12:53:15 +0100 Subject: [PATCH] Bandcamp playlist and search extractors: better and more tests --- .../extractors/BandcampChannelExtractor.java | 1 + .../extractors/BandcampPlaylistExtractor.java | 3 +- .../BandcampPlaylistExtractorTest.java | 36 ++++++++++++++++++- .../bandcamp/BandcampSearchExtractorTest.java | 7 +++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java index 3f39603e..ddb8f336 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelExtractor.java @@ -6,6 +6,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.jsoup.Jsoup; +import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.channel.ChannelExtractor; diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java index 78c5f021..4544644e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java @@ -7,6 +7,7 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.downloader.Downloader; +import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; @@ -50,7 +51,7 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { if (trackInfo.length() <= 1) { // In this case, we are actually viewing a track page! - throw new ExtractionException("Page is actually a track, not an album"); + throw new ContentNotAvailableException("Album needs to be purchased or is actually a track"); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampPlaylistExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampPlaylistExtractorTest.java index e7482dfb..c6572974 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampPlaylistExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampPlaylistExtractorTest.java @@ -1,17 +1,26 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + package org.schabi.newpipe.extractor.services.bandcamp; import org.junit.BeforeClass; import org.junit.Test; import org.schabi.newpipe.DownloaderTestImpl; import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampPlaylistExtractor; +import org.schabi.newpipe.extractor.stream.StreamInfoItem; import java.io.IOException; +import java.util.List; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ServiceList.bandcamp; +/** + * Tests for {@link BandcampPlaylistExtractor} + */ public class BandcampPlaylistExtractorTest { @BeforeClass @@ -19,6 +28,9 @@ public class BandcampPlaylistExtractorTest { NewPipe.init(DownloaderTestImpl.getInstance()); } + /** + * Test whether playlists contain the correct amount of items + */ @Test public void testCount() throws ExtractionException, IOException { PlaylistExtractor extractor = bandcamp.getPlaylistExtractor("https://macbenson.bandcamp.com/album/coming-of-age"); @@ -26,4 +38,26 @@ public class BandcampPlaylistExtractorTest { assertEquals(5, extractor.getStreamCount()); } + + /** + * Tests whether different stream thumbnails (track covers) get loaded correctly + */ + @Test + public void testDifferentTrackCovers() throws ExtractionException, IOException { + PlaylistExtractor extractor = bandcamp.getPlaylistExtractor("https://zachbensonarchive.bandcamp.com/album/results-of-boredom"); + extractor.fetchPage(); + + List l = extractor.getInitialPage().getItems(); + assertEquals(extractor.getThumbnailUrl(), l.get(0).getThumbnailUrl()); + assertNotEquals(extractor.getThumbnailUrl(), l.get(5).getThumbnailUrl()); + } + + /** + * Test playlists with locked content + */ + @Test(expected = ContentNotAvailableException.class) + public void testLockedContent() throws ExtractionException, IOException { + PlaylistExtractor extractor = bandcamp.getPlaylistExtractor("https://billwurtz.bandcamp.com/album/high-enough"); + extractor.fetchPage(); + } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSearchExtractorTest.java index 98bdc843..45624fe7 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampSearchExtractorTest.java @@ -10,8 +10,10 @@ import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampChannelInfoItemExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampPlaylistInfoItemExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampSearchExtractor; import java.io.IOException; @@ -77,11 +79,14 @@ public class BandcampSearchExtractorTest { InfoItem minecraft = extractor.getInitialPage() .getItems().get(0); - // C418's artist profile should be the first result, no? + // Minecraft volume alpha should be the first result, no? assertEquals("Minecraft - Volume Alpha", minecraft.getName()); assertTrue(minecraft.getThumbnailUrl().endsWith(".jpg")); assertTrue(minecraft.getThumbnailUrl().contains("f4.bcbits.com/img/")); assertEquals("https://c418.bandcamp.com/album/minecraft-volume-alpha", minecraft.getUrl()); + // Verify that playlists get counted correctly + assertEquals(24, ((PlaylistInfoItem) minecraft).getStreamCount()); + } }