Skip to content

Commit 4668653

Browse files
committed
add support for affiliate
1 parent 016c2fc commit 4668653

8 files changed

Lines changed: 77 additions & 4 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public String[] getDonationLinks() {
214214
return new String[0];
215215
}
216216

217+
@Override
218+
public String[] getAffiliateLinks() {
219+
return new String[0];
220+
}
221+
217222
@Override
218223
public String getErrorMessage() {
219224
return null;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class ItagItem {
4343
new ItagItem(139, AUDIO, M4A, 48),
4444
new ItagItem(140, AUDIO, M4A, 128),
4545
new ItagItem(141, AUDIO, M4A, 256),
46+
new ItagItem(249, AUDIO, OPUS, 50),
47+
new ItagItem(250, AUDIO, OPUS, 70),
48+
new ItagItem(160, AUDIO, OPUS, 160),
4649

4750
/// VIDEO ONLY ////////////////////////////////////////////
4851
// ID Type Format Resolution FPS ///

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public String[] getDonationLinks() throws ParsingException {
196196
}
197197
for(Element a : linkHolder.select("a")) {
198198
String link = a.attr("abs:href");
199-
if(DonationLinkHelper.getServiceByLink(link) != DonationLinkHelper.DonationService.NO_DONATION) {
199+
if(DonationLinkHelper.getDonatoinServiceByLink(link) != DonationLinkHelper.DonationService.NO_DONATION) {
200200
links.add(link);
201201
}
202202
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ public String[] getDonationLinks() throws ParsingException {
534534
try {
535535
ArrayList<String> donationLinks = new ArrayList<>();
536536
for (String s : Parser.getLinksFromString(getDescription())) {
537-
if (DonationLinkHelper.getServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
537+
if (DonationLinkHelper.getDonatoinServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
538538
donationLinks.add(s);
539539
}
540540
}
@@ -546,6 +546,23 @@ public String[] getDonationLinks() throws ParsingException {
546546
}
547547
}
548548

549+
@Override
550+
public String[] getAffiliateLinks() throws ParsingException {
551+
try {
552+
ArrayList<String> donationLinks = new ArrayList<>();
553+
for (String s : Parser.getLinksFromString(getDescription())) {
554+
if (DonationLinkHelper.getAffiliateServiceByLink(s) != DonationLinkHelper.AffiliateService.NO_AFILIATE) {
555+
donationLinks.add(s);
556+
}
557+
}
558+
String[] donlret = new String[donationLinks.size()];
559+
donlret = donationLinks.toArray(donlret);
560+
return donlret;
561+
} catch (Exception e) {
562+
throw new ParsingException("Could not get afiliate links", e);
563+
}
564+
}
565+
549566

550567
/*//////////////////////////////////////////////////////////////////////////
551568
// Fetch page

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ protected long getTimestampSeconds(String regexPattern) throws ParsingException
142142
public abstract StreamInfoItemsCollector getRelatedVideos() throws IOException, ExtractionException;
143143

144144
public abstract String[] getDonationLinks() throws ExtractionException;
145+
public abstract String[] getAffiliateLinks() throws ExtractionException;
145146

146147
/**
147148
* Analyses the webpage's document and extracts any error message there might be.

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,16 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra
242242
} catch (Exception e) {
243243
streamInfo.addError(e);
244244
}
245+
try {
246+
streamInfo.setAffiliateLinks(extractor.getAffiliateLinks());
247+
} catch (Exception e) {
248+
streamInfo.addError(e);
249+
}
250+
try {
251+
streamInfo.setDonationLinks(extractor.getDonationLinks());
252+
} catch (Exception e) {
253+
streamInfo.addError(e);
254+
}
245255

246256
streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor));
247257
return streamInfo;
@@ -274,6 +284,9 @@ private static StreamInfo extractOptionalData(StreamInfo streamInfo, StreamExtra
274284
private long startPosition = 0;
275285
private List<Subtitles> subtitles;
276286

287+
private String[] donationLinks;
288+
private String[] affiliateLinks;
289+
277290
/**
278291
* Get the stream type
279292
*
@@ -466,4 +479,20 @@ public List<Subtitles> getSubtitles() {
466479
public void setSubtitles(List<Subtitles> subtitles) {
467480
this.subtitles = subtitles;
468481
}
482+
483+
public String[] getDonationLinks() {
484+
return donationLinks;
485+
}
486+
487+
public void setDonationLinks(String[] donationLinks) {
488+
this.donationLinks = donationLinks;
489+
}
490+
491+
public String[] getAffiliateLinks() {
492+
return affiliateLinks;
493+
}
494+
495+
public void setAffiliateLinks(String[] affiliateLinks) {
496+
this.affiliateLinks = affiliateLinks;
497+
}
469498
}

extractor/src/main/java/org/schabi/newpipe/extractor/utils/DonationLinkHelper.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ public class DonationLinkHelper {
77
public enum DonationService {
88
NO_DONATION,
99
PATREON,
10-
PAYPAL
10+
PAYPAL,
11+
}
12+
13+
public enum AffiliateService {
14+
NO_AFILIATE,
15+
AMAZON,
1116
}
1217

1318

14-
public static DonationService getServiceByLink(String link) throws MalformedURLException {
19+
public static DonationService getDonatoinServiceByLink(String link) throws MalformedURLException {
1520
URL url = new URL(link);
1621
switch (url.getHost()) {
1722
case "www.patreon.com":
@@ -27,5 +32,12 @@ public static DonationService getServiceByLink(String link) throws MalformedURLE
2732
}
2833
}
2934

35+
public static AffiliateService getAffiliateServiceByLink(String link) throws MalformedURLException {
36+
URL url = new URL(link);
37+
switch (url.getHost()) {
38+
case "amzn.to": return AffiliateService.AMAZON;
39+
default: return AffiliateService.NO_AFILIATE;
40+
}
41+
}
3042

3143
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public void getDonationLinksTest() throws Exception {
4949
assertTrue(String.valueOf(extractor.getDonationLinks().length),
5050
extractor.getDonationLinks().length == 2);
5151
}
52+
53+
@Test
54+
public void getAffiliateLinksTest() throws Exception {
55+
assertTrue(String.valueOf(extractor.getAffiliateLinks().length),
56+
extractor.getAffiliateLinks().length == 1);
57+
}
5258
}

0 commit comments

Comments
 (0)