Skip to content

Commit 0fff030

Browse files
committed
Get description with correct links
1 parent 5d08c34 commit 0fff030

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import java.io.IOException;
4040
import java.io.UnsupportedEncodingException;
41+
import java.nio.charset.StandardCharsets;
4142
import java.text.SimpleDateFormat;
4243
import java.util.*;
4344

@@ -164,22 +165,57 @@ public String getThumbnailUrl() throws ParsingException {
164165
@Override
165166
public Description getDescription() throws ParsingException {
166167
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
171170
try {
171+
boolean htmlConversionRequired = false;
172172
JsonArray descriptions = getVideoSecondaryInfoRenderer().getObject("description").getArray("runs");
173173
StringBuilder descriptionBuilder = new StringBuilder(descriptions.size());
174174
for (Object textObjectHolder : descriptions) {
175175
JsonObject textHolder = (JsonObject) textObjectHolder;
176176
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+
}
178199
}
200+
179201
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+
181211
} 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+
}
183219
}
184220

185221
@Override

0 commit comments

Comments
 (0)