Skip to content

Commit

Permalink
Merge pull request #1207 from TeamNewPipe/fix/peertube-certain-domains
Browse files Browse the repository at this point in the history
[PeerTube] Fix parsing ID for instances whose domain ends with a or c
  • Loading branch information
TobiGr authored Oct 5, 2024
2 parents eb30316 + bcacfc5 commit 743a400
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.utils.Parser;

import javax.annotation.Nonnull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
Expand All @@ -14,6 +15,7 @@ public final class PeertubeChannelLinkHandlerFactory extends ListLinkHandlerFact
private static final PeertubeChannelLinkHandlerFactory INSTANCE
= new PeertubeChannelLinkHandlerFactory();
private static final String ID_PATTERN = "((accounts|a)|(video-channels|c))/([^/?&#]*)";
private static final String ID_URL_PATTERN = "/((accounts|a)|(video-channels|c))/([^/?&#]*)";
public static final String API_ENDPOINT = "/api/v1/";

private PeertubeChannelLinkHandlerFactory() {
Expand All @@ -25,7 +27,7 @@ public static PeertubeChannelLinkHandlerFactory getInstance() {

@Override
public String getId(final String url) throws ParsingException, UnsupportedOperationException {
return fixId(Parser.matchGroup(ID_PATTERN, url, 0));
return fixId(Parser.matchGroup(ID_URL_PATTERN, url, 0));
}

@Override
Expand Down Expand Up @@ -74,12 +76,13 @@ public boolean onAcceptUrl(final String url) {
* @param id the id to fix
* @return the fixed id
*/
private String fixId(final String id) {
if (id.startsWith("a/")) {
return "accounts" + id.substring(1);
} else if (id.startsWith("c/")) {
return "video-channels" + id.substring(1);
private String fixId(@Nonnull final String id) {
final String cleanedId = id.startsWith("/") ? id.substring(1) : id;
if (cleanedId.startsWith("a/")) {
return "accounts" + cleanedId.substring(1);
} else if (cleanedId.startsWith("c/")) {
return "video-channels" + cleanedId.substring(1);
}
return id;
return cleanedId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,111 @@ public void testTags() throws Exception {
assertTrue(extractor.getTags().isEmpty());
}
}

public static class DocumentaryChannel implements BaseChannelExtractorTest {
// https://github.com/TeamNewPipe/NewPipe/issues/11369

private static PeertubeChannelExtractor extractor;

@BeforeAll
public static void setUp() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance());
// setting instance might break test when running in parallel
PeerTube.setInstance(new PeertubeInstance("https://kolektiva.media", "kolektiva.media"));
extractor = (PeertubeChannelExtractor) PeerTube
.getChannelExtractor("https://kolektiva.media/video-channels/documentary_channel");
extractor.fetchPage();
}

/*//////////////////////////////////////////////////////////////////////////
// Extractor
//////////////////////////////////////////////////////////////////////////*/

@Test
public void testServiceId() {
assertEquals(PeerTube.getServiceId(), extractor.getServiceId());
}

@Test
public void testName() throws ParsingException {
assertEquals("Documentary Channel", extractor.getName());
}

@Test
public void testId() throws ParsingException {
assertEquals("video-channels/documentary_channel", extractor.getId());
}

@Test
public void testUrl() throws ParsingException {
assertEquals("https://kolektiva.media/video-channels/documentary_channel", extractor.getUrl());
}

@Test
public void testOriginalUrl() throws ParsingException {
assertEquals("https://kolektiva.media/video-channels/documentary_channel", extractor.getOriginalUrl());
}

/*//////////////////////////////////////////////////////////////////////////
// ChannelExtractor
//////////////////////////////////////////////////////////////////////////*/

@Test
public void testDescription() {
assertNotNull(extractor.getDescription());
}

@Test
void testParentChannelName() throws ParsingException {
assertEquals("consumedmind", extractor.getParentChannelName());
}

@Test
void testParentChannelUrl() throws ParsingException {
assertEquals("https://kolektiva.media/accounts/consumedmind", extractor.getParentChannelUrl());
}

@Test
void testParentChannelAvatars() {
defaultTestImageCollection(extractor.getParentChannelAvatars());
}

@Test
public void testAvatars() {
defaultTestImageCollection(extractor.getAvatars());
}

@Test
public void testBanners() throws ParsingException {
assertGreaterOrEqual(1, extractor.getBanners().size());
}

@Test
public void testFeedUrl() throws ParsingException {
assertEquals("https://kolektiva.media/feeds/videos.xml?videoChannelId=2994", extractor.getFeedUrl());
}

@Test
public void testSubscriberCount() {
assertGreaterOrEqual(25, extractor.getSubscriberCount());
}

@Test
@Override
public void testVerified() throws Exception {
assertFalse(extractor.isVerified());
}

@Test
@Override
public void testTabs() throws Exception {
assertTabsContain(extractor.getTabs(), ChannelTabs.VIDEOS, ChannelTabs.PLAYLISTS);
}

@Test
@Override
public void testTags() throws Exception {
assertTrue(extractor.getTags().isEmpty());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void acceptUrlTest() throws ParsingException {
assertTrue(linkHandler.acceptUrl("https://peertube.stream/video-channels/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/c/[email protected]/videos"));
assertTrue(linkHandler.acceptUrl("https://peertube.stream/api/v1/video-channels/7682d9f2-07be-4622-862e-93ec812e2ffa"));
assertTrue(linkHandler.acceptUrl("https://kolektiva.media/video-channels/documentary_channel"));

assertDoNotAcceptNonURLs(linkHandler);
}
Expand Down Expand Up @@ -60,6 +61,16 @@ public void getId() throws ParsingException {
linkHandler.fromUrl("https://peertube.stream/c/[email protected]/video-playlists").getId());
assertEquals("video-channels/[email protected]",
linkHandler.fromUrl("https://peertube.stream/api/v1/video-channels/[email protected]").getId());

// instance URL ending with an "a": https://kolektiva.media
assertEquals("video-channels/documentary_channel",
linkHandler.fromUrl("https://kolektiva.media/video-channels/documentary_channel/videos").getId());
assertEquals("video-channels/documentary_channel",
linkHandler.fromUrl("https://kolektiva.media/c/documentary_channel/videos").getId());
assertEquals("video-channels/documentary_channel",
linkHandler.fromUrl("https://kolektiva.media/c/documentary_channel/video-playlists").getId());
assertEquals("video-channels/documentary_channel",
linkHandler.fromUrl("https://kolektiva.media/api/v1/video-channels/documentary_channel").getId());
}

@Test
Expand Down

0 comments on commit 743a400

Please sign in to comment.