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;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
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;
|
|
|
|
import org.schabi.newpipe.extractor.stream.*;
|
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
import java.io.IOException;
|
2019-12-21 19:31:46 +01:00
|
|
|
import java.util.ArrayList;
|
2019-12-21 19:00:07 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-01-03 13:13:42 +01:00
|
|
|
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampChannelExtractor.getImageUrl;
|
|
|
|
|
2019-12-21 19:00:07 +01:00
|
|
|
public class BandcampStreamExtractor extends StreamExtractor {
|
|
|
|
|
|
|
|
private JSONObject albumJson;
|
|
|
|
private JSONObject current;
|
|
|
|
private Document document;
|
|
|
|
|
|
|
|
public BandcampStreamExtractor(StreamingService service, LinkHandler linkHandler) {
|
|
|
|
super(service, linkHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFetchPage(@Nonnull Downloader downloader) throws IOException, ExtractionException {
|
|
|
|
String html = downloader.get(getLinkHandler().getUrl()).responseBody();
|
|
|
|
document = Jsoup.parse(html);
|
|
|
|
albumJson = getAlbumInfoJson(html);
|
|
|
|
current = albumJson.getJSONObject("current");
|
|
|
|
|
|
|
|
if (albumJson.getJSONArray("trackinfo").length() > 1) {
|
|
|
|
// 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
|
|
|
*/
|
|
|
|
public static JSONObject getAlbumInfoJson(String html) throws ParsingException {
|
|
|
|
try {
|
|
|
|
return BandcampExtractorHelper.getJSONFromJavaScriptVariables(html, "TralbumData");
|
|
|
|
} catch (JSONException e) {
|
2019-12-21 23:14:23 +01:00
|
|
|
throw new ParsingException("Faulty JSON; page likely does not contain album data", e);
|
2019-12-22 00:42:26 +01:00
|
|
|
} catch (ArrayIndexOutOfBoundsException e) {
|
|
|
|
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 {
|
|
|
|
String[] parts = getUrl().split("/");
|
|
|
|
// 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
|
|
|
|
public String getUploaderName() throws ParsingException {
|
|
|
|
return albumJson.getString("artist");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public String getTextualUploadDate() throws ParsingException {
|
|
|
|
return current.getString("release_date");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public DateWrapper getUploadDate() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getThumbnailUrl() throws ParsingException {
|
2020-01-03 13:13:42 +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");
|
|
|
|
} catch (NullPointerException e) {
|
|
|
|
return "";
|
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
return BandcampExtractorHelper.smartConcatenate(
|
|
|
|
new String[]{
|
|
|
|
getStringOrNull(current, "about"),
|
|
|
|
getStringOrNull(current, "lyrics"),
|
|
|
|
getStringOrNull(current, "credits")
|
|
|
|
}, "\n\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Avoid exceptions like "<code>JSONObject["about"] not a string.</code>" and instead just return null.
|
|
|
|
* This is for the case that the actual JSON has something like <code>"about": null</code>.
|
|
|
|
*/
|
|
|
|
private String getStringOrNull(JSONObject jsonObject, String value) {
|
|
|
|
try {
|
|
|
|
return jsonObject.getString(value);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getAgeLimit() throws ParsingException {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLength() throws ParsingException {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getTimeStamp() throws ParsingException {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getViewCount() throws ParsingException {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getLikeCount() throws ParsingException {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public long getDislikeCount() throws ParsingException {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getDashMpdUrl() throws ParsingException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public String getHlsUrl() throws ParsingException {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<AudioStream> getAudioStreams() {
|
2019-12-21 19:31:46 +01:00
|
|
|
List<AudioStream> audioStreams = new ArrayList<>();
|
|
|
|
|
|
|
|
audioStreams.add(new AudioStream(
|
|
|
|
albumJson.getJSONArray("trackinfo").getJSONObject(0)
|
2019-12-22 02:55:54 +01:00
|
|
|
.getJSONObject("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() {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<VideoStream> getVideoOnlyStreams() {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<SubtitlesStream> getSubtitlesDefault() {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
2019-12-21 21:47:06 +01:00
|
|
|
public List<SubtitlesStream> getSubtitles(MediaFormat format) {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2019-12-21 23:14:23 +01:00
|
|
|
public StreamInfoItem getNextStream() {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 23:14:23 +01:00
|
|
|
public StreamInfoItemsCollector getRelatedStreams() {
|
2019-12-21 19:00:07 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getErrorMessage() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|