|
1 | 1 | package org.schabi.newpipe.extractor.services.peertube.extractors; |
2 | 2 |
|
| 3 | +import com.grack.nanojson.JsonArray; |
3 | 4 | import com.grack.nanojson.JsonObject; |
4 | 5 | import com.grack.nanojson.JsonParser; |
5 | 6 | import com.grack.nanojson.JsonParserException; |
|
10 | 11 | import org.schabi.newpipe.extractor.downloader.Response; |
11 | 12 | import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
12 | 13 | import org.schabi.newpipe.extractor.exceptions.ParsingException; |
| 14 | +import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; |
13 | 15 | import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler; |
14 | 16 | import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper; |
15 | 17 | import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory; |
|
27 | 29 | public class PeertubeAccountExtractor extends ChannelExtractor { |
28 | 30 | private JsonObject json; |
29 | 31 | private final String baseUrl; |
| 32 | + private static final String ACCOUNTS = "accounts/"; |
30 | 33 |
|
31 | 34 | public PeertubeAccountExtractor(final StreamingService service, final ListLinkHandler linkHandler) throws ParsingException { |
32 | 35 | super(service, linkHandler); |
@@ -55,8 +58,31 @@ public String getFeedUrl() throws ParsingException { |
55 | 58 | } |
56 | 59 |
|
57 | 60 | @Override |
58 | | - public long getSubscriberCount() { |
59 | | - return json.getLong("followersCount"); |
| 61 | + public long getSubscriberCount() throws ParsingException { |
| 62 | + // The subscriber count cannot be retrieved directly. It needs to be calculated. |
| 63 | + // An accounts subscriber count is the number of the channel owner's subscriptions |
| 64 | + // plus the sum of all sub channels subscriptions. |
| 65 | + long subscribersCount = json.getLong("followersCount"); |
| 66 | + String accountVideoChannelUrl = baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT; |
| 67 | + if (getId().contains(ACCOUNTS)) { |
| 68 | + accountVideoChannelUrl += getId(); |
| 69 | + } else { |
| 70 | + accountVideoChannelUrl += ACCOUNTS + getId(); |
| 71 | + } |
| 72 | + accountVideoChannelUrl += "/video-channels"; |
| 73 | + |
| 74 | + try { |
| 75 | + final String responseBody = getDownloader().get(accountVideoChannelUrl).responseBody(); |
| 76 | + final JsonObject jsonResponse = JsonParser.object().from(responseBody); |
| 77 | + final JsonArray videoChannels = jsonResponse.getArray("data"); |
| 78 | + for (final Object videoChannel : videoChannels) { |
| 79 | + final JsonObject videoChannelJsonObject = (JsonObject) videoChannel; |
| 80 | + subscribersCount += videoChannelJsonObject.getInt("followersCount"); |
| 81 | + } |
| 82 | + } catch (final IOException | JsonParserException | ReCaptchaException ignored) { |
| 83 | + // something went wrong during video channels extraction, only return subscribers of ownerAccount |
| 84 | + } |
| 85 | + return subscribersCount; |
60 | 86 | } |
61 | 87 |
|
62 | 88 | @Override |
@@ -130,10 +156,10 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) |
130 | 156 | public void onFetchPage(@Nonnull final Downloader downloader) |
131 | 157 | throws IOException, ExtractionException { |
132 | 158 | String accountUrl = baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT; |
133 | | - if (getId().contains("accounts/")) { |
| 159 | + if (getId().contains(ACCOUNTS)) { |
134 | 160 | accountUrl += getId(); |
135 | 161 | } else { |
136 | | - accountUrl += "accounts/" + getId(); |
| 162 | + accountUrl += ACCOUNTS + getId(); |
137 | 163 | } |
138 | 164 |
|
139 | 165 | final Response response = downloader.get(accountUrl); |
|
0 commit comments