Skip to content

Commit

Permalink
[Bandcamp] Add Java 8 streams
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Mar 26, 2022
1 parent 349990f commit 24e8399
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package org.schabi.newpipe.extractor.services.bandcamp.extractors;

import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;

Expand All @@ -19,6 +21,8 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import java.io.IOException;
import java.util.Objects;
import java.util.stream.Stream;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -48,21 +52,18 @@ public String getBannerUrl() throws ParsingException {
*/
try {
final String html = getDownloader()
.get(channelInfo.getString("bandcamp_url")
.replace("http://", "https://"))
.get(replaceHttpWithHttps(channelInfo.getString("bandcamp_url")))
.responseBody();

return Jsoup.parse(html)
.getElementById("customHeader")
.getElementsByTag("img")
.first()
.attr("src");
return Stream.of(Jsoup.parse(html).getElementById("customHeader"))
.filter(Objects::nonNull)
.flatMap(element -> element.getElementsByTag("img").stream())
.map(element -> element.attr("src"))
.findFirst()
.orElse(""); // no banner available

} catch (final IOException | ReCaptchaException e) {
throw new ParsingException("Could not download artist web site", e);
} catch (final NullPointerException e) {
// No banner available
return "";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public String getUrl() throws ParsingException {

@Override
public String getThumbnailUrl() throws ParsingException {
final Element img = searchResult.getElementsByClass("art").first()
.getElementsByTag("img").first();
if (img != null) {
return img.attr("src");
} else {
return null;
}
return BandcampExtractorHelper.getThumbnailUrlFromSearchResult(searchResult);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import java.util.Objects;

public class BandcampCommentsInfoItemExtractor implements CommentsInfoItemExtractor {

private final Element writing;
Expand All @@ -16,7 +18,7 @@ public BandcampCommentsInfoItemExtractor(final Element writing, final String url

@Override
public String getName() throws ParsingException {
return writing.getElementsByClass("text").first().ownText();
return getCommentText();
}

@Override
Expand All @@ -30,13 +32,21 @@ public String getThumbnailUrl() throws ParsingException {
}

@Override
public String getCommentText() {
return writing.getElementsByClass("text").first().ownText();
public String getCommentText() throws ParsingException {
return writing.getElementsByClass("text").stream()
.filter(Objects::nonNull)
.map(Element::ownText)
.findFirst()
.orElseThrow(() -> new ParsingException("Could not get comment text"));
}

@Override
public String getUploaderName() throws ParsingException {
return writing.getElementsByClass("name").first().text();
return writing.getElementsByClass("name").stream()
.filter(Objects::nonNull)
.map(Element::text)
.findFirst()
.orElseThrow(() -> new ParsingException("Could not get uploader name"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.grack.nanojson.JsonWriter;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
Expand All @@ -19,6 +20,8 @@
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import javax.annotation.Nullable;

public final class BandcampExtractorHelper {

public static final String BASE_URL = "https://bandcamp.com";
Expand Down Expand Up @@ -134,4 +137,14 @@ public static DateWrapper parseDate(final String textDate) throws ParsingExcepti
throw new ParsingException("Could not parse date '" + textDate + "'", e);
}
}

@Nullable
public static String getThumbnailUrlFromSearchResult(final Element searchResult) {
return searchResult.getElementsByClass("art").stream()
.flatMap(element -> element.getElementsByTag("img").stream())
.map(element -> element.attr("src"))
.filter(string -> !string.isEmpty())
.findFirst()
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package org.schabi.newpipe.extractor.services.bandcamp.extractors;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson;
import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData;
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParserException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.schabi.newpipe.extractor.Page;
Expand All @@ -17,15 +24,9 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Objects;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImageUrl;
import static org.schabi.newpipe.extractor.utils.JsonUtils.getJsonData;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampStreamExtractor.getAlbumInfoJson;
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
import javax.annotation.Nonnull;

public class BandcampPlaylistExtractor extends PlaylistExtractor {

Expand Down Expand Up @@ -92,12 +93,10 @@ public String getUploaderName() {

@Override
public String getUploaderAvatarUrl() {
try {
return Objects.requireNonNull(document.getElementsByClass("band-photo").first())
.attr("src");
} catch (final NullPointerException e) {
return EMPTY_STRING;
}
return document.getElementsByClass("band-photo").stream()
.map(element -> element.attr("src"))
.findFirst()
.orElse(EMPTY_STRING);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ public String getUrl() {

@Override
public String getThumbnailUrl() {
final Element img = searchResult.getElementsByClass("art").first()
.getElementsByTag("img").first();
if (img != null) {
return img.attr("src");
} else {
return null;
}
return BandcampExtractorHelper.getThumbnailUrlFromSearchResult(searchResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.StreamingService;
Expand Down Expand Up @@ -77,9 +78,11 @@ public String getUrl() throws ParsingException {

@Nonnull
@Override
public String getUploaderName() {
return Jsoup.parse(showInfo.getString("image_caption"))
.getElementsByTag("a").first().text();
public String getUploaderName() throws ParsingException {
return Jsoup.parse(showInfo.getString("image_caption")).getElementsByTag("a").stream()
.map(Element::text)
.findFirst()
.orElseThrow(() -> new ParsingException("Could not get uploader name"));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,31 @@ public List<MetaInfo> getMetaInfo() throws ParsingException {

public InfoItemsPage<InfoItem> getPage(final Page page)
throws IOException, ExtractionException {
final String html = getDownloader().get(page.getUrl()).responseBody();

final MultiInfoItemsCollector collector = new MultiInfoItemsCollector(getServiceId());
final Document d = Jsoup.parse(getDownloader().get(page.getUrl()).responseBody());


final Document d = Jsoup.parse(html);

final Elements searchResultsElements = d.getElementsByClass("searchresult");

for (final Element searchResult : searchResultsElements) {

final String type = searchResult.getElementsByClass("result-info").first()
.getElementsByClass("itemtype").first().text();
for (final Element searchResult : d.getElementsByClass("searchresult")) {
final String type = searchResult.getElementsByClass("result-info").stream()
.flatMap(element -> element.getElementsByClass("itemtype").stream())
.map(Element::text)
.findFirst()
.orElse("");

switch (type) {
default:
continue;
case "FAN":
// don't display fan results
break;

case "ARTIST":
collector.commit(new BandcampChannelInfoItemExtractor(searchResult));
break;

case "ALBUM":
collector.commit(new BandcampPlaylistInfoItemExtractor(searchResult));
break;

case "TRACK":
collector.commit(new BandcampSearchStreamInfoItemExtractor(searchResult, null));
break;
}

}

// Count pages
Expand All @@ -92,7 +83,10 @@ public InfoItemsPage<InfoItem> getPage(final Page page)
return new InfoItemsPage<>(collector, null);
}

final Elements pages = pageLists.first().getElementsByTag("li");
final Elements pages = pageLists.stream()
.map(element -> element.getElementsByTag("li"))
.findFirst()
.orElseGet(Elements::new);

// Find current page
int currentPage = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ public String getThumbnailUrl() throws ParsingException {
@Nonnull
@Override
public String getUploaderAvatarUrl() {
try {
return document.getElementsByClass("band-photo").first().attr("src");
} catch (final NullPointerException e) {
return "";
}
return document.getElementsByClass("band-photo").stream()
.map(element -> element.attr("src"))
.findFirst()
.orElse("");
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
Expand All @@ -17,9 +16,9 @@

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class BandcampSuggestionExtractor extends SuggestionExtractor {

Expand All @@ -36,20 +35,12 @@ public List<String> suggestionList(final String query) throws IOException, Extra
final JsonObject fuzzyResults = JsonParser.object().from(downloader
.get(AUTOCOMPLETE_URL + URLEncoder.encode(query, "UTF-8")).responseBody());

final JsonArray jsonArray = fuzzyResults.getObject("auto")
.getArray("results");

final List<String> suggestions = new ArrayList<>();

for (final Object fuzzyResult : jsonArray) {
final String res = ((JsonObject) fuzzyResult).getString("name");

if (!suggestions.contains(res)) {
suggestions.add(res);
}
}

return suggestions;
return fuzzyResults.getObject("auto").getArray("results").stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.map(jsonObject -> jsonObject.getString("name"))
.distinct()
.collect(Collectors.toList());
} catch (final JsonParserException e) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -46,13 +47,7 @@ public String getUrl() throws ParsingException {

@Override
public String getThumbnailUrl() throws ParsingException {
final Element img = searchResult.getElementsByClass("art").first()
.getElementsByTag("img").first();
if (img != null) {
return img.attr("src");
} else {
return null;
}
return BandcampExtractorHelper.getThumbnailUrlFromSearchResult(searchResult);
}

@Override
Expand Down

0 comments on commit 24e8399

Please sign in to comment.