2019-12-21 19:00:07 +01:00
|
|
|
// Created by Fynn Godau 2019, licensed GNU GPL version 3 or later
|
|
|
|
|
|
|
|
package org.schabi.newpipe.extractor.services.bandcamp.extractors;
|
|
|
|
|
2022-03-18 17:50:25 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2022-03-18 17:50:25 +01:00
|
|
|
|
2019-12-21 19:00:07 +01:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
2020-03-17 20:40:25 +01:00
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
import org.jsoup.select.Elements;
|
2019-12-21 19:00:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.MediaFormat;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.downloader.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
|
|
|
|
import org.schabi.newpipe.extractor.localization.DateWrapper;
|
2021-04-13 17:55:19 +02:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItemsCollector;
|
2022-03-18 17:50:25 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.AudioStream;
|
|
|
|
import org.schabi.newpipe.extractor.stream.Description;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType;
|
|
|
|
import org.schabi.newpipe.extractor.stream.VideoStream;
|
2021-02-21 13:51:12 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.JsonUtils;
|
2021-02-21 12:35:14 +01:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2019-12-21 19:00:07 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-12-21 19:31:46 +01:00
|
|
|
import java.util.ArrayList;
|
2020-11-19 22:33:52 +01:00
|
|
|
import java.util.Collections;
|
2019-12-21 19:00:07 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2022-03-18 17:50:25 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
2020-01-03 13:13:42 +01:00
|
|
|
|
2019-12-21 19:00:07 +01:00
|
|
|
public class BandcampStreamExtractor extends StreamExtractor {
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
private JsonObject albumJson;
|
|
|
|
private JsonObject current;
|
2019-12-21 19:00:07 +01:00
|
|
|
private Document document;
|
|
|
|
|
2020-11-24 09:41:40 +01:00
|
|
|
public BandcampStreamExtractor(final StreamingService service, final LinkHandler linkHandler) {
|
2019-12-21 19:00:07 +01:00
|
|
|
super(service, linkHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-03-18 17:50:25 +01:00
|
|
|
public void onFetchPage(@Nonnull final Downloader downloader)
|
|
|
|
throws IOException, ExtractionException {
|
2020-11-24 09:41:40 +01:00
|
|
|
final String html = downloader.get(getLinkHandler().getUrl()).responseBody();
|
2019-12-21 19:00:07 +01:00
|
|
|
document = Jsoup.parse(html);
|
|
|
|
albumJson = getAlbumInfoJson(html);
|
2020-03-19 11:18:29 +01:00
|
|
|
current = albumJson.getObject("current");
|
2019-12-21 19:00:07 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
if (albumJson.getArray("trackinfo").size() > 1) {
|
2019-12-21 19:00:07 +01:00
|
|
|
// In this case, we are actually viewing an album page!
|
|
|
|
throw new ExtractionException("Page is actually an album, not a track");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the JSON that contains album's metadata from page
|
|
|
|
*
|
|
|
|
* @param html Website
|
|
|
|
* @return Album metadata JSON
|
2019-12-22 02:55:54 +01:00
|
|
|
* @throws ParsingException In case of a faulty website
|
2019-12-21 19:00:07 +01:00
|
|
|
*/
|
2020-11-24 09:41:40 +01:00
|
|
|
public static JsonObject getAlbumInfoJson(final String html) throws ParsingException {
|
2019-12-21 19:00:07 +01:00
|
|
|
try {
|
2021-02-21 13:51:12 +01:00
|
|
|
return JsonUtils.getJsonData(html, "data-tralbum");
|
2020-11-24 09:41:40 +01:00
|
|
|
} catch (final JsonParserException e) {
|
2019-12-21 23:14:23 +01:00
|
|
|
throw new ParsingException("Faulty JSON; page likely does not contain album data", e);
|
2020-11-24 09:41:40 +01:00
|
|
|
} catch (final ArrayIndexOutOfBoundsException e) {
|
2019-12-22 00:42:26 +01:00
|
|
|
throw new ParsingException("JSON does not exist", e);
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getName() throws ParsingException {
|
|
|
|
return current.getString("title");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getUploaderUrl() throws ParsingException {
|
2020-11-24 09:41:40 +01:00
|
|
|
final String[] parts = getUrl().split("/");
|
2019-12-21 19:00:07 +01:00
|
|
|
// https: (/) (/) * .bandcamp.com (/) and leave out the rest
|
|
|
|
return "https://" + parts[2] + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getUrl() throws ParsingException {
|
|
|
|
return albumJson.getString("url").replace("http://", "https://");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2022-03-18 17:50:25 +01:00
|
|
|
public String getUploaderName() throws ParsingException {
|
2019-12-21 19:00:07 +01:00
|
|
|
return albumJson.getString("artist");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2020-04-20 22:46:54 +02:00
|
|
|
public String getTextualUploadDate() {
|
2020-06-04 14:09:21 +02:00
|
|
|
return current.getString("publish_date");
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2020-06-04 14:09:21 +02:00
|
|
|
public DateWrapper getUploadDate() throws ParsingException {
|
2020-06-04 19:36:58 +02:00
|
|
|
return BandcampExtractorHelper.parseDate(getTextualUploadDate());
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2022-03-18 17:50:25 +01:00
|
|
|
if (albumJson.isNull("art_id")) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return getImageUrl(albumJson.getLong("art_id"), true);
|
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getUploaderAvatarUrl() {
|
2019-12-22 01:14:35 +01:00
|
|
|
try {
|
|
|
|
return document.getElementsByClass("band-photo").first().attr("src");
|
2020-11-24 09:41:40 +01:00
|
|
|
} catch (final NullPointerException e) {
|
2019-12-22 01:14:35 +01:00
|
|
|
return "";
|
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2020-03-17 20:40:25 +01:00
|
|
|
public Description getDescription() {
|
2021-02-21 12:35:14 +01:00
|
|
|
final String s = Utils.nonEmptyAndNullJoin(
|
|
|
|
"\n\n",
|
2019-12-21 19:00:07 +01:00
|
|
|
new String[]{
|
2020-03-19 11:18:29 +01:00
|
|
|
current.getString("about"),
|
|
|
|
current.getString("lyrics"),
|
|
|
|
current.getString("credits")
|
2021-02-21 12:35:14 +01:00
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
);
|
2020-03-17 20:40:25 +01:00
|
|
|
return new Description(s, Description.PLAIN_TEXT);
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<AudioStream> getAudioStreams() {
|
2020-11-24 09:41:40 +01:00
|
|
|
final List<AudioStream> audioStreams = new ArrayList<>();
|
2019-12-21 19:31:46 +01:00
|
|
|
|
|
|
|
audioStreams.add(new AudioStream(
|
2020-03-19 11:18:29 +01:00
|
|
|
albumJson.getArray("trackinfo").getObject(0)
|
|
|
|
.getObject("file").getString("mp3-128"),
|
2019-12-21 19:31:46 +01:00
|
|
|
MediaFormat.MP3, 128
|
|
|
|
));
|
|
|
|
return audioStreams;
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<VideoStream> getVideoStreams() {
|
2021-02-16 20:25:48 +01:00
|
|
|
return Collections.emptyList();
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<VideoStream> getVideoOnlyStreams() {
|
2021-02-16 20:25:48 +01:00
|
|
|
return Collections.emptyList();
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 23:14:23 +01:00
|
|
|
public StreamType getStreamType() {
|
2019-12-21 19:31:46 +01:00
|
|
|
return StreamType.AUDIO_STREAM;
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-04-13 17:55:19 +02:00
|
|
|
public PlaylistInfoItemsCollector getRelatedItems() {
|
2022-03-18 17:50:25 +01:00
|
|
|
final PlaylistInfoItemsCollector collector = new PlaylistInfoItemsCollector(getServiceId());
|
|
|
|
final Elements recommendedAlbums = document.getElementsByClass("recommended-album");
|
2021-04-13 17:55:19 +02:00
|
|
|
|
2022-03-18 17:50:25 +01:00
|
|
|
for (final Element album : recommendedAlbums) {
|
2021-04-13 17:55:19 +02:00
|
|
|
collector.commit(new BandcampRelatedPlaylistInfoItemExtractor(album));
|
|
|
|
}
|
|
|
|
return collector;
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-17 20:40:25 +01:00
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getCategory() {
|
|
|
|
// Get first tag from html, which is the artist's Genre
|
2022-03-18 17:50:25 +01:00
|
|
|
return document.getElementsByClass("tralbum-tags").stream()
|
|
|
|
.flatMap(element -> element.getElementsByClass("tag").stream())
|
|
|
|
.map(Element::text)
|
|
|
|
.findFirst()
|
|
|
|
.orElse("");
|
2020-03-17 20:40:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getLicence() {
|
2022-03-18 17:50:25 +01:00
|
|
|
/* Tests resulted in this mapping of ints to licence:
|
|
|
|
https://cloud.disroot.org/s/ZTWBxbQ9fKRmRWJ/preview (screenshot from a Bandcamp artist's
|
|
|
|
account) */
|
2020-03-17 20:40:25 +01:00
|
|
|
|
2022-03-18 17:50:25 +01:00
|
|
|
switch (current.getInt("license_type")) {
|
2020-03-17 20:40:25 +01:00
|
|
|
case 1:
|
|
|
|
return "All rights reserved ©";
|
|
|
|
case 2:
|
|
|
|
return "CC BY-NC-ND 3.0";
|
|
|
|
case 3:
|
|
|
|
return "CC BY-NC-SA 3.0";
|
|
|
|
case 4:
|
|
|
|
return "CC BY-NC 3.0";
|
|
|
|
case 5:
|
|
|
|
return "CC BY-ND 3.0";
|
|
|
|
case 6:
|
|
|
|
return "CC BY 3.0";
|
2020-06-04 19:27:39 +02:00
|
|
|
case 8:
|
|
|
|
return "CC BY-SA 3.0";
|
2020-03-17 20:40:25 +01:00
|
|
|
default:
|
2020-06-04 18:26:58 +02:00
|
|
|
return "Unknown";
|
2020-03-17 20:40:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public List<String> getTags() {
|
2020-11-24 09:41:40 +01:00
|
|
|
final Elements tagElements = document.getElementsByAttributeValue("itemprop", "keywords");
|
2020-03-17 20:40:25 +01:00
|
|
|
|
2021-02-20 00:23:32 +01:00
|
|
|
final List<String> tags = new ArrayList<>();
|
2020-03-17 20:40:25 +01:00
|
|
|
|
2020-11-24 09:41:40 +01:00
|
|
|
for (final Element e : tagElements) {
|
2020-03-17 20:40:25 +01:00
|
|
|
tags.add(e.text());
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags;
|
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|