|
| 1 | +/* |
| 2 | + * Copyright 2018 John Grosh (john.a.grosh@gmail.com). |
| 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 | +package com.jagrosh.jlyrics; |
| 17 | + |
| 18 | +import com.typesafe.config.Config; |
| 19 | +import com.typesafe.config.ConfigException; |
| 20 | +import com.typesafe.config.ConfigFactory; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.concurrent.Executor; |
| 25 | +import java.util.concurrent.Executors; |
| 26 | +import java.util.regex.Matcher; |
| 27 | +import java.util.regex.Pattern; |
| 28 | +import org.json.JSONException; |
| 29 | +import org.json.JSONObject; |
| 30 | +import org.json.XML; |
| 31 | +import org.jsoup.Connection; |
| 32 | +import org.jsoup.Jsoup; |
| 33 | +import org.jsoup.nodes.Document; |
| 34 | +import org.jsoup.nodes.Document.OutputSettings; |
| 35 | +import org.jsoup.nodes.Element; |
| 36 | +import org.jsoup.safety.Safelist; |
| 37 | + |
| 38 | +public class LyricsClient |
| 39 | +{ |
| 40 | + private final Config config = ConfigFactory.load(); |
| 41 | + private final HashMap<String, Lyrics> cache = new HashMap<>(); |
| 42 | + private final OutputSettings noPrettyPrint = new OutputSettings().prettyPrint(false); |
| 43 | + private final Safelist newlineSafelist = Safelist.none().addTags("br", "p"); |
| 44 | + private final Executor executor; |
| 45 | + private final String defaultSource; |
| 46 | + private final String userAgent; |
| 47 | + private final int timeout; |
| 48 | + |
| 49 | + public LyricsClient() |
| 50 | + { |
| 51 | + this(null, null); |
| 52 | + } |
| 53 | + |
| 54 | + public LyricsClient(String defaultSource) |
| 55 | + { |
| 56 | + this(defaultSource, null); |
| 57 | + } |
| 58 | + |
| 59 | + public LyricsClient(Executor executor) |
| 60 | + { |
| 61 | + this(null, executor); |
| 62 | + } |
| 63 | + |
| 64 | + public LyricsClient(String defaultSource, Executor executor) |
| 65 | + { |
| 66 | + this.defaultSource = defaultSource == null ? config.getString("lyrics.default") : defaultSource; |
| 67 | + this.userAgent = config.getString("lyrics.user-agent"); |
| 68 | + this.timeout = config.getInt("lyrics.timeout"); |
| 69 | + this.executor = executor == null ? Executors.newCachedThreadPool() : executor; |
| 70 | + } |
| 71 | + |
| 72 | + public CompletableFuture<Lyrics> getLyrics(String search) |
| 73 | + { |
| 74 | + return getLyrics(search, defaultSource); |
| 75 | + } |
| 76 | + |
| 77 | + public CompletableFuture<Lyrics> getLyrics(String search, String source) |
| 78 | + { |
| 79 | + String cacheKey = source + "||" + search; |
| 80 | + if(cache.containsKey(cacheKey)) |
| 81 | + return CompletableFuture.completedFuture(cache.get(cacheKey)); |
| 82 | + try |
| 83 | + { |
| 84 | + CompletableFuture<String> futureToken; |
| 85 | + boolean jsonSearch = config.getBoolean("lyrics." + source + ".search.json"); |
| 86 | + String select = config.getString("lyrics." + source + ".search.select"); |
| 87 | + String titleSelector = config.getString("lyrics." + source + ".parse.title"); |
| 88 | + String authorSelector = config.getString("lyrics." + source + ".parse.author"); |
| 89 | + String contentSelector = config.getString("lyrics." + source + ".parse.content"); |
| 90 | + |
| 91 | + if(config.hasPath("lyrics." + source + ".token")) |
| 92 | + futureToken = getToken(source); |
| 93 | + else |
| 94 | + futureToken = CompletableFuture.completedFuture(""); |
| 95 | + |
| 96 | + return futureToken.thenCompose(token -> { |
| 97 | + String searchUrl = String.format(config.getString("lyrics." + source + ".search.url"), search, token); |
| 98 | + |
| 99 | + return CompletableFuture.supplyAsync(() -> { |
| 100 | + try |
| 101 | + { |
| 102 | + Document doc; |
| 103 | + Connection connection = Jsoup.connect(searchUrl).userAgent(userAgent).timeout(timeout); |
| 104 | + if(jsonSearch) |
| 105 | + { |
| 106 | + String body = connection.ignoreContentType(true).execute().body(); |
| 107 | + JSONObject json = new JSONObject(body); |
| 108 | + doc = Jsoup.parse(XML.toString(json)); |
| 109 | + } |
| 110 | + else |
| 111 | + doc = connection.get(); |
| 112 | + |
| 113 | + Element urlElement = doc.selectFirst(select); |
| 114 | + String url; |
| 115 | + if(jsonSearch) |
| 116 | + url = urlElement.text(); |
| 117 | + else |
| 118 | + url = urlElement.attr("abs:href"); |
| 119 | + if(url == null || url.isEmpty()) |
| 120 | + return null; |
| 121 | + doc = Jsoup.connect(url).userAgent(userAgent).timeout(timeout).get(); |
| 122 | + Lyrics lyrics = new Lyrics( |
| 123 | + doc.selectFirst(titleSelector).ownText(), |
| 124 | + doc.selectFirst(authorSelector).ownText(), |
| 125 | + cleanWithNewlines(doc.selectFirst(contentSelector)), |
| 126 | + url, |
| 127 | + source |
| 128 | + ); |
| 129 | + cache.put(cacheKey, lyrics); |
| 130 | + return lyrics; |
| 131 | + } |
| 132 | + catch(IOException | NullPointerException | JSONException ex) |
| 133 | + { |
| 134 | + return null; |
| 135 | + } |
| 136 | + }, executor); |
| 137 | + }); |
| 138 | + } |
| 139 | + catch(ConfigException ex) |
| 140 | + { |
| 141 | + throw new IllegalArgumentException(String.format("Source '%s' does not exist or is not configured correctly", source)); |
| 142 | + } |
| 143 | + catch(Exception ignored) |
| 144 | + { |
| 145 | + return null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private CompletableFuture<String> getToken(String source) |
| 150 | + { |
| 151 | + try |
| 152 | + { |
| 153 | + String tokenUrl = config.getString("lyrics." + source + ".token.url"); |
| 154 | + String select = config.getString("lyrics." + source + ".token.select"); |
| 155 | + boolean textSearch = config.getBoolean("lyrics." + source + ".token.text"); |
| 156 | + |
| 157 | + return CompletableFuture.supplyAsync(() -> { |
| 158 | + try |
| 159 | + { |
| 160 | + Pattern pattern = null; |
| 161 | + |
| 162 | + if(config.hasPath("lyrics." + source + ".token.regex")) |
| 163 | + { |
| 164 | + String regexPattern = config.getString("lyrics." + source + ".token.regex"); |
| 165 | + pattern = Pattern.compile(regexPattern); |
| 166 | + } |
| 167 | + |
| 168 | + Connection connection = Jsoup.connect(tokenUrl).userAgent(userAgent).timeout(timeout); |
| 169 | + String body; |
| 170 | + |
| 171 | + if(textSearch) |
| 172 | + { |
| 173 | + body = connection.ignoreContentType(true).execute().body(); |
| 174 | + } |
| 175 | + else |
| 176 | + { |
| 177 | + Document doc = connection.get(); |
| 178 | + body = doc.selectFirst(select).ownText(); |
| 179 | + } |
| 180 | + |
| 181 | + if(pattern != null) |
| 182 | + { |
| 183 | + Matcher matcher = pattern.matcher(body); |
| 184 | + if(matcher.find()) |
| 185 | + return matcher.group(); |
| 186 | + } |
| 187 | + return null; |
| 188 | + } |
| 189 | + catch(IOException | NullPointerException ex) |
| 190 | + { |
| 191 | + return null; |
| 192 | + } |
| 193 | + }, executor); |
| 194 | + } |
| 195 | + catch(ConfigException ex) |
| 196 | + { |
| 197 | + throw new IllegalArgumentException(String.format("Source '%s' does not exist or is not configured correctly", source)); |
| 198 | + } |
| 199 | + catch(Exception ignored) |
| 200 | + { |
| 201 | + return null; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private String cleanWithNewlines(Element element) |
| 206 | + { |
| 207 | + return Jsoup.clean(Jsoup.clean(element.html(), newlineSafelist), "", Safelist.none(), noPrettyPrint); |
| 208 | + } |
| 209 | +} |
0 commit comments