simplify youtube channels url detection

This commit is contained in:
Athou
2025-01-23 21:49:52 +01:00
parent dae5efa787
commit fde8dab8cd
2 changed files with 30 additions and 5 deletions

View File

@@ -1,7 +1,6 @@
package com.commafeed.backend.urlprovider;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import jakarta.inject.Singleton;
@@ -14,12 +13,16 @@ import jakarta.inject.Singleton;
@Singleton
public class YoutubeFeedURLProvider implements FeedURLProvider {
private static final Pattern REGEXP = Pattern.compile("(.*\\byoutube\\.com)\\/channel\\/([^\\/]+)", Pattern.CASE_INSENSITIVE);
private static final String PREFIX = "https://www.youtube.com/channel/";
private static final String REPLACEMENT_PREFIX = "https://www.youtube.com/feeds/videos.xml?channel_id=";
@Override
public String get(String url, String urlContent) {
Matcher matcher = REGEXP.matcher(url);
return matcher.find() ? matcher.group(1) + "/feeds/videos.xml?channel_id=" + matcher.group(2) : null;
if (!StringUtils.startsWithIgnoreCase(url, PREFIX)) {
return null;
}
return REPLACEMENT_PREFIX + url.substring(PREFIX.length());
}
}

View File

@@ -0,0 +1,22 @@
package com.commafeed.backend.urlprovider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class YoutubeFeedURLProviderTest {
private final YoutubeFeedURLProvider provider = new YoutubeFeedURLProvider();
@Test
void matchesYoutubeChannelURL() {
Assertions.assertEquals("https://www.youtube.com/feeds/videos.xml?channel_id=abc",
provider.get("https://www.youtube.com/channel/abc", null));
}
@Test
void doesNotmatchYoutubeChannelURL() {
Assertions.assertNull(provider.get("https://www.anothersite.com/channel/abc", null));
Assertions.assertNull(provider.get("https://www.youtube.com/user/abc", null));
}
}