Merge pull request #579 from fynngodau/bandcamp-fix-linkhandler

[Bandcamp] Fix link handler acceptance behaviour
This commit is contained in:
Tobi 2021-03-14 12:56:10 +01:00 committed by GitHub
commit def745b801
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 13 deletions

View file

@ -7,20 +7,15 @@ import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException; import com.grack.nanojson.JsonParserException;
import com.grack.nanojson.JsonWriter; import com.grack.nanojson.JsonWriter;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.NewPipe;
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.localization.DateWrapper; import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale; import java.util.Locale;
public class BandcampExtractorHelper { public class BandcampExtractorHelper {
@ -95,12 +90,18 @@ public class BandcampExtractorHelper {
if (url.toLowerCase().matches("https?://.+\\.bandcamp\\.com(/.*)?")) return true; if (url.toLowerCase().matches("https?://.+\\.bandcamp\\.com(/.*)?")) return true;
try { try {
// Accept all other URLs if they contain a <meta> tag that says they are generated by bandcamp // Test other URLs for whether they contain a footer that links to bandcamp
return Jsoup.parse( return Jsoup.parse(
NewPipe.getDownloader().get(url).responseBody() NewPipe.getDownloader().get(url).responseBody()
) )
.getElementsByAttributeValue("name", "generator") .getElementById("pgFt")
.attr("content").equals("Bandcamp"); .getElementById("pgFt-inner")
.getElementById("footer-logo-wrapper")
.getElementById("footer-logo")
.getElementsByClass("hiddenAccess")
.text().equals("Bandcamp");
} catch (NullPointerException e) {
return false;
} catch (IOException | ReCaptchaException e) { } catch (IOException | ReCaptchaException e) {
throw new ParsingException("Could not determine whether URL is custom domain " + throw new ParsingException("Could not determine whether URL is custom domain " +
"(not available? network error?)"); "(not available? network error?)");

View file

@ -55,7 +55,9 @@ public class BandcampChannelLinkHandlerFactory extends ListLinkHandlerFactory {
* Accepts only pages that lead to the root of an artist profile. Supports external pages. * Accepts only pages that lead to the root of an artist profile. Supports external pages.
*/ */
@Override @Override
public boolean onAcceptUrl(final String url) throws ParsingException { public boolean onAcceptUrl(String url) throws ParsingException {
url = url.toLowerCase();
// https: | | artist.bandcamp.com | releases // https: | | artist.bandcamp.com | releases
// 0 1 2 3 // 0 1 2 3
@ -64,8 +66,10 @@ public class BandcampChannelLinkHandlerFactory extends ListLinkHandlerFactory {
// URL is too short // URL is too short
if (splitUrl.length < 3) return false; if (splitUrl.length < 3) return false;
// Must have "releases" as segment after url or none at all // Must have "releases" or "music" as segment after url or none at all
if (splitUrl.length > 3 && !splitUrl[3].equals("releases")) { if (splitUrl.length > 3 && !(
splitUrl[3].equals("releases") || splitUrl[3].equals("music")
)) {
return false; return false;

View file

@ -29,17 +29,24 @@ public class BandcampChannelLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com")); assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com"));
assertTrue(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/")); assertTrue(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/"));
assertTrue(linkHandler.acceptUrl("https://billwurtz.bandcamp.com/releases")); assertTrue(linkHandler.acceptUrl("https://billwurtz.bandcamp.com/releases"));
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases"));
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases/"));
assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com/")); assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com/"));
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://daily.bandcamp.com/")); assertFalse(linkHandler.acceptUrl("https://daily.bandcamp.com/"));
assertFalse(linkHandler.acceptUrl("https://DAILY.BANDCAMP.COM"));
assertFalse(linkHandler.acceptUrl("https://daily.bandcamp.com/best-of-2020/bandcamp-daily-staffers-on-their-favorite-albums-of-2020")); assertFalse(linkHandler.acceptUrl("https://daily.bandcamp.com/best-of-2020/bandcamp-daily-staffers-on-their-favorite-albums-of-2020"));
// External URLs // External URLs
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases")); assertTrue(linkHandler.acceptUrl("https://lobstertheremin.com"));
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases/")); assertTrue(linkHandler.acceptUrl("https://lobstertheremin.com/music"));
assertTrue(linkHandler.acceptUrl("https://lobstertheremin.com/music/"));
assertTrue(linkHandler.acceptUrl("https://diskak.usopop.com/"));
assertTrue(linkHandler.acceptUrl("https://diskak.usopop.com/releases"));
assertTrue(linkHandler.acceptUrl("https://diskak.usopop.com/RELEASES"));
assertFalse(linkHandler.acceptUrl("https://example.com/releases")); assertFalse(linkHandler.acceptUrl("https://example.com/releases"));
} }
@ -51,6 +58,10 @@ public class BandcampChannelLinkHandlerFactoryTest {
assertEquals("1581461772", linkHandler.getId("https://interovgm.bandcamp.com/releases")); assertEquals("1581461772", linkHandler.getId("https://interovgm.bandcamp.com/releases"));
assertEquals("3321800855", linkHandler.getId("https://infiniteammo.bandcamp.com/")); assertEquals("3321800855", linkHandler.getId("https://infiniteammo.bandcamp.com/"));
assertEquals("3775652329", linkHandler.getId("https://npet.bandcamp.com/")); assertEquals("3775652329", linkHandler.getId("https://npet.bandcamp.com/"));
assertEquals("2735462545", linkHandler.getId("http://lobstertheremin.com/"));
assertEquals("2735462545", linkHandler.getId("https://lobstertheremin.com/music/"));
assertEquals("3826445168", linkHandler.getId("https://diskak.usopop.com/releases"));
} }
@Test @Test
@ -58,6 +69,8 @@ public class BandcampChannelLinkHandlerFactoryTest {
assertEquals("https://macbenson.bandcamp.com", linkHandler.getUrl("1196681540")); assertEquals("https://macbenson.bandcamp.com", linkHandler.getUrl("1196681540"));
assertEquals("https://interovgm.bandcamp.com", linkHandler.getUrl("1581461772")); assertEquals("https://interovgm.bandcamp.com", linkHandler.getUrl("1581461772"));
assertEquals("https://infiniteammo.bandcamp.com", linkHandler.getUrl("3321800855")); assertEquals("https://infiniteammo.bandcamp.com", linkHandler.getUrl("3321800855"));
assertEquals("https://lobstertheremin.com", linkHandler.getUrl("2735462545"));
} }
@Test(expected = ParsingException.class) @Test(expected = ParsingException.class)

View file

@ -49,5 +49,6 @@ public class BandcampStreamLinkHandlerFactoryTest {
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/track/title")); assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/track/title"));
assertTrue(linkHandler.acceptUrl("http://bandcamP.com/?show=38")); assertTrue(linkHandler.acceptUrl("http://bandcamP.com/?show=38"));
assertTrue(linkHandler.acceptUrl("https://goodgoodblood-tl.bandcamp.com/track/when-it-all-wakes-up")); assertTrue(linkHandler.acceptUrl("https://goodgoodblood-tl.bandcamp.com/track/when-it-all-wakes-up"));
assertTrue(linkHandler.acceptUrl("https://lobstertheremin.com/track/unfinished"));
} }
} }