Skip to content

Commit 1d94e7d

Browse files
committed
add ability to get donation links from channel
1 parent 77a74b8 commit 1d94e7d

5 files changed

Lines changed: 59 additions & 0 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelExtractor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ protected UrlIdHandler getUrlIdHandler() {
4545
public abstract String getFeedUrl() throws ParsingException;
4646
public abstract long getSubscriberCount() throws ParsingException;
4747
public abstract String getDescription() throws ParsingException;
48+
public abstract String[] getDonationLinks() throws ParsingException;
4849
}

extractor/src/main/java/org/schabi/newpipe/extractor/channel/ChannelInfo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
9292
} catch (Exception e) {
9393
info.addError(e);
9494
}
95+
try {
96+
info.setDonationLinks(extractor.getDonationLinks());
97+
} catch (Exception e) {
98+
info.addError(e);
99+
}
95100

96101
return info;
97102
}
@@ -101,6 +106,7 @@ public static ChannelInfo getInfo(ChannelExtractor extractor) throws IOException
101106
private String feedUrl;
102107
private long subscriberCount = -1;
103108
private String description;
109+
private String[] donationLinks;
104110

105111
public String getAvatarUrl() {
106112
return avatarUrl;
@@ -141,4 +147,12 @@ public String getDescription() {
141147
public void setDescription(String description) {
142148
this.description = description;
143149
}
150+
151+
public String[] getDonationLinks() {
152+
return donationLinks;
153+
}
154+
155+
public void setDonationLinks(String[] donationLinks) {
156+
this.donationLinks = donationLinks;
157+
}
144158
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChannelExtractor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,9 @@ public InfoItemsPage<StreamInfoItem> getPage(final String pageUrl) throws IOExce
133133

134134
return new InfoItemsPage<>(collector, nextPageUrl);
135135
}
136+
137+
@Override
138+
public String[] getDonationLinks() {
139+
return new String[0];
140+
}
136141
}

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractor.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
import com.grack.nanojson.JsonObject;
55
import com.grack.nanojson.JsonParser;
66
import com.grack.nanojson.JsonParserException;
7+
import com.sun.org.apache.xerces.internal.xs.StringList;
78
import org.jsoup.Jsoup;
89
import org.jsoup.nodes.Document;
910
import org.jsoup.nodes.Element;
1011
import org.schabi.newpipe.extractor.Downloader;
1112
import org.schabi.newpipe.extractor.NewPipe;
1213
import org.schabi.newpipe.extractor.StreamingService;
14+
import org.schabi.newpipe.extractor.UrlIdHandler;
1315
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
1416
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1517
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -20,6 +22,7 @@
2022

2123
import javax.annotation.Nonnull;
2224
import java.io.IOException;
25+
import java.util.ArrayList;
2326

2427
/*
2528
* Created by Christian Schabesberger on 25.07.16.
@@ -181,6 +184,27 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException,
181184
return new InfoItemsPage<>(collector, getNextPageUrlFromAjaxPage(ajaxJson, pageUrl));
182185
}
183186

187+
@Override
188+
public String[] getDonationLinks() throws ParsingException {
189+
try {
190+
ArrayList<String> links = new ArrayList<>();
191+
Element linkHolder = doc.select("div[id=\"links-holder\"]").first();
192+
if(linkHolder == null) {
193+
// this occures if no links are embeded into the channel
194+
return new String[0];
195+
}
196+
for(Element a : linkHolder.select("a")) {
197+
System.err.println(a.attr("abs:href"));
198+
links.add(a.attr("abs:href"));
199+
}
200+
String[] retLinks = new String[links.size()];
201+
retLinks = links.toArray(retLinks);
202+
return retLinks;
203+
} catch (Exception e) {
204+
throw new ParsingException("Could not get donation links", e);
205+
}
206+
}
207+
184208
private String getNextPageUrlFromAjaxPage(final JsonObject ajaxJson, final String pageUrl)
185209
throws ParsingException {
186210
String loadMoreHtmlDataRaw = ajaxJson.getString("load_more_widget_html");

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public void testFeedUrl() throws Exception {
103103
public void testSubscriberCount() throws Exception {
104104
assertTrue("Wrong subscriber count", extractor.getSubscriberCount() >= 0);
105105
}
106+
107+
@Test
108+
public void testChannelDonation() throws Exception {
109+
assertTrue(extractor.getDonationLinks().length != 0);
110+
}
106111
}
107112

108113
public static class Kurzgesagt implements BaseChannelExtractorTest {
@@ -205,6 +210,11 @@ public void testFeedUrl() throws Exception {
205210
public void testSubscriberCount() throws Exception {
206211
assertTrue("Wrong subscriber count", extractor.getSubscriberCount() >= 5e6);
207212
}
213+
214+
@Test
215+
public void testChannelDonation() throws Exception {
216+
assertTrue(extractor.getDonationLinks().length == 0);
217+
}
208218
}
209219

210220
public static class CaptainDisillusion implements BaseChannelExtractorTest {
@@ -389,6 +399,11 @@ public void testFeedUrl() throws Exception {
389399
public void testSubscriberCount() throws Exception {
390400
assertTrue("Wrong subscriber count", extractor.getSubscriberCount() >= 50);
391401
}
402+
403+
@Test
404+
public void testChannelDonation() throws Exception {
405+
assertTrue(extractor.getDonationLinks().length == 0);
406+
}
392407
}
393408
};
394409

0 commit comments

Comments
 (0)