Skip to content

Commit a1688fe

Browse files
committed
Move BandcampExtractorHelper.getJsonData(String, String) to JsonUtils
1 parent 70814dc commit a1688fe

5 files changed

Lines changed: 39 additions & 20 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,6 @@ public class BandcampExtractorHelper {
2828
public static final String BASE_URL = "https://bandcamp.com";
2929
public static final String BASE_API_URL = BASE_URL + "/api";
3030

31-
/**
32-
* <p>Get an attribute of a web page as JSON
33-
*
34-
* <p>Originally a part of bandcampDirect.</p>
35-
*
36-
* @param html The HTML where the JSON we're looking for is stored inside a
37-
* variable inside some JavaScript block
38-
* @param variable Name of the variable
39-
* @return The JsonObject stored in the variable with this name
40-
*/
41-
public static JsonObject getJsonData(final String html, final String variable)
42-
throws JsonParserException, ArrayIndexOutOfBoundsException {
43-
final Document document = Jsoup.parse(html);
44-
final String json = document.getElementsByAttribute(variable).attr(variable);
45-
return JsonParser.object().from(json);
46-
}
47-
4831
/**
4932
* Translate all these parameters together to the URL of the corresponding album or track
5033
* using the mobile API

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampPlaylistExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.io.IOException;
2222

2323
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
24-
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getJsonData;
24+
import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData;
2525
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson;
2626

2727
public class BandcampPlaylistExtractor extends PlaylistExtractor {

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampStreamExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
1818
import org.schabi.newpipe.extractor.localization.DateWrapper;
1919
import org.schabi.newpipe.extractor.stream.*;
20+
import org.schabi.newpipe.extractor.utils.JsonUtils;
2021
import org.schabi.newpipe.extractor.utils.Utils;
2122

2223
import javax.annotation.Nonnull;
@@ -62,7 +63,7 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
6263
*/
6364
public static JsonObject getAlbumInfoJson(final String html) throws ParsingException {
6465
try {
65-
return BandcampExtractorHelper.getJsonData(html, "data-tralbum");
66+
return JsonUtils.getJsonData(html, "data-tralbum");
6667
} catch (final JsonParserException e) {
6768
throw new ParsingException("Faulty JSON; page likely does not contain album data", e);
6869
} catch (final ArrayIndexOutOfBoundsException e) {

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/linkHandler/BandcampChannelLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
1010
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
1111
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper;
12+
import org.schabi.newpipe.extractor.utils.JsonUtils;
1213

1314
import java.io.IOException;
1415
import java.util.List;
@@ -25,7 +26,7 @@ public String getId(final String url) throws ParsingException {
2526
final String response = NewPipe.getDownloader().get(url).responseBody();
2627

2728
// Use band data embedded in website to extract ID
28-
final JsonObject bandData = BandcampExtractorHelper.getJsonData(response, "data-band");
29+
final JsonObject bandData = JsonUtils.getJsonData(response, "data-band");
2930

3031
return String.valueOf(bandData.getLong("id"));
3132

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5+
import com.grack.nanojson.JsonParser;
6+
import com.grack.nanojson.JsonParserException;
7+
import org.jsoup.Jsoup;
8+
import org.jsoup.nodes.Document;
59
import org.schabi.newpipe.extractor.exceptions.ParsingException;
610

711
import javax.annotation.Nonnull;
@@ -99,4 +103,34 @@ private static JsonObject getObject(@Nonnull JsonObject object, @Nonnull List<St
99103
return result;
100104
}
101105

106+
/**
107+
* <p>Get an attribute of a web page as JSON
108+
*
109+
* <p>Originally a part of bandcampDirect.</p>
110+
* <p>Example HTML:</p>
111+
* <pre>
112+
* {@code
113+
* <p data-town="{&quot;name&quot;:&quot;Mycenae&quot;,&quot;country&quot;:&quot;Greece&quot;}">This is Sparta!</p>
114+
* }
115+
* </pre>
116+
* <p>Calling this function to get the attribute <code>data-town</code> returns the JsonObject for</p>
117+
* <pre>
118+
* {@code
119+
* {
120+
* "name": "Mycenae",
121+
* "country": "Greece"
122+
* }
123+
* }
124+
* </pre>
125+
* @param html The HTML where the JSON we're looking for is stored inside a
126+
* variable inside some JavaScript block
127+
* @param variable Name of the variable
128+
* @return The JsonObject stored in the variable with this name
129+
*/
130+
public static JsonObject getJsonData(final String html, final String variable)
131+
throws JsonParserException, ArrayIndexOutOfBoundsException {
132+
final Document document = Jsoup.parse(html);
133+
final String json = document.getElementsByAttribute(variable).attr(variable);
134+
return JsonParser.object().from(json);
135+
}
102136
}

0 commit comments

Comments
 (0)