diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java index cfc3f5fc..e53dbbef 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java @@ -2,14 +2,12 @@ package org.schabi.newpipe.extractor; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; -import org.schabi.newpipe.extractor.utils.Utils; -import javax.annotation.Nonnull; import java.io.IOException; import java.util.Collections; import java.util.List; -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; +import javax.annotation.Nonnull; /** @@ -110,8 +108,7 @@ public abstract class ListExtractor extends Extractor { } public boolean hasNextPage() { - return nextPage != null && (!isNullOrEmpty(nextPage.getUrl()) - || !isNullOrEmpty(nextPage.getIds())); + return Page.isValid(nextPage); } public List getItems() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java index 84010205..c06c24c4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java @@ -4,8 +4,6 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import java.util.List; -import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; - public abstract class ListInfo extends Info { private List relatedItems; private Page nextPage = null; @@ -39,8 +37,7 @@ public abstract class ListInfo extends Info { } public boolean hasNextPage() { - return nextPage != null && (!isNullOrEmpty(nextPage.getUrl()) - || !isNullOrEmpty(nextPage.getIds())); + return Page.isValid(nextPage); } public Page getNextPage() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java b/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java index 445fa3f0..6b8b4247 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/Page.java @@ -4,6 +4,8 @@ import java.io.Serializable; import java.util.List; import java.util.Map; +import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; + public class Page implements Serializable { private final String url; private final List ids; @@ -42,4 +44,9 @@ public class Page implements Serializable { public Map getCookies() { return cookies; } + + public static boolean isValid(final Page page) { + return page != null && (!isNullOrEmpty(page.getUrl()) + || !isNullOrEmpty(page.getIds())); + } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java index fa47a828..5c6ceac4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java @@ -1,10 +1,14 @@ package org.schabi.newpipe.extractor.services.peertube; +import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; +import org.schabi.newpipe.extractor.InfoItemsCollector; import org.schabi.newpipe.extractor.Page; import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor; +import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Parser; import org.schabi.newpipe.extractor.utils.Utils; @@ -66,4 +70,21 @@ public class PeertubeParsingHelper { return new Page(prevPageUrl.replace(START_KEY + "=" + prevStart, START_KEY + "=" + nextStart)); } } + + public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl) throws ParsingException { + final JsonArray contents; + try { + contents = (JsonArray) JsonUtils.getValue(json, "data"); + } catch (Exception e) { + throw new ParsingException("Unable to extract list info", e); + } + + for (final Object c : contents) { + if (c instanceof JsonObject) { + final JsonObject item = (JsonObject) c; + final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); + collector.commit(extractor); + } + } + } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeAccountExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeAccountExtractor.java index 6f987877..49978e88 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeAccountExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeAccountExtractor.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.services.peertube.extractors; -import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; @@ -24,6 +23,7 @@ import java.io.IOException; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; +import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class PeertubeAccountExtractor extends ChannelExtractor { @@ -57,9 +57,8 @@ public class PeertubeAccountExtractor extends ChannelExtractor { } @Override - public long getSubscriberCount() throws ParsingException { - final Number number = JsonUtils.getNumber(json, "followersCount"); - return number.longValue(); + public long getSubscriberCount() { + return json.getLong("followersCount"); } @Override @@ -92,23 +91,6 @@ public class PeertubeAccountExtractor extends ChannelExtractor { return getPage(new Page(pageUrl)); } - private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException { - final JsonArray contents; - try { - contents = (JsonArray) JsonUtils.getValue(json, "data"); - } catch (Exception e) { - throw new ParsingException("Unable to extract account streams", e); - } - - for (final Object c : contents) { - if (c instanceof JsonObject) { - final JsonObject item = (JsonObject) c; - final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); - collector.commit(extractor); - } - } - } - @Override public InfoItemsPage getPage(final Page page) throws IOException, ExtractionException { if (page == null || isNullOrEmpty(page.getUrl())) { @@ -128,10 +110,10 @@ public class PeertubeAccountExtractor extends ChannelExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); - collectStreamsFrom(collector, json); + collectStreamsFrom(collector, json, getBaseUrl()); return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeChannelExtractor.java index 4bc3d9af..e6cc66b6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeChannelExtractor.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.services.peertube.extractors; -import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; @@ -24,6 +23,7 @@ import java.io.IOException; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; +import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; @@ -58,9 +58,8 @@ public class PeertubeChannelExtractor extends ChannelExtractor { } @Override - public long getSubscriberCount() throws ParsingException { - final Number number = JsonUtils.getNumber(json, "followersCount"); - return number.longValue(); + public long getSubscriberCount() { + return json.getLong("followersCount"); } @Override @@ -99,23 +98,6 @@ public class PeertubeChannelExtractor extends ChannelExtractor { return getPage(new Page(pageUrl)); } - private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException { - final JsonArray contents; - try { - contents = (JsonArray) JsonUtils.getValue(json, "data"); - } catch (Exception e) { - throw new ParsingException("Unable to extract channel streams", e); - } - - for (final Object c : contents) { - if (c instanceof JsonObject) { - final JsonObject item = (JsonObject) c; - final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); - collector.commit(extractor); - } - } - } - @Override public InfoItemsPage getPage(final Page page) throws IOException, ExtractionException { if (page == null || isNullOrEmpty(page.getUrl())) { @@ -135,10 +117,10 @@ public class PeertubeChannelExtractor extends ChannelExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); - collectStreamsFrom(collector, json); + collectStreamsFrom(collector, json, getBaseUrl()); return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java index 9e628ee3..7c51e740 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java @@ -69,7 +69,7 @@ public class PeertubeCommentsExtractor extends CommentsExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId()); collectCommentsFrom(collector, json); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java index 7c35df78..446a4e8e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java @@ -11,6 +11,8 @@ import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.utils.JsonUtils; +import java.util.Objects; + public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor { private final JsonObject item; private final String url; @@ -71,9 +73,8 @@ public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtrac } @Override - public String getCommentId() throws ParsingException { - final Number value = JsonUtils.getNumber(item, "id"); - return value.toString(); + public String getCommentId() { + return Objects.toString(item.getLong("id"), null); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java index afcc881e..b2298536 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.services.peertube.extractors; -import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; import com.grack.nanojson.JsonParserException; @@ -16,7 +15,6 @@ import org.schabi.newpipe.extractor.playlist.PlaylistExtractor; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; -import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; @@ -26,6 +24,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; +import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class PeertubePlaylistExtractor extends PlaylistExtractor { @@ -62,7 +61,7 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor { @Override public long getStreamCount() { - return playlistInfo.getNumber("videosLength").longValue(); + return playlistInfo.getLong("videosLength"); } @Nonnull @@ -89,24 +88,6 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor { return getPage(new Page(getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE)); } - private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException { - final JsonArray contents; - try { - contents = (JsonArray) JsonUtils.getValue(json, "data"); - } catch (Exception e) { - throw new ParsingException("Unable to extract playlist streams", e); - } - - final String baseUrl = getBaseUrl(); - for (final Object c : contents) { - if (c instanceof JsonObject) { - final JsonObject item = (JsonObject) c; - final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); - collector.commit(extractor); - } - } - } - @Override public InfoItemsPage getPage(final Page page) throws IOException, ExtractionException { if (page == null || isNullOrEmpty(page.getUrl())) { @@ -126,10 +107,10 @@ public class PeertubePlaylistExtractor extends PlaylistExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); - collectStreamsFrom(collector, json); + collectStreamsFrom(collector, json, getBaseUrl()); return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java index 90ba6894..72fcbed2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.services.peertube.extractors; -import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; @@ -15,7 +14,6 @@ import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler; import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; import org.schabi.newpipe.extractor.search.SearchExtractor; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; -import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; @@ -25,6 +23,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; +import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class PeertubeSearchExtractor extends SearchExtractor { @@ -49,24 +48,6 @@ public class PeertubeSearchExtractor extends SearchExtractor { return getPage(new Page(pageUrl)); } - private void collectStreamsFrom(final InfoItemsSearchCollector collector, final JsonObject json) throws ParsingException { - final JsonArray contents; - try { - contents = (JsonArray) JsonUtils.getValue(json, "data"); - } catch (Exception e) { - throw new ParsingException("unable to extract search info", e); - } - - final String baseUrl = getBaseUrl(); - for (final Object c : contents) { - if (c instanceof JsonObject) { - final JsonObject item = (JsonObject) c; - final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); - collector.commit(extractor); - } - } - } - @Override public InfoItemsPage getPage(final Page page) throws IOException, ExtractionException { if (page == null || isNullOrEmpty(page.getUrl())) { @@ -86,10 +67,10 @@ public class PeertubeSearchExtractor extends SearchExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId()); - collectStreamsFrom(collector, json); + collectStreamsFrom(collector, json, getBaseUrl()); return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java index d001296c..e4a3ac92 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamExtractor.java @@ -103,9 +103,8 @@ public class PeertubeStreamExtractor extends StreamExtractor { } @Override - public long getLength() throws ParsingException { - final Number value = JsonUtils.getNumber(json, "duration"); - return value.longValue(); + public long getLength() { + return json.getLong("duration"); } @Override @@ -115,21 +114,18 @@ public class PeertubeStreamExtractor extends StreamExtractor { } @Override - public long getViewCount() throws ParsingException { - final Number value = JsonUtils.getNumber(json, "views"); - return value.longValue(); + public long getViewCount() { + return json.getLong("views"); } @Override - public long getLikeCount() throws ParsingException { - final Number value = JsonUtils.getNumber(json, "likes"); - return value.longValue(); + public long getLikeCount() { + return json.getLong("likes"); } @Override - public long getDislikeCount() throws ParsingException { - final Number value = JsonUtils.getNumber(json, "dislikes"); - return value.longValue(); + public long getDislikeCount() { + return json.getLong("dislikes"); } @Override @@ -222,7 +218,7 @@ public class PeertubeStreamExtractor extends StreamExtractor { @Override public List getVideoOnlyStreams() { - return null; + return Collections.emptyList(); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamInfoItemExtractor.java index ebe09d96..edb72c16 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeStreamInfoItemExtractor.java @@ -42,9 +42,8 @@ public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor } @Override - public long getViewCount() throws ParsingException { - final Number value = JsonUtils.getNumber(item, "views"); - return value.longValue(); + public long getViewCount() { + return item.getLong("views"); } @Override @@ -82,8 +81,7 @@ public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor } @Override - public long getDuration() throws ParsingException { - final Number value = JsonUtils.getNumber(item, "duration"); - return value.longValue(); + public long getDuration() { + return item.getLong("duration"); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeTrendingExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeTrendingExtractor.java index 5686bb20..fdc32aa1 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeTrendingExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeTrendingExtractor.java @@ -1,6 +1,5 @@ package org.schabi.newpipe.extractor.services.peertube.extractors; -import com.grack.nanojson.JsonArray; import com.grack.nanojson.JsonObject; import com.grack.nanojson.JsonParser; @@ -15,7 +14,6 @@ import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; -import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; @@ -25,6 +23,7 @@ import javax.annotation.Nonnull; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE; import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY; +import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom; import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty; public class PeertubeTrendingExtractor extends KioskExtractor { @@ -43,24 +42,6 @@ public class PeertubeTrendingExtractor extends KioskExtractor { return getPage(new Page(pageUrl)); } - private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException { - final JsonArray contents; - try { - contents = (JsonArray) JsonUtils.getValue(json, "data"); - } catch (Exception e) { - throw new ParsingException("Unable to extract kiosk info", e); - } - - final String baseUrl = getBaseUrl(); - for (final Object c : contents) { - if (c instanceof JsonObject) { - final JsonObject item = (JsonObject) c; - final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl); - collector.commit(extractor); - } - } - } - @Override public InfoItemsPage getPage(final Page page) throws IOException, ExtractionException { if (page == null || isNullOrEmpty(page.getUrl())) { @@ -80,10 +61,10 @@ public class PeertubeTrendingExtractor extends KioskExtractor { if (json != null) { PeertubeParsingHelper.validate(json); - final long total = JsonUtils.getNumber(json, "total").longValue(); + final long total = json.getLong("total"); final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); - collectStreamsFrom(collector, json); + collectStreamsFrom(collector, json, getBaseUrl()); return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total)); } else { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java index 1b56ace2..2ec6f508 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelExtractor.java @@ -75,7 +75,7 @@ public class SoundcloudChannelExtractor extends ChannelExtractor { @Override public long getSubscriberCount() { - return user.getNumber("followers_count", 0).longValue(); + return user.getLong("followers_count", 0); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelInfoItemExtractor.java index 27444858..7fcc28a6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChannelInfoItemExtractor.java @@ -32,12 +32,12 @@ public class SoundcloudChannelInfoItemExtractor implements ChannelInfoItemExtrac @Override public long getSubscriberCount() { - return itemObject.getNumber("followers_count", 0).longValue(); + return itemObject.getLong("followers_count"); } @Override public long getStreamCount() { - return itemObject.getNumber("track_count", 0).longValue(); + return itemObject.getLong("track_count"); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java index 4076accb..e58dc991 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudChartsExtractor.java @@ -40,8 +40,8 @@ public class SoundcloudChartsExtractor extends KioskExtractor { throw new IllegalArgumentException("Page doesn't contain an URL"); } - StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); - String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, page.getUrl(), true); + final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); + final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, page.getUrl(), true); return new InfoItemsPage<>(collector, new Page(nextPageUrl)); } @@ -49,7 +49,7 @@ public class SoundcloudChartsExtractor extends KioskExtractor { @Nonnull @Override public InfoItemsPage getInitialPage() throws IOException, ExtractionException { - StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); + final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId()); String apiUrl = "https://api-v2.soundcloud.com/charts" + "?genre=soundcloud:genres:all-music" + @@ -61,10 +61,10 @@ public class SoundcloudChartsExtractor extends KioskExtractor { apiUrl += "&kind=trending"; } - String contentCountry = SoundCloud.getContentCountry().getCountryCode(); + final String contentCountry = SoundCloud.getContentCountry().getCountryCode(); apiUrl += "®ion=soundcloud:regions:" + contentCountry; - String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true); + final String nextPageUrl = SoundcloudParsingHelper.getStreamsFromApi(collector, apiUrl, true); return new InfoItemsPage<>(collector, new Page(nextPageUrl)); } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java index 8a478dff..7969904e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudCommentsInfoItemExtractor.java @@ -6,10 +6,11 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper; +import java.util.Objects; + import javax.annotation.Nullable; public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor { - private JsonObject json; private String url; @@ -19,32 +20,32 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr } @Override - public String getCommentId() throws ParsingException { - return json.getNumber("id").toString(); + public String getCommentId() { + return Objects.toString(json.getLong("id"), null); } @Override - public String getCommentText() throws ParsingException { + public String getCommentText() { return json.getString("body"); } @Override - public String getUploaderName() throws ParsingException { + public String getUploaderName() { return json.getObject("user").getString("username"); } @Override - public String getUploaderAvatarUrl() throws ParsingException { + public String getUploaderAvatarUrl() { return json.getObject("user").getString("avatar_url"); } @Override - public String getUploaderUrl() throws ParsingException { + public String getUploaderUrl() { return json.getObject("user").getString("permalink_url"); } @Override - public String getTextualUploadDate() throws ParsingException { + public String getTextualUploadDate() { return json.getString("created_at"); } @@ -55,7 +56,7 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr } @Override - public int getLikeCount() throws ParsingException { + public int getLikeCount() { return -1; } @@ -70,7 +71,7 @@ public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtr } @Override - public String getThumbnailUrl() throws ParsingException { + public String getThumbnailUrl() { return json.getObject("user").getString("avatar_url"); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java index b8518a7e..878a7766 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistExtractor.java @@ -113,7 +113,7 @@ public class SoundcloudPlaylistExtractor extends PlaylistExtractor { @Override public long getStreamCount() { - return playlist.getNumber("track_count", 0).longValue(); + return playlist.getLong("track_count"); } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistInfoItemExtractor.java index f29efb1c..7c463125 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudPlaylistInfoItemExtractor.java @@ -81,6 +81,6 @@ public class SoundcloudPlaylistInfoItemExtractor implements PlaylistInfoItemExtr @Override public long getStreamCount() { - return itemObject.getNumber("track_count", 0).longValue(); + return itemObject.getLong("track_count"); } } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java index 431baff9..a8cc7e13 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamExtractor.java @@ -102,7 +102,7 @@ public class SoundcloudStreamExtractor extends StreamExtractor { @Override public long getLength() { - return track.getNumber("duration", 0).longValue() / 1000L; + return track.getLong("duration") / 1000L; } @Override @@ -112,12 +112,12 @@ public class SoundcloudStreamExtractor extends StreamExtractor { @Override public long getViewCount() { - return track.getNumber("playback_count", 0).longValue(); + return track.getLong("playback_count"); } @Override public long getLikeCount() { - return track.getNumber("favoritings_count", -1).longValue(); + return track.getLong("favoritings_count", -1); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamInfoItemExtractor.java index 3aef17ff..7da3ab5d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/extractors/SoundcloudStreamInfoItemExtractor.java @@ -30,7 +30,7 @@ public class SoundcloudStreamInfoItemExtractor implements StreamInfoItemExtracto @Override public long getDuration() { - return itemObject.getNumber("duration", 0).longValue() / 1000L; + return itemObject.getLong("duration") / 1000L; } @Override @@ -53,13 +53,9 @@ public class SoundcloudStreamInfoItemExtractor implements StreamInfoItemExtracto return new DateWrapper(SoundcloudParsingHelper.parseDateFrom(getTextualUploadDate())); } - private String getCreatedAt() { - return itemObject.getString("created_at"); - } - @Override public long getViewCount() { - return itemObject.getNumber("playback_count", 0).longValue(); + return itemObject.getLong("playback_count"); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java index 10413c7c..91302244 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java @@ -73,7 +73,7 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract @Override public int getLikeCount() throws ParsingException { try { - return JsonUtils.getNumber(json, "likeCount").intValue(); + return json.getInt("likeCount"); } catch (Exception e) { throw new ParsingException("Could not get like count", e); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java index 57886744..5b0dfdb3 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java @@ -1,18 +1,24 @@ package org.schabi.newpipe.extractor.utils; -import com.grack.nanojson.JsonParserException; import org.junit.Test; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import java.util.Arrays; + import static org.junit.Assert.assertEquals; public class UtilsTest { @Test - public void testMixedNumberWordToLong() throws JsonParserException, ParsingException { + public void testMixedNumberWordToLong() throws ParsingException { assertEquals(10, Utils.mixedNumberWordToLong("10")); assertEquals(10.5e3, Utils.mixedNumberWordToLong("10.5K"), 0.0); assertEquals(10.5e6, Utils.mixedNumberWordToLong("10.5M"), 0.0); assertEquals(10.5e6, Utils.mixedNumberWordToLong("10,5M"), 0.0); assertEquals(1.5e9, Utils.mixedNumberWordToLong("1,5B"), 0.0); } + + @Test + public void testJoin() { + assertEquals("some,random,stuff", Utils.join(",", Arrays.asList("some", "random", "stuff"))); + } }