|
38 | 38 |
|
39 | 39 | import java.io.IOException; |
40 | 40 | import java.io.UnsupportedEncodingException; |
| 41 | +import java.nio.charset.StandardCharsets; |
41 | 42 | import java.text.SimpleDateFormat; |
42 | 43 | import java.util.*; |
43 | 44 |
|
@@ -164,22 +165,57 @@ public String getThumbnailUrl() throws ParsingException { |
164 | 165 | @Override |
165 | 166 | public Description getDescription() throws ParsingException { |
166 | 167 | assertPageFetched(); |
167 | | - // raw non-html description |
168 | | - try { |
169 | | - return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT); |
170 | | - } catch (Exception ignored) { } |
| 168 | + |
| 169 | + // description with more info on links |
171 | 170 | try { |
| 171 | + boolean htmlConversionRequired = false; |
172 | 172 | JsonArray descriptions = getVideoSecondaryInfoRenderer().getObject("description").getArray("runs"); |
173 | 173 | StringBuilder descriptionBuilder = new StringBuilder(descriptions.size()); |
174 | 174 | for (Object textObjectHolder : descriptions) { |
175 | 175 | JsonObject textHolder = (JsonObject) textObjectHolder; |
176 | 176 | String text = textHolder.getString("text"); |
177 | | - if (text != null) descriptionBuilder.append(text); |
| 177 | + if (textHolder.getObject("navigationEndpoint") != null) { |
| 178 | + // The text is a link. Get the URL it points to and generate a HTML link of it |
| 179 | + String internUrl = textHolder.getObject("navigationEndpoint").getObject("urlEndpoint").getString("url"); |
| 180 | + if (internUrl.startsWith("/redirect?")) { |
| 181 | + // q parameter can be the first parameter |
| 182 | + internUrl = internUrl.substring(10); |
| 183 | + } |
| 184 | + String[] params = internUrl.split("&"); |
| 185 | + for (String param : params) { |
| 186 | + if (param.charAt(0) == 'q') { |
| 187 | + String url = java.net.URLDecoder.decode(param.substring(2), StandardCharsets.UTF_8.name()); |
| 188 | + if (url != null && !url.isEmpty()) { |
| 189 | + descriptionBuilder.append("<a href=\"").append(url).append("\">").append(text).append("</a>"); |
| 190 | + htmlConversionRequired = true; |
| 191 | + } else { |
| 192 | + descriptionBuilder.append(text); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } else if (text != null) { |
| 197 | + descriptionBuilder.append(text); |
| 198 | + } |
178 | 199 | } |
| 200 | + |
179 | 201 | String description = descriptionBuilder.toString(); |
180 | | - if (!description.isEmpty()) return new Description(description, Description.PLAIN_TEXT); |
| 202 | + |
| 203 | + if (!description.isEmpty()) { |
| 204 | + if (htmlConversionRequired) { |
| 205 | + description = description.replaceAll("\\n", "<br>"); |
| 206 | + return new Description(description, Description.HTML); |
| 207 | + } |
| 208 | + return new Description(description, Description.PLAIN_TEXT); |
| 209 | + } |
| 210 | + |
181 | 211 | } catch (Exception ignored) { } |
182 | | - throw new ParsingException("Could not get description"); |
| 212 | + |
| 213 | + // raw non-html description |
| 214 | + try { |
| 215 | + return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT); |
| 216 | + } catch (Exception ignored) { |
| 217 | + throw new ParsingException("Could not get description"); |
| 218 | + } |
183 | 219 | } |
184 | 220 |
|
185 | 221 | @Override |
|
0 commit comments