Skip to content

Commit e5125c1

Browse files
authored
Merge pull request #78 from TeamNewPipe/donation
enable Donations for streams
2 parents 1c8dafe + 016c2fc commit e5125c1

13 files changed

Lines changed: 200 additions & 1 deletion

File tree

extractor/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dependencies {
55
implementation 'org.jsoup:jsoup:1.9.2'
66
implementation 'org.mozilla:rhino:1.7.7.1'
77
implementation 'com.github.spotbugs:spotbugs-annotations:3.1.0'
8+
implementation 'org.nibor.autolink:autolink:0.8.0'
89

910
testImplementation 'junit:junit:4.12'
1011
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public enum MediaFormat {
3535
// audio formats
3636
M4A (0x3, "m4a", "m4a", "audio/mp4"),
3737
WEBMA (0x4, "WebM", "webm", "audio/webm"),
38-
MP3 (0x5, "MP3", "mp3", "audio/mpeg");
38+
MP3 (0x5, "MP3", "mp3", "audio/mpeg"),
39+
OPUS (0x6, "opus", "opus", "audio/opus");
3940

4041
public final int id;
4142
public final String name;

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/soundcloud/SoundcloudStreamExtractor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ public StreamInfoItemsCollector getRelatedVideos() throws IOException, Extractio
209209
return collector;
210210
}
211211

212+
@Override
213+
public String[] getDonationLinks() {
214+
return new String[0];
215+
}
212216

213217
@Override
214218
public String getErrorMessage() {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@
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;
1618
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1719
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
20+
import org.schabi.newpipe.extractor.utils.DonationLinkHelper;
1821
import org.schabi.newpipe.extractor.utils.Parser;
1922
import org.schabi.newpipe.extractor.utils.Utils;
2023

2124
import javax.annotation.Nonnull;
2225
import java.io.IOException;
26+
import java.util.ArrayList;
2327

2428
/*
2529
* Created by Christian Schabesberger on 25.07.16.
@@ -181,6 +185,29 @@ public InfoItemsPage<StreamInfoItem> getPage(String pageUrl) throws IOException,
181185
return new InfoItemsPage<>(collector, getNextPageUrlFromAjaxPage(ajaxJson, pageUrl));
182186
}
183187

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

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2020
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
2121
import org.schabi.newpipe.extractor.stream.*;
22+
import org.schabi.newpipe.extractor.utils.DonationLinkHelper;
2223
import org.schabi.newpipe.extractor.utils.Parser;
2324
import org.schabi.newpipe.extractor.utils.Utils;
2425

@@ -528,6 +529,24 @@ public String getErrorMessage() {
528529
return errorReason != null ? errorReason.toString() : null;
529530
}
530531

532+
@Override
533+
public String[] getDonationLinks() throws ParsingException {
534+
try {
535+
ArrayList<String> donationLinks = new ArrayList<>();
536+
for (String s : Parser.getLinksFromString(getDescription())) {
537+
if (DonationLinkHelper.getServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
538+
donationLinks.add(s);
539+
}
540+
}
541+
String[] donlret = new String[donationLinks.size()];
542+
donlret = donationLinks.toArray(donlret);
543+
return donlret;
544+
} catch (Exception e) {
545+
throw new ParsingException("Could not get donation links", e);
546+
}
547+
}
548+
549+
531550
/*//////////////////////////////////////////////////////////////////////////
532551
// Fetch page
533552
//////////////////////////////////////////////////////////////////////////*/

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException
141141
public abstract StreamInfoItem getNextVideo() throws IOException, ExtractionException;
142142
public abstract StreamInfoItemsCollector getRelatedVideos() throws IOException, ExtractionException;
143143

144+
public abstract String[] getDonationLinks() throws ExtractionException;
145+
144146
/**
145147
* Analyses the webpage's document and extracts any error message there might be.
146148
*
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.schabi.newpipe.extractor.utils;
2+
3+
import java.net.MalformedURLException;
4+
import java.net.URL;
5+
6+
public class DonationLinkHelper {
7+
public enum DonationService {
8+
NO_DONATION,
9+
PATREON,
10+
PAYPAL
11+
}
12+
13+
14+
public static DonationService getServiceByLink(String link) throws MalformedURLException {
15+
URL url = new URL(link);
16+
switch (url.getHost()) {
17+
case "www.patreon.com":
18+
return DonationService.PATREON;
19+
case "patreon.com":
20+
return DonationService.PATREON;
21+
case "paypal.me":
22+
return DonationService.PAYPAL;
23+
case "www.paypal.me":
24+
return DonationService.PAYPAL;
25+
default:
26+
return DonationService.NO_DONATION;
27+
}
28+
}
29+
30+
31+
}

0 commit comments

Comments
 (0)