mirror of https://github.com/ThmmyNoLife/mTHMMY
Thodoris Tyrovouzis
5 years ago
11 changed files with 90 additions and 33 deletions
@ -0,0 +1,30 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
import java.util.regex.Matcher; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
public class StringUtils { |
||||
|
/** |
||||
|
* Method that extracts the base URL from a topic's page URL. For example a topic with url similar to |
||||
|
* "https://www.thmmy.gr/smf/index.php?topic=1.15;topicseen" or |
||||
|
* "https://www.thmmy.gr/smf/index.php?topic=1.msg1#msg1" |
||||
|
* has the base url "https://www.thmmy.gr/smf/index.php?topic=1" |
||||
|
* |
||||
|
* @param topicURL a topic's page URL |
||||
|
* @return the base URL of the given topic |
||||
|
*/ |
||||
|
public static String getBaseURL(String topicURL) { |
||||
|
String forumUrl = "https://www.thmmy.gr/smf/index.php?"; |
||||
|
Matcher baseUrlMatcher = Pattern.compile("topic=[0-9]+").matcher(topicURL); |
||||
|
if (baseUrlMatcher.find()) |
||||
|
return forumUrl + topicURL.substring(baseUrlMatcher.start(), baseUrlMatcher.end()); |
||||
|
else return ""; |
||||
|
} |
||||
|
|
||||
|
public static int extractUserCodeFromUrl(String url) { |
||||
|
Matcher userCodeMatcher = Pattern.compile("u=[0-9]+").matcher(url); |
||||
|
if (userCodeMatcher.find()) |
||||
|
return Integer.parseInt(url.substring(userCodeMatcher.start()+2, userCodeMatcher.end())); |
||||
|
else return -1; |
||||
|
} |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
package gr.thmmy.mthmmy.utils.parsing; |
||||
|
|
||||
|
import org.junit.Before; |
||||
|
import org.junit.Test; |
||||
|
|
||||
|
import static org.junit.Assert.*; |
||||
|
|
||||
|
public class StringUtilsTest { |
||||
|
|
||||
|
@Before |
||||
|
public void setUp() throws Exception { |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void extractUserCodeFromUrl() { |
||||
|
String url = "https://www.thmmy.gr/smf/index.php?action=profile;u=14670"; |
||||
|
assertEquals(StringUtils.extractUserCodeFromUrl(url), 14670); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue