enable donations for streams

This commit is contained in:
Christian Schabesberger 2018-04-07 21:36:20 +02:00
parent 1d94e7d477
commit 17e7e5a359
8 changed files with 137 additions and 0 deletions

View file

@ -5,6 +5,7 @@ dependencies {
implementation 'org.jsoup:jsoup:1.9.2'
implementation 'org.mozilla:rhino:1.7.7.1'
implementation 'com.github.spotbugs:spotbugs-annotations:3.1.0'
implementation 'org.nibor.autolink:autolink:0.8.0'
testImplementation 'junit:junit:4.12'
}

View file

@ -209,6 +209,10 @@ public class SoundcloudStreamExtractor extends StreamExtractor {
return collector;
}
@Override
public String[] getDonationLinks() {
return new String[0];
}
@Override
public String getErrorMessage() {

View file

@ -19,6 +19,7 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
import org.schabi.newpipe.extractor.stream.*;
import org.schabi.newpipe.extractor.utils.DonationLinkHelper;
import org.schabi.newpipe.extractor.utils.Parser;
import org.schabi.newpipe.extractor.utils.Utils;
@ -528,6 +529,24 @@ public class YoutubeStreamExtractor extends StreamExtractor {
return errorReason != null ? errorReason.toString() : null;
}
@Override
public String[] getDonationLinks() throws ParsingException {
try {
ArrayList<String> donationLinks = new ArrayList<>();
for (String s : Parser.getLinksFromString(getDescription())) {
if (DonationLinkHelper.getServiceByLink(s) != DonationLinkHelper.DonationService.NO_DONATION) {
donationLinks.add(s);
}
}
String[] donlret = new String[donationLinks.size()];
donlret = donationLinks.toArray(donlret);
return donlret;
} catch (Exception e) {
throw new ParsingException("Could not get donation links", e);
}
}
/*//////////////////////////////////////////////////////////////////////////
// Fetch page
//////////////////////////////////////////////////////////////////////////*/

View file

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

View file

@ -0,0 +1,31 @@
package org.schabi.newpipe.extractor.utils;
import java.net.MalformedURLException;
import java.net.URL;
public class DonationLinkHelper {
public enum DonationService {
NO_DONATION,
PATREON,
PAYPAL
}
public static DonationService getServiceByLink(String link) throws MalformedURLException {
URL url = new URL(link);
switch (url.getHost()) {
case "www.patreon.com":
return DonationService.PATREON;
case "patreon.com":
return DonationService.PATREON;
case "paypal.me":
return DonationService.PAYPAL;
case "www.paypal.me":
return DonationService.PAYPAL;
default:
return DonationService.NO_DONATION;
}
}
}

View file

@ -1,9 +1,14 @@
package org.schabi.newpipe.extractor.utils;
import org.nibor.autolink.LinkExtractor;
import org.nibor.autolink.LinkSpan;
import org.nibor.autolink.LinkType;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
@ -80,4 +85,23 @@ public class Parser {
}
return map;
}
public static String[] getLinksFromString(final String txt) throws ParsingException {
try {
ArrayList<String> links = new ArrayList<>();
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL, LinkType.WWW))
.build();
Iterable<LinkSpan> linkss = linkExtractor.extractLinks(txt);
for(LinkSpan ls : linkss) {
links.add(txt.substring(ls.getBeginIndex(), ls.getEndIndex()));
}
String[] linksarray = new String[links.size()];
linksarray = links.toArray(linksarray);
return linksarray;
} catch (Exception e) {
throw new ParsingException("Could not get links from string", e);
}
}
}

View file

@ -1,6 +1,7 @@
package org.schabi.newpipe.extractor.services.youtube;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
@ -105,7 +106,10 @@ public class YoutubeChannelExtractorTest {
}
@Test
@Ignore
public void testChannelDonation() throws Exception {
// this needs to be ignored since wed have to upgrade channel extractor to the new yt interface
// in order to make this work
assertTrue(extractor.getDonationLinks().length != 0);
}
}

View file

@ -0,0 +1,52 @@
package org.schabi.newpipe.extractor.services.youtube;
/*
* Created by Christian Schabesberger on 30.12.15.
*
* Copyright (C) Christian Schabesberger 2018 <chris.schabesberger@mailbox.org>
* YoutubeStreamExtractorDonationTest.java is part of NewPipe.
*
* NewPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NewPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.Downloader;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.stream.StreamExtractor;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
/**
* Test for {@link StreamExtractor}
*/
public class YoutubeStreamExtractorDonationTest {
private static YoutubeStreamExtractor extractor;
@BeforeClass
public static void setUp() throws Exception {
NewPipe.init(Downloader.getInstance());
extractor = (YoutubeStreamExtractor) YouTube
.getStreamExtractor("https://www.youtube.com/watch?v=pXb3jERMoI0");
extractor.fetchPage();
}
@Test
public void getDonationLinksTest() throws Exception {
assertTrue(String.valueOf(extractor.getDonationLinks().length),
extractor.getDonationLinks().length == 2);
}
}