Bandcamp channel link handler factory

This commit is contained in:
Fynn Godau 2019-12-21 23:14:23 +01:00
parent c3d127ccd9
commit 5281456899
5 changed files with 55 additions and 21 deletions

View File

@ -41,8 +41,7 @@ public class BandcampService extends StreamingService {
@Override @Override
public ListLinkHandlerFactory getChannelLHFactory() { public ListLinkHandlerFactory getChannelLHFactory() {
//return new BandcampChannelLinkHandlerFactory(); TODO return new BandcampChannelLinkHandlerFactory();
return null;
} }
@Override @Override

View File

@ -42,7 +42,8 @@ public class BandcampExtractorHelper {
level--; level--;
if (level == 0) { if (level == 0) {
return new JSONObject(firstHalfGone.substring(0, position + 1) return new JSONObject(firstHalfGone.substring(0, position + 1)
.replaceAll(" {4}//.+", "") // Remove comments in JSON .replaceAll(" {4}//.+", "") // Remove "for the curious" in JSON
.replaceAll("// xxx: note - don't internationalize this variable", "") // Remove this comment
); );
} }
} }

View File

@ -56,7 +56,7 @@ public class BandcampStreamExtractor extends StreamExtractor {
try { try {
return BandcampExtractorHelper.getJSONFromJavaScriptVariables(html, "TralbumData"); return BandcampExtractorHelper.getJSONFromJavaScriptVariables(html, "TralbumData");
} catch (JSONException e) { } catch (JSONException e) {
throw new ParsingException("Faulty JSON", e); throw new ParsingException("Faulty JSON; page likely does not contain album data", e);
} }
} }
@ -211,17 +211,17 @@ public class BandcampStreamExtractor extends StreamExtractor {
} }
@Override @Override
public StreamType getStreamType() throws ParsingException { public StreamType getStreamType() {
return StreamType.AUDIO_STREAM; return StreamType.AUDIO_STREAM;
} }
@Override @Override
public StreamInfoItem getNextStream() throws IOException, ExtractionException { public StreamInfoItem getNextStream() {
return null; return null;
} }
@Override @Override
public StreamInfoItemsCollector getRelatedStreams() throws IOException, ExtractionException { public StreamInfoItemsCollector getRelatedStreams() {
return null; return null;
} }

View File

@ -2,13 +2,15 @@
package org.schabi.newpipe.extractor.services.bandcamp.linkHandler; package org.schabi.newpipe.extractor.services.bandcamp.linkHandler;
import org.json.JSONObject;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor; import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor;
import org.schabi.newpipe.extractor.utils.ExtractorHelper;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
@ -23,16 +25,41 @@ public class BandcampChannelLinkHandlerFactory extends ListLinkHandlerFactory {
public String getId(String url) throws ParsingException { public String getId(String url) throws ParsingException {
try { try {
String response = NewPipe.getDownloader().get(url).responseBody(); String response = NewPipe.getDownloader().get(url).responseBody();
return BandcampStreamExtractor.getAlbumInfoJson(response)
.getString("band_id"); // This variable contains band data!
JSONObject bandData = BandcampExtractorHelper.getJSONFromJavaScriptVariables(response, "BandData");
return String.valueOf(bandData.getLong("id"));
} catch (IOException | ReCaptchaException e) { } catch (IOException | ReCaptchaException e) {
throw new ParsingException("Download failed", e); throw new ParsingException("Download failed", e);
} }
} }
/**
* Fetch artist details from mobile endpoint, thereby receiving their URL.
* <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>
*
* @throws ParsingException
*/
@Override @Override
public String getUrl(String id, List<String> contentFilter, String sortFilter) throws ParsingException { public String getUrl(String id, List<String> contentFilter, String sortFilter) throws ParsingException {
return null; // TODO try {
String data = NewPipe.getDownloader().post(
"https://bandcamp.com/api/mobile/22/band_details",
null,
("{\"band_id\":\"" + id + "\"}").getBytes()
).responseBody();
return new JSONObject(data)
.getString("bandcamp_url")
.replace("http://", "https://");
} catch (IOException | ReCaptchaException e) {
throw new ParsingException("Download failed", e);
}
} }
/** /**
@ -40,17 +67,10 @@ public class BandcampChannelLinkHandlerFactory extends ListLinkHandlerFactory {
* where the profile is at <code>* . * /releases</code> * where the profile is at <code>* . * /releases</code>
*/ */
@Override @Override
public boolean onAcceptUrl(String url) throws ParsingException { public boolean onAcceptUrl(String url) {
// Ends with "bandcamp.com" or "bandcamp.com/"?
boolean endsWithBandcampCom = url.endsWith("bandcamp.com")
|| url.endsWith("bandcamp.com/");
// Is a subdomain of bandcamp.com? // Is a subdomain of bandcamp.com?
boolean isBandcampComSubdomain = url.matches("https?://.+\\.bandcamp\\.com"); boolean isBandcampComArtistPage = url.matches("https?://.+\\.bandcamp\\.com/?");
// Is root of bandcamp.com subdomain?
boolean isBandcampComArtistPage = endsWithBandcampCom && isBandcampComSubdomain;
boolean isCustomDomainReleases = url.matches("https?://.+\\..+/releases/?(?!.)"); boolean isCustomDomainReleases = url.matches("https?://.+\\..+/releases/?(?!.)");

View File

@ -30,11 +30,25 @@ public class BandcampChannelLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("http://interovgm.com/releases/")); assertTrue(linkHandler.acceptUrl("http://interovgm.com/releases/"));
assertTrue(linkHandler.acceptUrl("https://interovgm.com/releases")); assertTrue(linkHandler.acceptUrl("https://interovgm.com/releases"));
assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com")); assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com"));
assertTrue(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/"));
// Tests expecting false // Tests expecting false
assertFalse(linkHandler.acceptUrl("https://bandcamp.com")); assertFalse(linkHandler.acceptUrl("https://bandcamp.com"));
assertFalse(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/track/kitchen")); assertFalse(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/track/kitchen"));
assertFalse(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/")); }
@Test
public void testGetId() throws ParsingException {
assertEquals("1196681540", linkHandler.getId("https://macbenson.bandcamp.com/"));
assertEquals("1581461772", linkHandler.getId("https://interovgm.com/releases"));
assertEquals("3321800855", linkHandler.getId("https://infiniteammo.bandcamp.com/"));
}
@Test
public void testGetUrl() throws ParsingException {
assertEquals("https://macbenson.bandcamp.com", linkHandler.getUrl("1196681540"));
assertEquals("https://interovgm.com", linkHandler.getUrl("1581461772"));
assertEquals("https://infiniteammo.bandcamp.com", linkHandler.getUrl("3321800855"));
} }
} }