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;
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
import com.grack.nanojson.JsonObject;
|
|
|
|
import com.grack.nanojson.JsonParser;
|
|
|
|
import com.grack.nanojson.JsonParserException;
|
2019-12-22 00:42:26 +01:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2019-12-21 19:00:07 +01:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
2019-12-22 00:42:26 +01:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
2019-12-21 19:00:07 +01:00
|
|
|
|
2019-12-22 00:42:26 +01:00
|
|
|
import java.io.IOException;
|
2019-12-21 19:00:07 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
public class BandcampExtractorHelper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get JSON behind <code>var $variable = </code> out of web page
|
|
|
|
* <br/<br/>
|
|
|
|
* 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
|
|
|
|
*/
|
2020-03-19 11:18:29 +01:00
|
|
|
public static JsonObject getJSONFromJavaScriptVariables(String html, String variable) throws JsonParserException, ArrayIndexOutOfBoundsException, ParsingException {
|
2019-12-21 19:00:07 +01:00
|
|
|
|
|
|
|
String[] part = html.split("var " + variable + " = ");
|
|
|
|
|
|
|
|
String firstHalfGone = part[1];
|
|
|
|
|
|
|
|
firstHalfGone = firstHalfGone.replaceAll("\" \\+ \"", "");
|
|
|
|
|
|
|
|
int position = -1;
|
|
|
|
int level = 0;
|
|
|
|
for (char character : firstHalfGone.toCharArray()) {
|
|
|
|
position++;
|
|
|
|
|
|
|
|
switch (character) {
|
|
|
|
case '{':
|
|
|
|
level++;
|
|
|
|
continue;
|
|
|
|
case '}':
|
|
|
|
level--;
|
|
|
|
if (level == 0) {
|
2020-03-19 11:18:29 +01:00
|
|
|
return JsonParser.object().from(firstHalfGone.substring(0, position + 1)
|
2019-12-21 23:14:23 +01:00
|
|
|
.replaceAll(" {4}//.+", "") // Remove "for the curious" in JSON
|
|
|
|
.replaceAll("// xxx: note - don't internationalize this variable", "") // Remove this comment
|
2019-12-21 19:00:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ParsingException("Unexpected HTML: JSON never ends");
|
|
|
|
}
|
|
|
|
|
2019-12-22 00:42:26 +01:00
|
|
|
/**
|
|
|
|
* Translate all these parameters together to the URL of the corresponding album or track
|
|
|
|
* using the mobile api
|
|
|
|
*/
|
|
|
|
public static String getStreamUrlFromIds(long bandId, long itemId, String itemType) throws ParsingException {
|
|
|
|
|
|
|
|
try {
|
2020-03-19 11:18:29 +01:00
|
|
|
String jsonString = NewPipe.getDownloader().get(
|
2019-12-22 00:42:26 +01:00
|
|
|
"https://bandcamp.com/api/mobile/22/tralbum_details?band_id=" + bandId
|
|
|
|
+ "&tralbum_id=" + itemId + "&tralbum_type=" + itemType.substring(0, 1))
|
|
|
|
.responseBody();
|
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
return JsonParser.object().from(jsonString).getString("bandcamp_url").replace("http://", "https://");
|
2019-12-22 00:42:26 +01:00
|
|
|
|
2020-03-19 11:18:29 +01:00
|
|
|
} catch (JsonParserException | ReCaptchaException | IOException e) {
|
2019-12-22 00:42:26 +01:00
|
|
|
throw new ParsingException("Ids could not be translated to URL", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-21 19:00:07 +01:00
|
|
|
/**
|
|
|
|
* Concatenate all non-null and non-empty strings together while separating them using
|
|
|
|
* the comma parameter
|
|
|
|
*/
|
|
|
|
public static String smartConcatenate(String[] strings, String comma) {
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
|
|
|
|
// Remove empty strings
|
|
|
|
ArrayList<String> list = new ArrayList<>(Arrays.asList(strings));
|
|
|
|
for (int i = list.size() - 1; i >= 0; i--) {
|
2019-12-22 01:38:48 +01:00
|
|
|
if (list.get(i) == null || list.get(i).isEmpty() || list.get(i).equals("null")) {
|
2019-12-21 19:00:07 +01:00
|
|
|
list.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append remaining strings to result
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
String string = list.get(i);
|
|
|
|
result.append(string);
|
|
|
|
|
|
|
|
if (i != list.size() - 1) {
|
|
|
|
// This is not the last iteration yet
|
|
|
|
result.append(comma);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return String.valueOf(result);
|
|
|
|
}
|
|
|
|
}
|