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 34bd3c6a2..547a0356e 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 @@ -28,23 +28,6 @@ public class BandcampExtractorHelper { public static final String BASE_URL = "https://bandcamp.com"; public static final String BASE_API_URL = BASE_URL + "/api"; - /** - *
Get an attribute of a web page as JSON - * - *
Originally a part of bandcampDirect.
- * - * @param html The HTML where the JSON we're looking for is stored inside a - * variable inside some JavaScript block - * @param variable Name of the variable - * @return The JsonObject stored in the variable with this name - */ - public static JsonObject getJsonData(final String html, final String variable) - throws JsonParserException, ArrayIndexOutOfBoundsException { - final Document document = Jsoup.parse(html); - final String json = document.getElementsByAttribute(variable).attr(variable); - return JsonParser.object().from(json); - } - /** * Translate all these parameters together to the URL of the corresponding album or track * using the mobile API 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 58a701d90..8cc21a6e7 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 @@ -21,7 +21,7 @@ import javax.annotation.Nonnull; import java.io.IOException; import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl; -import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getJsonData; +import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData; import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson; public class BandcampPlaylistExtractor extends PlaylistExtractor { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java index 99a19cecd..344663525 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java @@ -17,6 +17,7 @@ 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 org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Utils; import javax.annotation.Nonnull; @@ -62,7 +63,7 @@ public class BandcampStreamExtractor extends StreamExtractor { */ public static JsonObject getAlbumInfoJson(final String html) throws ParsingException { try { - return BandcampExtractorHelper.getJsonData(html, "data-tralbum"); + return JsonUtils.getJsonData(html, "data-tralbum"); } catch (final JsonParserException e) { throw new ParsingException("Faulty JSON; page likely does not contain album data", e); } catch (final ArrayIndexOutOfBoundsException e) { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampChannelLinkHandlerFactory.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampChannelLinkHandlerFactory.java index 2cf956cc9..727aec404 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampChannelLinkHandlerFactory.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampChannelLinkHandlerFactory.java @@ -9,6 +9,7 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper; +import org.schabi.newpipe.extractor.utils.JsonUtils; import java.io.IOException; import java.util.List; @@ -25,7 +26,7 @@ public class BandcampChannelLinkHandlerFactory extends ListLinkHandlerFactory { final String response = NewPipe.getDownloader().get(url).responseBody(); // Use band data embedded in website to extract ID - final JsonObject bandData = BandcampExtractorHelper.getJsonData(response, "data-band"); + final JsonObject bandData = JsonUtils.getJsonData(response, "data-band"); return String.valueOf(bandData.getLong("id")); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java index ad132b5ac..7a5bd6c42 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java @@ -2,6 +2,10 @@ package org.schabi.newpipe.extractor.utils; import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; +import com.grack.nanojson.JsonParser; +import com.grack.nanojson.JsonParserException; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; import org.schabi.newpipe.extractor.exceptions.ParsingException; import javax.annotation.Nonnull; @@ -99,4 +103,34 @@ public class JsonUtils { return result; } + /** + *Get an attribute of a web page as JSON + * + *
Originally a part of bandcampDirect.
+ *Example HTML:
+ *+ * {@code + *+ *This is Sparta!
+ * } + *
Calling this function to get the attribute data-town
returns the JsonObject for
+ * {@code + * { + * "name": "Mycenae", + * "country": "Greece" + * } + * } + *+ * @param html The HTML where the JSON we're looking for is stored inside a + * variable inside some JavaScript block + * @param variable Name of the variable + * @return The JsonObject stored in the variable with this name + */ + public static JsonObject getJsonData(final String html, final String variable) + throws JsonParserException, ArrayIndexOutOfBoundsException { + final Document document = Jsoup.parse(html); + final String json = document.getElementsByAttribute(variable).attr(variable); + return JsonParser.object().from(json); + } }