2 * Copyright (c) 2017-2020. 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 private static final String WORLDSNOOKER_URL = "http://livescores.worldsnookerdata.com/";
55 private static final String WORLDSNOOKER_EVENT_MATCHES = "Matches/Index/";
56 private static final String WORLDSNOOKER_MATCH_RESULT = "Matches/Result/";
58 public static List<SnookerURL> parseUrls(Map<String, String> htmlStrings) throws APIException {
59 List<SnookerURL> result = new ArrayList<>();
60 htmlStrings.forEach( (name, htmlString) -> {
61 Matcher matcher = ANCHOR_PATTERN.matcher(htmlString);
62 while (matcher.find()) {
64 URL url = new URL(matcher.group(1));
65 String text = matcher.group(2);
67 new SnookerURL(text, url)
69 } catch (IOException ignored) {
76 public static List<SnookerURL> assignUrls(Map<String, String> urlStrings) throws APIException {
77 List<SnookerURL> result = new ArrayList<>();
78 urlStrings.forEach( (name, urlString) -> {
79 Matcher matcher = HTTP_PATTERN.matcher(urlString);
82 URL url = new URL(matcher.group(1));
84 new SnookerURL(name, url)
86 } catch (IOException ignored) {
93 public static SnookerURL assignUrl(String name, String urlString) throws APIException {
94 SnookerURL result = null;
95 URL url = assignUrl(urlString);
97 result = new SnookerURL(name, url);
102 public static URL assignUrl(String urlString) throws APIException {
104 if (urlString != null && !urlString.isEmpty()) {
105 Matcher matcher = HTTP_PATTERN.matcher(urlString);
106 if (matcher.find()) {
108 result = new URL(matcher.group(1));
109 } catch (IOException e) {
110 throw new APIException(
111 APIException.Type.INFO, "Failed to recognize URL: " + e.getMessage()
119 public static String cutTags(String htmlString) {
120 if (htmlString != null && !htmlString.isEmpty()) {
121 return htmlString.replaceAll(
130 public static BufferedImage loadImage(URL imageUrl) throws APIException {
131 BufferedImage result;
133 result = ImageIO.read(imageUrl);
134 } catch (IOException e) {
135 throw new APIException(
136 APIException.Type.INFO, "Failed to load image at the address: " + imageUrl
142 public static String twitterUrl(String twitterName) {
143 return twitterName != null && !twitterName.isEmpty()
144 ? TWITTER_URL.concat(twitterName)
148 public static String hashtagUrl(String twitterHashtag) {
149 return twitterHashtag != null && !twitterHashtag.isEmpty()
150 ? TWITTER_URL.concat(TWITTER_HASHTAG).concat(twitterHashtag)
154 public static String worldSnookerEventUrl(int wsEventId) {
156 ? String.format("%s%s%d", WORLDSNOOKER_URL, WORLDSNOOKER_EVENT_MATCHES, wsEventId)
160 public static String worldSnookerMatchUrl(int wsEventId, int wsMatchId) {
161 return wsEventId > 0 && wsMatchId > 0
162 ? String.format("%s%s%d/%d", WORLDSNOOKER_URL, WORLDSNOOKER_MATCH_RESULT, wsEventId, wsMatchId)