From 692b2d06f48d469f965b74093c14060a77f6c555 Mon Sep 17 00:00:00 2001 From: Fynn Godau Date: Mon, 25 May 2020 18:51:31 +0200 Subject: [PATCH] Bandcamp: read info item data in place and not in advance --- .../extractors/BandcampChannelExtractor.java | 14 +- .../BandcampChannelInfoItemExtractor.java | 30 ++-- .../extractors/BandcampExtractorHelper.java | 2 +- .../extractors/BandcampFeaturedExtractor.java | 11 +- .../extractors/BandcampPlaylistExtractor.java | 19 +-- .../BandcampPlaylistInfoItemExtractor.java | 50 ++----- ...campPlaylistInfoItemFeaturedExtractor.java | 41 ++++++ .../BandcampRadioInfoItemExtractor.java | 2 +- .../extractors/BandcampSearchExtractor.java | 4 +- .../BandcampStreamInfoItemExtractor.java | 134 ------------------ ...campDiscographStreamInfoItemExtractor.java | 46 ++++++ ...ndcampPlaylistStreamInfoItemExtractor.java | 69 +++++++++ ...BandcampSearchStreamInfoItemExtractor.java | 48 +++++++ .../BandcampStreamInfoItemExtractor.java | 54 +++++++ .../BandcampFeaturedExtractorTest.java | 3 +- .../BandcampPlaylistExtractorTest.java | 2 - 16 files changed, 303 insertions(+), 226 deletions(-) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemFeaturedExtractor.java delete mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamInfoItemExtractor.java create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampDiscographStreamInfoItemExtractor.java create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampPlaylistStreamInfoItemExtractor.java create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampSearchStreamInfoItemExtractor.java create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampStreamInfoItemExtractor.java 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 4da4a44ef..446b962c7 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 @@ -11,6 +11,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem.BandcampDiscographStreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; @@ -89,18 +90,7 @@ public class BandcampChannelExtractor extends ChannelExtractor { if (!discograph.getString("item_type").equals("track")) continue; - collector.commit(new BandcampStreamInfoItemExtractor( - discograph.getString("title"), - BandcampExtractorHelper.getStreamUrlFromIds( - discograph.getLong("band_id"), - discograph.getLong("item_id"), - discograph.getString("item_type") - ), - BandcampExtractorHelper.getImageUrl( - discograph.getLong("art_id"), true - ), - discograph.getString("band_name") - )); + collector.commit(new BandcampDiscographStreamInfoItemExtractor(discograph, getUrl())); } return new InfoItemsPage<>(collector, null); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelInfoItemExtractor.java index 2ff853407..3baf45b3e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampChannelInfoItemExtractor.java @@ -8,43 +8,35 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; public class BandcampChannelInfoItemExtractor implements ChannelInfoItemExtractor { - private String name, url, image, location; + private final Element resultInfo, searchResult; public BandcampChannelInfoItemExtractor(Element searchResult) { - - Element resultInfo = searchResult.getElementsByClass("result-info").first(); - - Element img = searchResult.getElementsByClass("art").first() - .getElementsByTag("img").first(); - if (img != null) { - image = img.attr("src"); - } - - name = resultInfo.getElementsByClass("heading").text(); - - location = resultInfo.getElementsByClass("subhead").text(); - - url = resultInfo.getElementsByClass("itemurl").text(); + this.searchResult = searchResult; + resultInfo = searchResult.getElementsByClass("result-info").first(); } @Override public String getName() throws ParsingException { - return name; + return resultInfo.getElementsByClass("heading").text(); } @Override public String getUrl() throws ParsingException { - return url; + return resultInfo.getElementsByClass("itemurl").text(); } @Override public String getThumbnailUrl() throws ParsingException { - return image; + Element img = searchResult.getElementsByClass("art").first() + .getElementsByTag("img").first(); + if (img != null) { + return img.attr("src"); + } else return null; } @Override public String getDescription() { - return location; + return resultInfo.getElementsByClass("subhead").text(); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java index 5371681a4..cfe5c2df2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java @@ -138,7 +138,7 @@ public class BandcampExtractorHelper { * @return Url of image with this ID in size 10 which is 1200x1200 (we could also choose size 0 * but we don't want something as large as 3460x3460 here, do we?) */ - static String getImageUrl(long id, boolean album) { + public static String getImageUrl(long id, boolean album) { return "https://f4.bcbits.com/img/" + (album ? 'a' : "") + id + "_10.jpg"; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java index d5d3c412d..82905d239 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java @@ -14,12 +14,13 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.kiosk.KioskExtractor; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemsCollector; import javax.annotation.Nonnull; import java.io.IOException; -public class BandcampFeaturedExtractor extends KioskExtractor { +public class BandcampFeaturedExtractor extends KioskExtractor { public static final String KIOSK_FEATURED = "Featured"; public static final String FEATURED_API_URL = "https://bandcamp.com/api/mobile/24/bootstrap_data"; @@ -41,9 +42,9 @@ public class BandcampFeaturedExtractor extends KioskExtractor { @Nonnull @Override - public InfoItemsPage getInitialPage() throws IOException, ExtractionException { + public InfoItemsPage getInitialPage() throws IOException, ExtractionException { - InfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId()); + PlaylistInfoItemsCollector c = new PlaylistInfoItemsCollector(getServiceId()); try { @@ -66,10 +67,10 @@ public class BandcampFeaturedExtractor extends KioskExtractor { continue; } - c.commit(new BandcampPlaylistInfoItemExtractor(featuredStory)); + c.commit(new BandcampPlaylistInfoItemFeaturedExtractor(featuredStory)); } - return new InfoItemsPage(c, null); + return new InfoItemsPage<>(c, null); } catch (JsonParserException e) { e.printStackTrace(); throw new ParsingException("JSON error", e); 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 ddc2b6b40..b57644400 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 @@ -12,6 +12,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.playlist.PlaylistExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem.BandcampPlaylistStreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; @@ -111,22 +112,12 @@ public class BandcampPlaylistExtractor extends PlaylistExtractor { if (trackInfo.size() < MAXIMUM_INDIVIDUAL_COVER_ARTS) { // Load cover art of every track individually - collector.commit(new BandcampStreamInfoItemExtractor( - track.getString("title"), - getUploaderUrl() + track.getString("title_link"), - "", - track.getLong("duration"), - getService() - )); + collector.commit(new BandcampPlaylistStreamInfoItemExtractor( + track, getUploaderUrl(), getService())); } else { // Pretend every track has the same cover art as the album - collector.commit(new BandcampStreamInfoItemExtractor( - track.getString("title"), - getUploaderUrl() + track.getString("title_link"), - getThumbnailUrl(), - "", - track.getLong("duration") - )); + collector.commit(new BandcampPlaylistStreamInfoItemExtractor( + track, getUploaderUrl(), getThumbnailUrl())); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemExtractor.java index 668dcddd7..b28d13d72 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemExtractor.java @@ -4,64 +4,42 @@ import com.grack.nanojson.JsonObject; import org.jsoup.nodes.Element; import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; -import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl; - public class BandcampPlaylistInfoItemExtractor implements PlaylistInfoItemExtractor { - - private String title, artist, url, cover; - private int trackCount; + private final Element searchResult, resultInfo; public BandcampPlaylistInfoItemExtractor(Element searchResult) { - - Element resultInfo = searchResult.getElementsByClass("result-info").first(); - - Element img = searchResult.getElementsByClass("art").first() - .getElementsByTag("img").first(); - if (img != null) { - cover = img.attr("src"); - } - - title = resultInfo.getElementsByClass("heading").text(); - url = resultInfo.getElementsByClass("itemurl").text(); - - artist = resultInfo.getElementsByClass("subhead").text() - .split(" by")[0]; - - String length = resultInfo.getElementsByClass("length").text(); - trackCount = Integer.parseInt(length.split(" track")[0]); - - } - - public BandcampPlaylistInfoItemExtractor(JsonObject featuredStory) { - title = featuredStory.getString("album_title"); - artist = featuredStory.getString("band_name"); - url = featuredStory.getString("item_url"); - cover = featuredStory.has("art_id") ? getImageUrl(featuredStory.getLong("art_id"), true) : ""; - trackCount = featuredStory.getInt("num_streamable_tracks"); + this.searchResult = searchResult; + resultInfo = searchResult.getElementsByClass("result-info").first(); } @Override public String getUploaderName() { - return artist; + return resultInfo.getElementsByClass("subhead").text() + .split(" by")[0]; } @Override public long getStreamCount() { - return trackCount; + String length = resultInfo.getElementsByClass("length").text(); + return Integer.parseInt(length.split(" track")[0]); } @Override public String getName() { - return title; + return resultInfo.getElementsByClass("heading").text(); } @Override public String getUrl() { - return url; + return resultInfo.getElementsByClass("itemurl").text(); } @Override public String getThumbnailUrl() { - return cover; + Element img = searchResult.getElementsByClass("art").first() + .getElementsByTag("img").first(); + if (img != null) { + return img.attr("src"); + } else return null; } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemFeaturedExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemFeaturedExtractor.java new file mode 100644 index 000000000..814794284 --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistInfoItemFeaturedExtractor.java @@ -0,0 +1,41 @@ +package org.schabi.newpipe.extractor.services.bandcamp.extractors; + +import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemExtractor; + +import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl; + +public class BandcampPlaylistInfoItemFeaturedExtractor implements PlaylistInfoItemExtractor { + + private final JsonObject featuredStory; + + public BandcampPlaylistInfoItemFeaturedExtractor(JsonObject featuredStory) { + this.featuredStory = featuredStory; + } + + @Override + public String getUploaderName() { + return featuredStory.getString("band_name"); + } + + @Override + public long getStreamCount() { + return featuredStory.getInt("num_streamable_tracks"); + } + + @Override + public String getName() { + return featuredStory.getString("album_title"); + } + + @Override + public String getUrl() { + return featuredStory.getString("item_url"); + } + + @Override + public String getThumbnailUrl() { + return featuredStory.has("art_id") ? getImageUrl(featuredStory.getLong("art_id"), true) : ""; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampRadioInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampRadioInfoItemExtractor.java index 808c1afd3..ebc843195 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampRadioInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampRadioInfoItemExtractor.java @@ -22,7 +22,7 @@ public class BandcampRadioInfoItemExtractor implements StreamInfoItemExtractor { @Override public long getDuration() { - /* Duration is only present in the more detailed information that has to be queried seperately. + /* Duration is only present in the more detailed information that has to be queried separately. * Because the servers would probably not like over 300 queries every time someone opens the kiosk, * we're just providing 0 here. */ diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSearchExtractor.java index 3f6a35b1a..8897d0406 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampSearchExtractor.java @@ -13,6 +13,8 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; import org.schabi.newpipe.extractor.search.SearchExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem.BandcampPlaylistStreamInfoItemExtractor; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem.BandcampSearchStreamInfoItemExtractor; import javax.annotation.Nonnull; import java.io.IOException; @@ -62,7 +64,7 @@ public class BandcampSearchExtractor extends SearchExtractor { break; case "TRACK": - collector.commit(new BandcampStreamInfoItemExtractor(searchResult)); + collector.commit(new BandcampSearchStreamInfoItemExtractor(searchResult, null)); break; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamInfoItemExtractor.java deleted file mode 100644 index 4c2cd2319..000000000 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamInfoItemExtractor.java +++ /dev/null @@ -1,134 +0,0 @@ -// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later - -package org.schabi.newpipe.extractor.services.bandcamp.extractors; - -import org.jsoup.nodes.Element; -import org.schabi.newpipe.extractor.StreamingService; -import org.schabi.newpipe.extractor.exceptions.ExtractionException; -import org.schabi.newpipe.extractor.exceptions.ParsingException; -import org.schabi.newpipe.extractor.localization.DateWrapper; -import org.schabi.newpipe.extractor.stream.StreamExtractor; -import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; -import org.schabi.newpipe.extractor.stream.StreamType; - -import javax.annotation.Nullable; -import java.io.IOException; - -public class BandcampStreamInfoItemExtractor implements StreamInfoItemExtractor { - - private String title; - private String url; - private String cover; - private String artist; - private long duration; - private StreamingService service; - - public BandcampStreamInfoItemExtractor(String title, String url, String artist, long duration, StreamingService service) { - this(title, url, null, artist, duration); - this.service = service; - } - - public BandcampStreamInfoItemExtractor(String title, String url, String cover, String artist) { - this(title, url, cover, artist, -1); - } - - public BandcampStreamInfoItemExtractor(Element searchResult) { - Element resultInfo = searchResult.getElementsByClass("result-info").first(); - - Element img = searchResult.getElementsByClass("art").first() - .getElementsByTag("img").first(); - if (img != null) { - cover = img.attr("src"); - } - - title = resultInfo.getElementsByClass("heading").text(); - url = resultInfo.getElementsByClass("itemurl").text(); - - String subhead = resultInfo.getElementsByClass("subhead").text(); - String[] splitBy = subhead.split(" by"); - if (splitBy.length > 1) { - artist = subhead.split(" by")[1]; - } - } - - public BandcampStreamInfoItemExtractor(String title, String url, String cover, String artist, long duration) { - this.title = title; - this.url = url; - this.cover = cover; - this.artist = artist; - this.duration = duration; - } - - @Override - public StreamType getStreamType() { - return StreamType.AUDIO_STREAM; - } - - @Override - public long getDuration() { - return duration; - } - - @Override - public long getViewCount() { - return -1; - } - - @Override - public String getUploaderName() { - return artist; - } - - @Override - public String getUploaderUrl() { - return null; - } - - @Nullable - @Override - public String getTextualUploadDate() { - return null; // TODO - } - - @Nullable - @Override - public DateWrapper getUploadDate() { - return null; - } - - @Override - public String getName() throws ParsingException { - return title; - } - - @Override - public String getUrl() throws ParsingException { - return url; - } - - /** - * There is no guarantee that every track of an album has the same cover art, so it needs to be fetched - * per-track if in playlist view - */ - @Override - public String getThumbnailUrl() throws ParsingException { - if (cover != null) return cover; - else { - try { - StreamExtractor extractor = service.getStreamExtractor(getUrl()); - extractor.fetchPage(); - return extractor.getThumbnailUrl(); - } catch (ExtractionException | IOException e) { - throw new ParsingException("could not download cover art location", e); - } - } - } - - /** - * There are no ads just like that, duh - */ - @Override - public boolean isAd() { - return false; - } -} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampDiscographStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampDiscographStreamInfoItemExtractor.java new file mode 100644 index 000000000..4c76b404d --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampDiscographStreamInfoItemExtractor.java @@ -0,0 +1,46 @@ +package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem; + +import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper; + +public class BandcampDiscographStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor { + + private final JsonObject discograph; + public BandcampDiscographStreamInfoItemExtractor(JsonObject discograph, String uploaderUrl) { + super(uploaderUrl); + + this.discograph = discograph; + } + + @Override + public String getUploaderName() { + return discograph.getString("band_name"); + } + + @Override + public String getName() { + return discograph.getString("title"); + } + + @Override + public String getUrl() throws ParsingException { + return BandcampExtractorHelper.getStreamUrlFromIds( + discograph.getLong("band_id"), + discograph.getLong("item_id"), + discograph.getString("item_type") + ); + } + + @Override + public String getThumbnailUrl() throws ParsingException { + return BandcampExtractorHelper.getImageUrl( + discograph.getLong("art_id"), true + ); + } + + @Override + public long getDuration() { + return -1; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampPlaylistStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampPlaylistStreamInfoItemExtractor.java new file mode 100644 index 000000000..6faf397ae --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampPlaylistStreamInfoItemExtractor.java @@ -0,0 +1,69 @@ +// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem; + +import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.StreamingService; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.stream.StreamExtractor; + +import java.io.IOException; + + +public class BandcampPlaylistStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor { + + private final JsonObject track; + private String substituteCoverUrl; + private StreamingService service; + + public BandcampPlaylistStreamInfoItemExtractor(JsonObject track, String uploaderUrl, StreamingService service) { + super(uploaderUrl); + this.track = track; + this.service = service; + } + + public BandcampPlaylistStreamInfoItemExtractor(JsonObject track, String uploaderUrl, String substituteCoverUrl) { + this(track, uploaderUrl, (StreamingService) null); + this.substituteCoverUrl = substituteCoverUrl; + } + + @Override + public String getName() { + return track.getString("title"); + } + + @Override + public String getUrl() { + return getUploaderUrl() + track.getString("title_link"); + } + + @Override + public long getDuration() { + return track.getLong("duration"); + } + + @Override + public String getUploaderName() { + return ""; + } + + /** + * Each track can have its own cover art. Therefore, unless a substitute is provided, + * the thumbnail is extracted using a stream extractor. + */ + @Override + public String getThumbnailUrl() throws ParsingException { + if (substituteCoverUrl != null) { + return substituteCoverUrl; + } else { + try { + StreamExtractor extractor = service.getStreamExtractor(getUrl()); + extractor.fetchPage(); + return extractor.getThumbnailUrl(); + } catch (ExtractionException | IOException e) { + throw new ParsingException("could not download cover art location", e); + } + } + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampSearchStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampSearchStreamInfoItemExtractor.java new file mode 100644 index 000000000..51fbe019d --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampSearchStreamInfoItemExtractor.java @@ -0,0 +1,48 @@ +package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem; + +import org.jsoup.nodes.Element; +import org.schabi.newpipe.extractor.exceptions.ParsingException; + +public class BandcampSearchStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor { + + private final Element resultInfo, searchResult; + + public BandcampSearchStreamInfoItemExtractor(Element searchResult, String uploaderUrl) { + super(uploaderUrl); + this.searchResult = searchResult; + resultInfo = searchResult.getElementsByClass("result-info").first(); + } + + @Override + public String getUploaderName() throws ParsingException { + String subhead = resultInfo.getElementsByClass("subhead").text(); + String[] splitBy = subhead.split(" by"); + if (splitBy.length > 1) { + return splitBy[1]; + } else throw new ParsingException("Uploader name was not found as expected"); + } + + @Override + public String getName() throws ParsingException { + return resultInfo.getElementsByClass("heading").text(); + } + + @Override + public String getUrl() throws ParsingException { + return resultInfo.getElementsByClass("itemurl").text(); + } + + @Override + public String getThumbnailUrl() throws ParsingException { + Element img = searchResult.getElementsByClass("art").first() + .getElementsByTag("img").first(); + if (img != null) { + return img.attr("src"); + } else return null; + } + + @Override + public long getDuration() { + return -1; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampStreamInfoItemExtractor.java new file mode 100644 index 000000000..a8a3ceb46 --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/streaminfoitem/BandcampStreamInfoItemExtractor.java @@ -0,0 +1,54 @@ +package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem; + +import org.schabi.newpipe.extractor.localization.DateWrapper; +import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; +import org.schabi.newpipe.extractor.stream.StreamType; + +import javax.annotation.Nullable; + +/** + * Implements methods that return a constant value for better readability in + * subclasses. + */ +public abstract class BandcampStreamInfoItemExtractor implements StreamInfoItemExtractor { + private final String uploaderUrl; + + public BandcampStreamInfoItemExtractor(String uploaderUrl) { + this.uploaderUrl = uploaderUrl; + } + + @Override + public StreamType getStreamType() { + return StreamType.AUDIO_STREAM; + } + + @Override + public long getViewCount() { + return -1; + } + + @Override + public String getUploaderUrl() { + return uploaderUrl; + } + + @Nullable + @Override + public String getTextualUploadDate() { + return null; + } + + @Nullable + @Override + public DateWrapper getUploadDate() { + return null; + } + + /** + * There are no ads just like that, duh + */ + @Override + public boolean isAd() { + return false; + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java index e02e45932..09f516be9 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/BandcampFeaturedExtractorTest.java @@ -8,6 +8,7 @@ import org.schabi.newpipe.DownloaderTestImpl; import org.schabi.newpipe.extractor.InfoItem; 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.services.bandcamp.extractors.BandcampFeaturedExtractor; import java.io.IOException; @@ -32,7 +33,7 @@ public class BandcampFeaturedExtractorTest { @Test public void testFeaturedCount() throws ExtractionException, IOException { - List list = extractor.getInitialPage().getItems(); + List list = extractor.getInitialPage().getItems(); assertTrue(list.size() > 1); } 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 8af486a1d..88836efab 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 @@ -11,8 +11,6 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampPlaylistExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor; -import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import java.io.IOException;