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;
|
2020-04-26 23:05:56 +02:00
|
|
|
import com.grack.nanojson.JsonWriter;
|
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;
|
2020-06-04 19:36:58 +02:00
|
|
|
import org.schabi.newpipe.extractor.localization.DateWrapper;
|
2020-06-04 19:04:25 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils;
|
2019-12-21 19:00:07 +01:00
|
|
|
|
2019-12-22 00:42:26 +01:00
|
|
|
import java.io.IOException;
|
2020-06-04 19:36:58 +02:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
2019-12-21 19:00:07 +01:00
|
|
|
|
|
|
|
public class BandcampExtractorHelper {
|
|
|
|
|
|
|
|
/**
|
2020-04-20 23:03:12 +02:00
|
|
|
* <p>Get JSON behind <code>var $variable = </code> out of web page</p>
|
|
|
|
*
|
|
|
|
* <p>Originally a part of bandcampDirect.</p>
|
2019-12-21 19:00:07 +01:00
|
|
|
*
|
|
|
|
* @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--) {
|
2020-06-04 19:16:08 +02:00
|
|
|
if (Utils.isNullOrEmpty(list.get(i)) || 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);
|
|
|
|
}
|
2020-04-26 23:05:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch artist details from mobile endpoint.
|
|
|
|
* <a href=https://notabug.org/fynngodau/bandcampDirect/wiki/rewindBandcamp+%E2%80%93+Fetching+artist+details>
|
|
|
|
* I once took a moment to note down how it works.</a>
|
|
|
|
*/
|
|
|
|
public static JsonObject getArtistDetails(String id) throws ParsingException {
|
|
|
|
try {
|
|
|
|
return
|
|
|
|
JsonParser.object().from(
|
|
|
|
NewPipe.getDownloader().post(
|
|
|
|
"https://bandcamp.com/api/mobile/22/band_details",
|
|
|
|
null,
|
|
|
|
JsonWriter.string()
|
|
|
|
.object()
|
|
|
|
.value("band_id", id)
|
|
|
|
.end()
|
|
|
|
.done()
|
|
|
|
.getBytes()
|
|
|
|
).responseBody()
|
|
|
|
);
|
|
|
|
} catch (IOException | ReCaptchaException | JsonParserException e) {
|
|
|
|
throw new ParsingException("Could not download band details", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param id The image ID
|
|
|
|
* @param album Whether this is the cover of an album
|
|
|
|
* @return Url of image with this ID in size 10 which is 1200x1200 (we could also choose size 0
|
|
|
|
* but we don't want something as large as 3460x3460 here, do we?)
|
|
|
|
*/
|
2020-05-25 18:51:31 +02:00
|
|
|
public static String getImageUrl(long id, boolean album) {
|
2020-04-26 23:05:56 +02:00
|
|
|
return "https://f4.bcbits.com/img/" + (album ? 'a' : "") + id + "_10.jpg";
|
|
|
|
}
|
2020-06-04 19:36:58 +02:00
|
|
|
|
|
|
|
static DateWrapper parseDate(String textDate) throws ParsingException {
|
|
|
|
try {
|
|
|
|
Date date = new SimpleDateFormat("dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH).parse(textDate);
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.setTime(date);
|
|
|
|
return new DateWrapper(calendar, false);
|
|
|
|
} catch (ParseException e) {
|
|
|
|
throw new ParsingException("Could not extract date", e);
|
|
|
|
}
|
|
|
|
}
|
2019-12-21 19:00:07 +01:00
|
|
|
}
|