Skip to content

Commit 4c183af

Browse files
[SoundCloud] Use Pattern instead of string for regex in SoundcloudStreamLinkHandlerFactory
Use non-capturing groups in regex Refactor Parser.java Add more utility methods (will be used in later commits)
1 parent 6e9e09d commit 4c183af

3 files changed

Lines changed: 102 additions & 32 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/linkHandler/SoundcloudStreamLinkHandlerFactory.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.schabi.newpipe.extractor.services.soundcloud.linkHandler;
22

3+
import java.util.regex.Pattern;
4+
35
import org.schabi.newpipe.extractor.exceptions.ParsingException;
46
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
57
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper;
@@ -9,11 +11,18 @@
911
public final class SoundcloudStreamLinkHandlerFactory extends LinkHandlerFactory {
1012
private static final SoundcloudStreamLinkHandlerFactory INSTANCE
1113
= new SoundcloudStreamLinkHandlerFactory();
12-
private static final String URL_PATTERN = "^https?://(www\\.|m\\.|on\\.)?"
13-
+ "soundcloud.com/[0-9a-z_-]+"
14-
+ "/(?!(tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?([#?].*)?$";
15-
private static final String API_URL_PATTERN = "^https?://api-v2\\.soundcloud.com"
16-
+ "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/";
14+
15+
private static final Pattern URL_PATTERN = Pattern.compile(
16+
"^https?://(?:www\\.|m\\.|on\\.)?"
17+
+ "soundcloud.com/[0-9a-z_-]+"
18+
+ "/(?!(?:tracks|albums|sets|reposts|followers|following)/?$)[0-9a-z_-]+/?(?:[#?].*)?$"
19+
);
20+
21+
private static final Pattern API_URL_PATTERN = Pattern.compile(
22+
"^https?://api-v2\\.soundcloud.com"
23+
+ "/(tracks|albums|sets|reposts|followers|following)/([0-9a-z_-]+)/"
24+
);
25+
1726
private SoundcloudStreamLinkHandlerFactory() {
1827
}
1928

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Parser.java

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,50 +44,104 @@ public RegexException(final String message) {
4444
}
4545
}
4646

47+
@Nonnull
48+
public static Matcher matchOrThrow(@Nonnull final Pattern pattern,
49+
final String input) throws RegexException {
50+
final Matcher matcher = pattern.matcher(input);
51+
if (matcher.find()) {
52+
return matcher;
53+
} else {
54+
String errorMessage = "Failed to find pattern \"" + pattern.pattern() + "\"";
55+
if (input.length() <= 1024) {
56+
errorMessage += " inside of \"" + input + "\"";
57+
}
58+
throw new RegexException(errorMessage);
59+
}
60+
}
61+
62+
@Nonnull
63+
public static String matchNamedGroup(final String pattern,
64+
final String input,
65+
final String groupName) throws RegexException {
66+
return matchNamedGroup(Pattern.compile(pattern), input, groupName);
67+
}
68+
69+
@Nonnull
70+
public static String matchNamedGroup(@Nonnull final Pattern pattern,
71+
final String input,
72+
@Nonnull final String groupName) throws RegexException {
73+
return matchOrThrow(pattern, input).group(groupName);
74+
}
75+
76+
public static int getStartIndexOfNamedGroup(@Nonnull final Pattern pattern,
77+
final String input,
78+
@Nonnull final String groupName)
79+
throws RegexException {
80+
return matchOrThrow(pattern, input).start(groupName);
81+
}
82+
83+
public static int getEndIndexOfNamedGroup(@Nonnull final Pattern pattern,
84+
final String input,
85+
@Nonnull final String groupName)
86+
throws RegexException {
87+
return matchOrThrow(pattern, input).end(groupName);
88+
}
89+
90+
@Nonnull
4791
public static String matchGroup1(final String pattern, final String input)
4892
throws RegexException {
4993
return matchGroup(pattern, input, 1);
5094
}
5195

96+
97+
@Nonnull
5298
public static String matchGroup1(final Pattern pattern,
53-
final String input) throws RegexException {
99+
final String input) throws RegexException {
54100
return matchGroup(pattern, input, 1);
55101
}
56-
102+
103+
/**
104+
* Matches the specified group of the given pattern against the input.
105+
*
106+
* @param pattern The regex pattern to match.
107+
* @param input The input string to match against.
108+
* @param group The group number to retrieve (1-based index).
109+
* @return The matching group as a string.
110+
* @throws RegexException If the pattern does not match the input or if the group is not found.
111+
*/
112+
@Nonnull
57113
public static String matchGroup(final String pattern,
58-
final String input,
59-
final int group) throws RegexException {
114+
final String input,
115+
final int group) throws RegexException {
60116
return matchGroup(Pattern.compile(pattern), input, group);
61117
}
62-
63-
public static String matchGroup(@Nonnull final Pattern pat,
118+
119+
@Nonnull
120+
public static String matchGroup(@Nonnull final Pattern pattern,
64121
final String input,
65122
final int group) throws RegexException {
66-
final Matcher matcher = pat.matcher(input);
67-
final boolean foundMatch = matcher.find();
68-
if (foundMatch) {
69-
return matcher.group(group);
70-
} else {
71-
// only pass input to exception message when it is not too long
72-
if (input.length() > 1024) {
73-
throw new RegexException("Failed to find pattern \"" + pat.pattern() + "\"");
74-
} else {
75-
throw new RegexException("Failed to find pattern \"" + pat.pattern()
76-
+ "\" inside of \"" + input + "\"");
77-
}
78-
}
123+
return matchOrThrow(pattern, input).group(group);
79124
}
80125

81126
public static String matchGroup1MultiplePatterns(final Pattern[] patterns, final String input)
82127
throws RegexException {
83128
return matchMultiplePatterns(patterns, input).group(1);
84129
}
85130

131+
/**
132+
* Matches multiple patterns against the input string and
133+
* returns the first successful matcher
134+
*
135+
* @param patterns The array of regex patterns to match.
136+
* @param input The input string to match against.
137+
* @return A {@code Matcher} for the first successful match.
138+
* @throws RegexException If no patterns match the input or if {@code patterns} is empty.
139+
*/
86140
public static Matcher matchMultiplePatterns(final Pattern[] patterns, final String input)
87141
throws RegexException {
88-
Parser.RegexException exception = null;
89-
for (final Pattern pattern : patterns) {
90-
final Matcher matcher = pattern.matcher(input);
142+
RegexException exception = null;
143+
for (final var pattern : patterns) {
144+
final var matcher = pattern.matcher(input);
91145
if (matcher.find()) {
92146
return matcher;
93147
} else if (exception == null) {
@@ -110,14 +164,11 @@ public static Matcher matchMultiplePatterns(final Pattern[] patterns, final Stri
110164
}
111165

112166
public static boolean isMatch(final String pattern, final String input) {
113-
final Pattern pat = Pattern.compile(pattern);
114-
final Matcher mat = pat.matcher(input);
115-
return mat.find();
167+
return isMatch(Pattern.compile(pattern), input);
116168
}
117169

118170
public static boolean isMatch(@Nonnull final Pattern pattern, final String input) {
119-
final Matcher mat = pattern.matcher(input);
120-
return mat.find();
171+
return pattern.matcher(input).find();
121172
}
122173

123174
@Nonnull

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ public static long mixedNumberWordToLong(final String numberWord)
110110
* @param url the url to be tested
111111
*/
112112
public static void checkUrl(final String pattern, final String url) throws ParsingException {
113+
checkUrl(Pattern.compile(pattern), url);
114+
}
115+
116+
/**
117+
* Check if the url matches the pattern.
118+
*
119+
* @param pattern the pattern that will be used to check the url
120+
* @param url the url to be tested
121+
*/
122+
public static void checkUrl(final Pattern pattern, final String url) throws ParsingException {
113123
if (isNullOrEmpty(url)) {
114124
throw new IllegalArgumentException("Url can't be null or empty");
115125
}

0 commit comments

Comments
 (0)