Move BandcampExtractorHelper.getJsonData(String, String) to JsonUtils

This commit is contained in:
TobiGr 2021-02-21 13:51:12 +01:00
parent 70814dcfef
commit a1688fe953
5 changed files with 39 additions and 20 deletions

View File

@ -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";
/**
* <p>Get an attribute of a web page as JSON
*
* <p>Originally a part of bandcampDirect.</p>
*
* @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

View File

@ -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 {

View File

@ -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) {

View File

@ -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"));

View File

@ -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;
}
/**
* <p>Get an attribute of a web page as JSON
*
* <p>Originally a part of bandcampDirect.</p>
* <p>Example HTML:</p>
* <pre>
* {@code
* <p data-town="{&quot;name&quot;:&quot;Mycenae&quot;,&quot;country&quot;:&quot;Greece&quot;}">This is Sparta!</p>
* }
* </pre>
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject for</p>
* <pre>
* {@code
* {
* "name": "Mycenae",
* "country": "Greece"
* }
* }
* </pre>
* @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);
}
}