forked from Archives/Athou_commafeed
simplify youtube channels url detection
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package com.commafeed.backend.urlprovider;
|
package com.commafeed.backend.urlprovider;
|
||||||
|
|
||||||
import java.util.regex.Matcher;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import jakarta.inject.Singleton;
|
import jakarta.inject.Singleton;
|
||||||
|
|
||||||
@@ -14,12 +13,16 @@ import jakarta.inject.Singleton;
|
|||||||
@Singleton
|
@Singleton
|
||||||
public class YoutubeFeedURLProvider implements FeedURLProvider {
|
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
|
@Override
|
||||||
public String get(String url, String urlContent) {
|
public String get(String url, String urlContent) {
|
||||||
Matcher matcher = REGEXP.matcher(url);
|
if (!StringUtils.startsWithIgnoreCase(url, PREFIX)) {
|
||||||
return matcher.find() ? matcher.group(1) + "/feeds/videos.xml?channel_id=" + matcher.group(2) : null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return REPLACEMENT_PREFIX + url.substring(PREFIX.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user