add support for affiliate

This commit is contained in:
Christian Schabesberger 2018-04-29 21:31:24 +02:00
parent 016c2fc549
commit 466865385e
8 changed files with 77 additions and 4 deletions

View File

@ -214,6 +214,11 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
return new String[0];
}
@Override
public String[] getAffiliateLinks() {
return new String[0];
}
@Override
public String getErrorMessage() {
return null;

View File

@ -43,6 +43,9 @@ public class ItagItem {
new ItagItem(139, AUDIO, M4A, 48),
new ItagItem(140, AUDIO, M4A, 128),
new ItagItem(141, AUDIO, M4A, 256),
new ItagItem(249, AUDIO, OPUS, 50),
new ItagItem(250, AUDIO, OPUS, 70),
new ItagItem(160, AUDIO, OPUS, 160),
/// VIDEO ONLY ////////////////////////////////////////////
// ID Type Format Resolution FPS ///

View File

@ -196,7 +196,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
}
for(Element a : linkHolder.select("a")) {
String link = a.attr("abs:href");
if(DonationLinkHelper.getServiceByLink(link) != DonationLinkHelper.DonationService.NO_DONATION) {
if(DonationLinkHelper.getDonatoinServiceByLink(link) != DonationLinkHelper.DonationService.NO_DONATION) {
links.add(link);
}
}

View File

@ -534,7 +534,7 @@ public class YoutubeStreamExtractor extends StreamExtractor {
try {
ArrayList<String> donationLinks = new ArrayList<>();
for (String s : Parser.getLinksFromString(getDescription())) {
if (DonationLinkHelper.getServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
if (DonationLinkHelper.getDonatoinServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
donationLinks.add(s);
}
}
@ -546,6 +546,23 @@ public class YoutubeStreamExtractor extends StreamExtractor {
}
}
@Override
public String[] getAffiliateLinks() throws ParsingException {
try {
ArrayList<String> donationLinks = new ArrayList<>();
for (String s : Parser.getLinksFromString(getDescription())) {
if (DonationLinkHelper.getAffiliateServiceByLink(s) != DonationLinkHelper.AffiliateService.NO_AFILIATE) {
donationLinks.add(s);
}
}
String[] donlret = new String[donationLinks.size()];
donlret = donationLinks.toArray(donlret);
return donlret;
} catch (Exception e) {
throw new ParsingException("Could not get afiliate links", e);
}
}
/*//////////////////////////////////////////////////////////////////////////
// Fetch page

View File

@ -142,6 +142,7 @@ public abstract class StreamExtractor extends Extractor {
public abstract StreamInfoItemsCollector getRelatedVideos() throws IOException, ExtractionException;
public abstract String[] getDonationLinks() throws ExtractionException;
public abstract String[] getAffiliateLinks() throws ExtractionException;
/**
* Analyses the webpage's document and extracts any error message there might be.

View File

@ -242,6 +242,16 @@ public class StreamInfo extends Info {
} catch (Exception e) {
streamInfo.addError(e);
}
try {
streamInfo.setAffiliateLinks(extractor.getAffiliateLinks());
} catch (Exception e) {
streamInfo.addError(e);
}
try {
streamInfo.setDonationLinks(extractor.getDonationLinks());
} catch (Exception e) {
streamInfo.addError(e);
}
streamInfo.setRelatedStreams(ExtractorHelper.getRelatedVideosOrLogError(streamInfo, extractor));
return streamInfo;
@ -274,6 +284,9 @@ public class StreamInfo extends Info {
private long startPosition = 0;
private List<Subtitles> subtitles;
private String[] donationLinks;
private String[] affiliateLinks;
/**
* Get the stream type
*
@ -466,4 +479,20 @@ public class StreamInfo extends Info {
public void setSubtitles(List<Subtitles> subtitles) {
this.subtitles = subtitles;
}
public String[] getDonationLinks() {
return donationLinks;
}
public void setDonationLinks(String[] donationLinks) {
this.donationLinks = donationLinks;
}
public String[] getAffiliateLinks() {
return affiliateLinks;
}
public void setAffiliateLinks(String[] affiliateLinks) {
this.affiliateLinks = affiliateLinks;
}
}

View File

@ -7,11 +7,16 @@ public class DonationLinkHelper {
public enum DonationService {
NO_DONATION,
PATREON,
PAYPAL
PAYPAL,
}
public enum AffiliateService {
NO_AFILIATE,
AMAZON,
}
public static DonationService getServiceByLink(String link) throws MalformedURLException {
public static DonationService getDonatoinServiceByLink(String link) throws MalformedURLException {
URL url = new URL(link);
switch (url.getHost()) {
case "www.patreon.com":
@ -27,5 +32,12 @@ public class DonationLinkHelper {
}
}
public static AffiliateService getAffiliateServiceByLink(String link) throws MalformedURLException {
URL url = new URL(link);
switch (url.getHost()) {
case "amzn.to": return AffiliateService.AMAZON;
default: return AffiliateService.NO_AFILIATE;
}
}
}

View File

@ -49,4 +49,10 @@ public class YoutubeStreamExtractorDonationTest {
assertTrue(String.valueOf(extractor.getDonationLinks().length),
extractor.getDonationLinks().length == 2);
}
@Test
public void getAffiliateLinksTest() throws Exception {
assertTrue(String.valueOf(extractor.getAffiliateLinks().length),
extractor.getAffiliateLinks().length == 1);
}
}