From aa6c17dc77dda965506df360c646d6124ead1328 Mon Sep 17 00:00:00 2001 From: TobiGr Date: Thu, 3 Aug 2023 14:37:08 +0200 Subject: [PATCH] [SoundCloud] Deduplicate some code --- .../soundcloud/SoundcloudParsingHelper.java | 37 ++++++++----------- .../SoundcloudCommentsExtractor.java | 28 +++++--------- 2 files changed, 24 insertions(+), 41 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java index a0bd239df..b926da6a8 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudParsingHelper.java @@ -1,14 +1,9 @@ package org.schabi.newpipe.extractor.services.soundcloud; -import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; -import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; - import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; - import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; @@ -28,6 +23,7 @@ import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Parser.RegexException; import org.schabi.newpipe.extractor.utils.Utils; +import javax.annotation.Nonnull; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; @@ -38,7 +34,9 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import javax.annotation.Nonnull; +import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; +import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; public final class SoundcloudParsingHelper { private static String clientId; @@ -200,6 +198,7 @@ public final class SoundcloudParsingHelper { * * @return the next streams url, empty if don't have */ + @Nonnull public static String getUsersFromApi(final ChannelInfoItemsCollector collector, final String apiUrl) throws IOException, ReCaptchaException, ParsingException { @@ -221,17 +220,7 @@ public final class SoundcloudParsingHelper { } } - String nextPageUrl; - try { - nextPageUrl = responseObject.getString("next_href"); - if (!nextPageUrl.contains("client_id=")) { - nextPageUrl += "&client_id=" + SoundcloudParsingHelper.clientId(); - } - } catch (final Exception ignored) { - nextPageUrl = ""; - } - - return nextPageUrl; + return getNextPageUrl(responseObject); } /** @@ -261,6 +250,7 @@ public final class SoundcloudParsingHelper { * * @return the next streams url, empty if don't have */ + @Nonnull public static String getStreamsFromApi(final StreamInfoItemsCollector collector, final String apiUrl, final boolean charts) throws IOException, @@ -288,17 +278,20 @@ public final class SoundcloudParsingHelper { } } - String nextPageUrl; + return getNextPageUrl(responseObject); + } + + @Nonnull + private static String getNextPageUrl(@Nonnull final JsonObject response) { try { - nextPageUrl = responseObject.getString("next_href"); + String nextPageUrl = response.getString("next_href"); if (!nextPageUrl.contains("client_id=")) { nextPageUrl += "&client_id=" + SoundcloudParsingHelper.clientId(); } + return nextPageUrl; } catch (final Exception ignored) { - nextPageUrl = ""; + return ""; } - - return nextPageUrl; } public static String getStreamsFromApi(final StreamInfoItemsCollector collector, diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java index b02a3ea80..7dba601bc 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsExtractor.java @@ -15,6 +15,7 @@ import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.downloader.Response; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import java.io.IOException; @@ -33,22 +34,7 @@ public class SoundcloudCommentsExtractor extends CommentsExtractor { @Override public InfoItemsPage getInitialPage() throws ExtractionException, IOException { - final Downloader downloader = NewPipe.getDownloader(); - final Response response = downloader.get(getUrl()); - - final JsonObject json; - try { - json = JsonParser.object().from(response.responseBody()); - } catch (final JsonParserException e) { - throw new ParsingException("Could not parse json", e); - } - - final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector( - getServiceId()); - - collectStreamsFrom(collector, json.getArray("collection")); - - return new InfoItemsPage<>(collector, new Page(json.getString("next_href"))); + return getPage(getUrl()); } @Override @@ -57,9 +43,14 @@ public class SoundcloudCommentsExtractor extends CommentsExtractor { if (page == null || isNullOrEmpty(page.getUrl())) { throw new IllegalArgumentException("Page doesn't contain an URL"); } + return getPage(page.getUrl()); + } + @Nonnull + private InfoItemsPage getPage(@Nonnull final String url) + throws ParsingException, IOException, ReCaptchaException { final Downloader downloader = NewPipe.getDownloader(); - final Response response = downloader.get(page.getUrl()); + final Response response = downloader.get(url); final JsonObject json; try { @@ -72,8 +63,7 @@ public class SoundcloudCommentsExtractor extends CommentsExtractor { getServiceId()); collectStreamsFrom(collector, json.getArray("collection")); - - return new InfoItemsPage<>(collector, new Page(json.getString("next_href"))); + return new InfoItemsPage<>(collector, new Page(json.getString("next_href", null))); } @Override