Extract real subscribers of a Peertube account
Apply fix provided in the PR and move all accounts/ strings to a constant, ACCOUNTS.
This commit is contained in:
parent
12835bfae1
commit
efe2b964f0
2 changed files with 29 additions and 5 deletions
|
@ -1,5 +1,6 @@
|
||||||
package org.schabi.newpipe.extractor.services.peertube.extractors;
|
package org.schabi.newpipe.extractor.services.peertube.extractors;
|
||||||
|
|
||||||
|
import com.grack.nanojson.JsonArray;
|
||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
import com.grack.nanojson.JsonParser;
|
import com.grack.nanojson.JsonParser;
|
||||||
import com.grack.nanojson.JsonParserException;
|
import com.grack.nanojson.JsonParserException;
|
||||||
|
@ -10,6 +11,7 @@ import org.schabi.newpipe.extractor.downloader.Downloader;
|
||||||
import org.schabi.newpipe.extractor.downloader.Response;
|
import org.schabi.newpipe.extractor.downloader.Response;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||||
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandler;
|
||||||
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
|
import org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper;
|
||||||
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
|
import org.schabi.newpipe.extractor.services.peertube.linkHandler.PeertubeChannelLinkHandlerFactory;
|
||||||
|
@ -27,6 +29,7 @@ import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
||||||
public class PeertubeAccountExtractor extends ChannelExtractor {
|
public class PeertubeAccountExtractor extends ChannelExtractor {
|
||||||
private JsonObject json;
|
private JsonObject json;
|
||||||
private final String baseUrl;
|
private final String baseUrl;
|
||||||
|
private static final String ACCOUNTS = "accounts/";
|
||||||
|
|
||||||
public PeertubeAccountExtractor(final StreamingService service, final ListLinkHandler linkHandler) throws ParsingException {
|
public PeertubeAccountExtractor(final StreamingService service, final ListLinkHandler linkHandler) throws ParsingException {
|
||||||
super(service, linkHandler);
|
super(service, linkHandler);
|
||||||
|
@ -55,8 +58,29 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getSubscriberCount() {
|
public long getSubscriberCount() throws ParsingException {
|
||||||
return json.getLong("followersCount");
|
long subscribersCount = json.getLong("followersCount");
|
||||||
|
String accountVideoChannelUrl = baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT;
|
||||||
|
if (getId().contains(ACCOUNTS)) {
|
||||||
|
accountVideoChannelUrl += getId();
|
||||||
|
} else {
|
||||||
|
accountVideoChannelUrl += ACCOUNTS + getId();
|
||||||
|
}
|
||||||
|
accountVideoChannelUrl += "/video-channels";
|
||||||
|
|
||||||
|
try {
|
||||||
|
final String responseBody = getDownloader().get(accountVideoChannelUrl).responseBody();
|
||||||
|
final JsonObject jsonResponse = JsonParser.object().from(responseBody);
|
||||||
|
final JsonArray videoChannels = jsonResponse.getArray("data");
|
||||||
|
for (final Object videoChannel : videoChannels) {
|
||||||
|
final JsonObject videoChannelJsonObject = (JsonObject) videoChannel;
|
||||||
|
subscribersCount += videoChannelJsonObject.getInt("followersCount");
|
||||||
|
}
|
||||||
|
} catch (final IOException | JsonParserException | ReCaptchaException ignored) {
|
||||||
|
// something went wrong during video channels extraction, only return subscribers of ownerAccount
|
||||||
|
}
|
||||||
|
System.out.println(subscribersCount);
|
||||||
|
return subscribersCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -130,10 +154,10 @@ public class PeertubeAccountExtractor extends ChannelExtractor {
|
||||||
public void onFetchPage(@Nonnull final Downloader downloader)
|
public void onFetchPage(@Nonnull final Downloader downloader)
|
||||||
throws IOException, ExtractionException {
|
throws IOException, ExtractionException {
|
||||||
String accountUrl = baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT;
|
String accountUrl = baseUrl + PeertubeChannelLinkHandlerFactory.API_ENDPOINT;
|
||||||
if (getId().contains("accounts/")) {
|
if (getId().contains(ACCOUNTS)) {
|
||||||
accountUrl += getId();
|
accountUrl += getId();
|
||||||
} else {
|
} else {
|
||||||
accountUrl += "accounts/" + getId();
|
accountUrl += ACCOUNTS + getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
final Response response = downloader.get(accountUrl);
|
final Response response = downloader.get(accountUrl);
|
||||||
|
|
|
@ -102,7 +102,7 @@ public class PeertubeAccountExtractorTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSubscriberCount() throws ParsingException {
|
public void testSubscriberCount() throws ParsingException {
|
||||||
assertTrue("Wrong subscriber count", extractor.getSubscriberCount() >= 500);
|
assertTrue("Wrong subscriber count", extractor.getSubscriberCount() >= 425);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue