[LIB-10] WorldSnooker URL utils
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / SnookerURLUtils.java
1 /*
2  * Copyright (c) 2017-2020. Developed by Hedgecode.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.snooker;
18
19 import java.awt.image.BufferedImage;
20 import java.io.IOException;
21 import java.net.URL;
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 import javax.imageio.ImageIO;
29
30 import org.hedgecode.snooker.api.APIException;
31 import org.hedgecode.snooker.api.SnookerURL;
32
33 /**
34  * Utils for working with URLs.
35  *
36  * @author Dmitry Samoshin aka gotty
37  */
38 public final class SnookerURLUtils {
39
40     private static final String CRLF = System.lineSeparator();
41
42     private static final String HTTP_REGEX = "(http[s]?://.+)";
43     private static final String ANCHOR_REGEX = "<a href=\"([^\"]+)\"[^>]*>([^<]+)</a>";
44
45     private static final Pattern HTTP_PATTERN = Pattern.compile(HTTP_REGEX);
46     private static final Pattern ANCHOR_PATTERN = Pattern.compile(ANCHOR_REGEX);
47
48     private static final String BR_REGEX = "<[Bb][Rr][ /]*>";
49     private static final String TAG_REGEX = "<[^>]+>";
50
51     private static final String TWITTER_URL = "https://twitter.com/";
52     private static final String TWITTER_HASHTAG = "hashtag/";
53
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/";
57
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()) {
63                 try {
64                     URL url = new URL(matcher.group(1));
65                     String text = matcher.group(2);
66                     result.add(
67                             new SnookerURL(text, url)
68                     );
69                 } catch (IOException ignored) {
70                 }
71             }
72         });
73         return result;
74     }
75
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);
80             if (matcher.find()) {
81                 try {
82                     URL url = new URL(matcher.group(1));
83                     result.add(
84                             new SnookerURL(name, url)
85                     );
86                 } catch (IOException ignored) {
87                 }
88             }
89         });
90         return result;
91     }
92
93     public static SnookerURL assignUrl(String name, String urlString) throws APIException {
94         SnookerURL result = null;
95         URL url = assignUrl(urlString);
96         if (url != null) {
97             result = new SnookerURL(name, url);
98         }
99         return result;
100     }
101
102     public static URL assignUrl(String urlString) throws APIException {
103         URL result = null;
104         if (urlString != null && !urlString.isEmpty()) {
105             Matcher matcher = HTTP_PATTERN.matcher(urlString);
106             if (matcher.find()) {
107                 try {
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()
112                     );
113                 }
114             }
115         }
116         return result;
117     }
118
119     public static String cutTags(String htmlString) {
120         if (htmlString != null && !htmlString.isEmpty()) {
121             return htmlString.replaceAll(
122                     BR_REGEX, CRLF
123             ).replaceAll(
124                     TAG_REGEX, ""
125             );
126         }
127         return htmlString;
128     }
129
130     public static BufferedImage loadImage(URL imageUrl) throws APIException {
131         BufferedImage result;
132         try {
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
137             );
138         }
139         return result;
140     }
141
142     public static String twitterUrl(String twitterName) {
143         return twitterName != null && !twitterName.isEmpty()
144                 ? TWITTER_URL.concat(twitterName)
145                 : null;
146     }
147
148     public static String hashtagUrl(String twitterHashtag) {
149         return twitterHashtag != null && !twitterHashtag.isEmpty()
150                 ? TWITTER_URL.concat(TWITTER_HASHTAG).concat(twitterHashtag)
151                 : null;
152     }
153
154     public static String worldSnookerEventUrl(int wsEventId) {
155         return wsEventId > 0
156                 ? String.format("%s%s%d", WORLDSNOOKER_URL, WORLDSNOOKER_EVENT_MATCHES, wsEventId)
157                 : null;
158     }
159
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)
163                 : null;
164     }
165
166 }