Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bandcamp] Fix link handler acceptance behaviour #579

Merged
merged 2 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
import com.grack.nanojson.JsonParserException;
import com.grack.nanojson.JsonWriter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.utils.Utils;

import java.io.IOException;
import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class BandcampExtractorHelper {
Expand Down Expand Up @@ -95,12 +90,18 @@ public static boolean isSupportedDomain(final String url) throws ParsingExceptio
if (url.toLowerCase().matches("https?://.+\\.bandcamp\\.com(/.*)?")) return true;

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(
NewPipe.getDownloader().get(url).responseBody()
)
.getElementsByAttributeValue("name", "generator")
.attr("content").equals("Bandcamp");
.getElementById("pgFt")
.getElementById("pgFt-inner")
.getElementById("footer-logo-wrapper")
.getElementById("footer-logo")
.getElementsByClass("hiddenAccess")
.text().equals("Bandcamp");
} catch (NullPointerException e) {
return false;
} catch (IOException | ReCaptchaException e) {
throw new ParsingException("Could not determine whether URL is custom domain " +
"(not available? network error?)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public String getUrl(final String id, final List<String> contentFilter, final St
* Accepts only pages that lead to the root of an artist profile. Supports external pages.
*/
@Override
public boolean onAcceptUrl(final String url) throws ParsingException {
public boolean onAcceptUrl(String url) throws ParsingException {

url = url.toLowerCase();

// https: | | artist.bandcamp.com | releases
// 0 1 2 3
Expand All @@ -64,8 +66,10 @@ public boolean onAcceptUrl(final String url) throws ParsingException {
// URL is too short
if (splitUrl.length < 3) return false;

// Must have "releases" as segment after url or none at all
if (splitUrl.length > 3 && !splitUrl[3].equals("releases")) {
// Must have "releases" or "music" as segment after url or none at all
if (splitUrl.length > 3 && !(
splitUrl[3].equals("releases") || splitUrl[3].equals("music")
)) {

return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ public void testAcceptUrl() throws ParsingException {
assertTrue(linkHandler.acceptUrl("http://zachbenson.bandcamp.com"));
assertTrue(linkHandler.acceptUrl("https://zachbenson.bandcamp.com/"));
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/"));

assertFalse(linkHandler.acceptUrl("https://bandcamp.com"));
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/best-of-2020/bandcamp-daily-staffers-on-their-favorite-albums-of-2020"));

// External URLs
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases"));
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/releases/"));
assertTrue(linkHandler.acceptUrl("https://lobstertheremin.com"));
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"));
}
Expand All @@ -51,13 +58,19 @@ public void testGetId() throws ParsingException {
assertEquals("1581461772", linkHandler.getId("https://interovgm.bandcamp.com/releases"));
assertEquals("3321800855", linkHandler.getId("https://infiniteammo.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
public void testGetUrl() throws ParsingException {
assertEquals("https://macbenson.bandcamp.com", linkHandler.getUrl("1196681540"));
assertEquals("https://interovgm.bandcamp.com", linkHandler.getUrl("1581461772"));
assertEquals("https://infiniteammo.bandcamp.com", linkHandler.getUrl("3321800855"));

assertEquals("https://lobstertheremin.com", linkHandler.getUrl("2735462545"));
}

@Test(expected = ParsingException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public void testAcceptUrl() throws ParsingException {
assertTrue(linkHandler.acceptUrl("https://interovgm.bandcamp.com/track/title"));
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://lobstertheremin.com/track/unfinished"));
}
}