b240d2a83eec58baef54dd3ac1a3aebc5b7140ab
[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     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()) {
59                 try {
60                     URL url = new URL(matcher.group(1));
61                     String text = matcher.group(2);
62                     result.add(
63                             new SnookerURL(text, url)
64                     );
65                 } catch (IOException ignored) {
66                 }
67             }
68         });
69         return result;
70     }
71
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);
76             if (matcher.find()) {
77                 try {
78                     URL url = new URL(matcher.group(1));
79                     result.add(
80                             new SnookerURL(name, url)
81                     );
82                 } catch (IOException ignored) {
83                 }
84             }
85         });
86         return result;
87     }
88
89     public static SnookerURL assignUrl(String name, String urlString) throws APIException {
90         SnookerURL result = null;
91         URL url = assignUrl(urlString);
92         if (url != null) {
93             result = new SnookerURL(name, url);
94         }
95         return result;
96     }
97
98     public static URL assignUrl(String urlString) throws APIException {
99         URL result = null;
100         if (urlString != null && !urlString.isEmpty()) {
101             Matcher matcher = HTTP_PATTERN.matcher(urlString);
102             if (matcher.find()) {
103                 try {
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()
108                     );
109                 }
110             }
111         }
112         return result;
113     }
114
115     public static String cutTags(String htmlString) {
116         if (htmlString != null && !htmlString.isEmpty()) {
117             return htmlString.replaceAll(
118                     BR_REGEX, CRLF
119             ).replaceAll(
120                     TAG_REGEX, ""
121             );
122         }
123         return htmlString;
124     }
125
126     public static BufferedImage loadImage(URL imageUrl) throws APIException {
127         BufferedImage result;
128         try {
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
133             );
134         }
135         return result;
136     }
137
138     public static String twitterUrl(String twitterName) {
139         return twitterName != null && !twitterName.isEmpty()
140                 ? TWITTER_URL.concat(twitterName)
141                 : null;
142     }
143
144     public static String hashtagUrl(String twitterHashtag) {
145         return twitterHashtag != null && !twitterHashtag.isEmpty()
146                 ? TWITTER_URL.concat(TWITTER_HASHTAG).concat(twitterHashtag)
147                 : null;
148     }
149
150 }