Skip to content

Commit 17ba8a5

Browse files
committed
Clean up the code
1 parent 9b6fe1d commit 17ba8a5

23 files changed

Lines changed: 104 additions & 177 deletions

extractor/src/main/java/org/schabi/newpipe/extractor/ListExtractor.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
44
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
5-
import org.schabi.newpipe.extractor.utils.Utils;
65

7-
import javax.annotation.Nonnull;
86
import java.io.IOException;
97
import java.util.Collections;
108
import java.util.List;
119

12-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
10+
import javax.annotation.Nonnull;
1311

1412

1513
/**
@@ -110,8 +108,7 @@ public InfoItemsPage(List<T> itemsList, Page nextPage, List<Throwable> errors) {
110108
}
111109

112110
public boolean hasNextPage() {
113-
return nextPage != null && (!isNullOrEmpty(nextPage.getUrl())
114-
|| !isNullOrEmpty(nextPage.getIds()));
111+
return Page.isValid(nextPage);
115112
}
116113

117114
public List<T> getItems() {

extractor/src/main/java/org/schabi/newpipe/extractor/ListInfo.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import java.util.List;
66

7-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
8-
97
public abstract class ListInfo<T extends InfoItem> extends Info {
108
private List<T> relatedItems;
119
private Page nextPage = null;
@@ -39,8 +37,7 @@ public void setRelatedItems(List<T> relatedItems) {
3937
}
4038

4139
public boolean hasNextPage() {
42-
return nextPage != null && (!isNullOrEmpty(nextPage.getUrl())
43-
|| !isNullOrEmpty(nextPage.getIds()));
40+
return Page.isValid(nextPage);
4441
}
4542

4643
public Page getNextPage() {

extractor/src/main/java/org/schabi/newpipe/extractor/Page.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55
import java.util.Map;
66

7+
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
8+
79
public class Page implements Serializable {
810
private final String url;
911
private final List<String> ids;
@@ -42,4 +44,9 @@ public List<String> getIds() {
4244
public Map<String, String> getCookies() {
4345
return cookies;
4446
}
47+
48+
public static boolean isValid(final Page page) {
49+
return page != null && (!isNullOrEmpty(page.getUrl())
50+
|| !isNullOrEmpty(page.getIds()));
51+
}
4552
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/PeertubeParsingHelper.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.schabi.newpipe.extractor.services.peertube;
22

3+
import com.grack.nanojson.JsonArray;
34
import com.grack.nanojson.JsonObject;
45

6+
import org.schabi.newpipe.extractor.InfoItemsCollector;
57
import org.schabi.newpipe.extractor.Page;
68
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
79
import org.schabi.newpipe.extractor.exceptions.ParsingException;
10+
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeStreamInfoItemExtractor;
11+
import org.schabi.newpipe.extractor.utils.JsonUtils;
812
import org.schabi.newpipe.extractor.utils.Parser;
913
import org.schabi.newpipe.extractor.utils.Utils;
1014

@@ -66,4 +70,21 @@ public static Page getNextPage(final String prevPageUrl, final long total) {
6670
return new Page(prevPageUrl.replace(START_KEY + "=" + prevStart, START_KEY + "=" + nextStart));
6771
}
6872
}
73+
74+
public static void collectStreamsFrom(final InfoItemsCollector collector, final JsonObject json, final String baseUrl) throws ParsingException {
75+
final JsonArray contents;
76+
try {
77+
contents = (JsonArray) JsonUtils.getValue(json, "data");
78+
} catch (Exception e) {
79+
throw new ParsingException("Unable to extract list info", e);
80+
}
81+
82+
for (final Object c : contents) {
83+
if (c instanceof JsonObject) {
84+
final JsonObject item = (JsonObject) c;
85+
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
86+
collector.commit(extractor);
87+
}
88+
}
89+
}
6990
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeAccountExtractor.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.extractor.services.peertube.extractors;
22

3-
import com.grack.nanojson.JsonArray;
43
import com.grack.nanojson.JsonObject;
54
import com.grack.nanojson.JsonParser;
65
import com.grack.nanojson.JsonParserException;
@@ -24,6 +23,7 @@
2423
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
2524
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
2625
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
26+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
2727
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2828

2929
public class PeertubeAccountExtractor extends ChannelExtractor {
@@ -57,9 +57,8 @@ public String getFeedUrl() throws ParsingException {
5757
}
5858

5959
@Override
60-
public long getSubscriberCount() throws ParsingException {
61-
final Number number = JsonUtils.getNumber(json, "followersCount");
62-
return number.longValue();
60+
public long getSubscriberCount() {
61+
return json.getLong("followersCount");
6362
}
6463

6564
@Override
@@ -92,23 +91,6 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
9291
return getPage(new Page(pageUrl));
9392
}
9493

95-
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException {
96-
final JsonArray contents;
97-
try {
98-
contents = (JsonArray) JsonUtils.getValue(json, "data");
99-
} catch (Exception e) {
100-
throw new ParsingException("Unable to extract account streams", e);
101-
}
102-
103-
for (final Object c : contents) {
104-
if (c instanceof JsonObject) {
105-
final JsonObject item = (JsonObject) c;
106-
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
107-
collector.commit(extractor);
108-
}
109-
}
110-
}
111-
11294
@Override
11395
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException {
11496
if (page == null || isNullOrEmpty(page.getUrl())) {
@@ -128,10 +110,10 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
128110

129111
if (json != null) {
130112
PeertubeParsingHelper.validate(json);
131-
final long total = JsonUtils.getNumber(json, "total").longValue();
113+
final long total = json.getLong("total");
132114

133115
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
134-
collectStreamsFrom(collector, json);
116+
collectStreamsFrom(collector, json, getBaseUrl());
135117

136118
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total));
137119
} else {

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeChannelExtractor.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.extractor.services.peertube.extractors;
22

3-
import com.grack.nanojson.JsonArray;
43
import com.grack.nanojson.JsonObject;
54
import com.grack.nanojson.JsonParser;
65
import com.grack.nanojson.JsonParserException;
@@ -24,6 +23,7 @@
2423
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
2524
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
2625
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
26+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
2727
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2828

2929

@@ -58,9 +58,8 @@ public String getFeedUrl() throws ParsingException {
5858
}
5959

6060
@Override
61-
public long getSubscriberCount() throws ParsingException {
62-
final Number number = JsonUtils.getNumber(json, "followersCount");
63-
return number.longValue();
61+
public long getSubscriberCount() {
62+
return json.getLong("followersCount");
6463
}
6564

6665
@Override
@@ -99,23 +98,6 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
9998
return getPage(new Page(pageUrl));
10099
}
101100

102-
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException {
103-
final JsonArray contents;
104-
try {
105-
contents = (JsonArray) JsonUtils.getValue(json, "data");
106-
} catch (Exception e) {
107-
throw new ParsingException("Unable to extract channel streams", e);
108-
}
109-
110-
for (final Object c : contents) {
111-
if (c instanceof JsonObject) {
112-
final JsonObject item = (JsonObject) c;
113-
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
114-
collector.commit(extractor);
115-
}
116-
}
117-
}
118-
119101
@Override
120102
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException {
121103
if (page == null || isNullOrEmpty(page.getUrl())) {
@@ -135,10 +117,10 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
135117

136118
if (json != null) {
137119
PeertubeParsingHelper.validate(json);
138-
final long total = JsonUtils.getNumber(json, "total").longValue();
120+
final long total = json.getLong("total");
139121

140122
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
141-
collectStreamsFrom(collector, json);
123+
collectStreamsFrom(collector, json, getBaseUrl());
142124

143125
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total));
144126
} else {

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public InfoItemsPage<CommentsInfoItem> getPage(final Page page) throws IOExcepti
6969

7070
if (json != null) {
7171
PeertubeParsingHelper.validate(json);
72-
final long total = JsonUtils.getNumber(json, "total").longValue();
72+
final long total = json.getLong("total");
7373

7474
final CommentsInfoItemsCollector collector = new CommentsInfoItemsCollector(getServiceId());
7575
collectCommentsFrom(collector, json);

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeCommentsInfoItemExtractor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
1212
import org.schabi.newpipe.extractor.utils.JsonUtils;
1313

14+
import java.util.Objects;
15+
1416
public class PeertubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
1517
private final JsonObject item;
1618
private final String url;
@@ -71,9 +73,8 @@ public String getCommentText() throws ParsingException {
7173
}
7274

7375
@Override
74-
public String getCommentId() throws ParsingException {
75-
final Number value = JsonUtils.getNumber(item, "id");
76-
return value.toString();
76+
public String getCommentId() {
77+
return Objects.toString(item.getLong("id"), null);
7778
}
7879

7980
@Override

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubePlaylistExtractor.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.extractor.services.peertube.extractors;
22

3-
import com.grack.nanojson.JsonArray;
43
import com.grack.nanojson.JsonObject;
54
import com.grack.nanojson.JsonParser;
65
import com.grack.nanojson.JsonParserException;
@@ -16,7 +15,6 @@
1615
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
1716
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1817
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
19-
import org.schabi.newpipe.extractor.utils.JsonUtils;
2018
import org.schabi.newpipe.extractor.utils.Utils;
2119

2220
import java.io.IOException;
@@ -26,6 +24,7 @@
2624
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
2725
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
2826
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
27+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
2928
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
3029

3130
public class PeertubePlaylistExtractor extends PlaylistExtractor {
@@ -62,7 +61,7 @@ public String getUploaderAvatarUrl() throws ParsingException {
6261

6362
@Override
6463
public long getStreamCount() {
65-
return playlistInfo.getNumber("videosLength").longValue();
64+
return playlistInfo.getLong("videosLength");
6665
}
6766

6867
@Nonnull
@@ -89,24 +88,6 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
8988
return getPage(new Page(getUrl() + "/videos?" + START_KEY + "=0&" + COUNT_KEY + "=" + ITEMS_PER_PAGE));
9089
}
9190

92-
private void collectStreamsFrom(final StreamInfoItemsCollector collector, final JsonObject json) throws ParsingException {
93-
final JsonArray contents;
94-
try {
95-
contents = (JsonArray) JsonUtils.getValue(json, "data");
96-
} catch (Exception e) {
97-
throw new ParsingException("Unable to extract playlist streams", e);
98-
}
99-
100-
final String baseUrl = getBaseUrl();
101-
for (final Object c : contents) {
102-
if (c instanceof JsonObject) {
103-
final JsonObject item = (JsonObject) c;
104-
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
105-
collector.commit(extractor);
106-
}
107-
}
108-
}
109-
11091
@Override
11192
public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException, ExtractionException {
11293
if (page == null || isNullOrEmpty(page.getUrl())) {
@@ -126,10 +107,10 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
126107

127108
if (json != null) {
128109
PeertubeParsingHelper.validate(json);
129-
final long total = JsonUtils.getNumber(json, "total").longValue();
110+
final long total = json.getLong("total");
130111

131112
final StreamInfoItemsCollector collector = new StreamInfoItemsCollector(getServiceId());
132-
collectStreamsFrom(collector, json);
113+
collectStreamsFrom(collector, json, getBaseUrl());
133114

134115
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total));
135116
} else {

extractor/src/main/java/org/schabi/newpipe/extractor/services/peertube/extractors/PeertubeSearchExtractor.java

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.schabi.newpipe.extractor.services.peertube.extractors;
22

3-
import com.grack.nanojson.JsonArray;
43
import com.grack.nanojson.JsonObject;
54
import com.grack.nanojson.JsonParser;
65

@@ -15,7 +14,6 @@
1514
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
1615
import org.schabi.newpipe.extractor.search.SearchExtractor;
1716
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
18-
import org.schabi.newpipe.extractor.utils.JsonUtils;
1917
import org.schabi.newpipe.extractor.utils.Utils;
2018

2119
import java.io.IOException;
@@ -25,6 +23,7 @@
2523
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.COUNT_KEY;
2624
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.ITEMS_PER_PAGE;
2725
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.START_KEY;
26+
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.collectStreamsFrom;
2827
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
2928

3029
public class PeertubeSearchExtractor extends SearchExtractor {
@@ -49,24 +48,6 @@ public InfoItemsPage<InfoItem> getInitialPage() throws IOException, ExtractionEx
4948
return getPage(new Page(pageUrl));
5049
}
5150

52-
private void collectStreamsFrom(final InfoItemsSearchCollector collector, final JsonObject json) throws ParsingException {
53-
final JsonArray contents;
54-
try {
55-
contents = (JsonArray) JsonUtils.getValue(json, "data");
56-
} catch (Exception e) {
57-
throw new ParsingException("unable to extract search info", e);
58-
}
59-
60-
final String baseUrl = getBaseUrl();
61-
for (final Object c : contents) {
62-
if (c instanceof JsonObject) {
63-
final JsonObject item = (JsonObject) c;
64-
final PeertubeStreamInfoItemExtractor extractor = new PeertubeStreamInfoItemExtractor(item, baseUrl);
65-
collector.commit(extractor);
66-
}
67-
}
68-
}
69-
7051
@Override
7152
public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, ExtractionException {
7253
if (page == null || isNullOrEmpty(page.getUrl())) {
@@ -86,10 +67,10 @@ public InfoItemsPage<InfoItem> getPage(final Page page) throws IOException, Extr
8667

8768
if (json != null) {
8869
PeertubeParsingHelper.validate(json);
89-
final long total = JsonUtils.getNumber(json, "total").longValue();
70+
final long total = json.getLong("total");
9071

9172
final InfoItemsSearchCollector collector = new InfoItemsSearchCollector(getServiceId());
92-
collectStreamsFrom(collector, json);
73+
collectStreamsFrom(collector, json, getBaseUrl());
9374

9475
return new InfoItemsPage<>(collector, PeertubeParsingHelper.getNextPage(page.getUrl(), total));
9576
} else {

0 commit comments

Comments
 (0)