2 * Copyright (c) 2017-2019. Developed by Hedgecode.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.hedgecode.snooker;
19 import java.awt.image.BufferedImage;
20 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 import javax.imageio.ImageIO;
30 import org.hedgecode.snooker.api.APIException;
31 import org.hedgecode.snooker.api.SnookerURL;
34 * Utils for working with URLs.
36 * @author Dmitry Samoshin aka gotty
38 public final class SnookerURLUtils {
40 private static final String CRLF = System.lineSeparator();
42 private static final String HTTP_REGEX = "(http[s]?://.+)";
43 private static final String ANCHOR_REGEX = "<a href=\"([^\"]+)\"[^>]*>([^<]+)</a>";
45 private static final Pattern HTTP_PATTERN = Pattern.compile(HTTP_REGEX);
46 private static final Pattern ANCHOR_PATTERN = Pattern.compile(ANCHOR_REGEX);
48 private static final String BR_REGEX = "<[Bb][Rr][ /]*>";
49 private static final String TAG_REGEX = "<[^>]+>";
51 private static final String TWITTER_URL = "https://twitter.com/";
52 private static final String TWITTER_HASHTAG = "hashtag/";
54 public static List<SnookerURL> parseUrls(Map<String, String> htmlStrings) throws APIException {
55 List<SnookerURL> result = new ArrayList<>();
56 htmlStrings.forEach( (name, htmlString) -> {
57 Matcher matcher = ANCHOR_PATTERN.matcher(htmlString);
58 while (matcher.find()) {
60 URL url = new URL(matcher.group(1));
61 String text = matcher.group(2);
63 new SnookerURL(text, url)
65 } catch (IOException ignored) {
72 public static List<SnookerURL> assignUrls(Map<String, String> urlStrings) throws APIException {
73 List<SnookerURL> result = new ArrayList<>();
74 urlStrings.forEach( (name, urlString) -> {
75 Matcher matcher = HTTP_PATTERN.matcher(urlString);
78 URL url = new URL(matcher.group(1));
80 new SnookerURL(name, url)
82 } catch (IOException ignored) {
89 public static SnookerURL assignUrl(String name, String urlString) throws APIException {
90 SnookerURL result = null;
91 URL url = assignUrl(urlString);
93 result = new SnookerURL(name, url);
98 public static URL assignUrl(String urlString) throws APIException {
100 if (urlString != null && !urlString.isEmpty()) {
101 Matcher matcher = HTTP_PATTERN.matcher(urlString);
102 if (matcher.find()) {
104 result = new URL(matcher.group(1));
105 } catch (IOException e) {
106 throw new APIException(
107 APIException.Type.INFO, "Failed to recognize URL: " + e.getMessage()
115 public static String cutTags(String htmlString) {
116 if (htmlString != null && !htmlString.isEmpty()) {
117 return htmlString.replaceAll(
126 public static BufferedImage loadImage(URL imageUrl) throws APIException {
127 BufferedImage result;
129 result = ImageIO.read(imageUrl);
130 } catch (IOException e) {
131 throw new APIException(
132 APIException.Type.INFO, "Failed to load image at the address: " + imageUrl
138 public static String twitterUrl(String twitterName) {
139 return twitterName != null && !twitterName.isEmpty()
140 ? TWITTER_URL.concat(twitterName)
144 public static String hashtagUrl(String twitterHashtag) {
145 return twitterHashtag != null && !twitterHashtag.isEmpty()
146 ? TWITTER_URL.concat(TWITTER_HASHTAG).concat(twitterHashtag)