Get description with correct links

This commit is contained in:
TobiGr 2020-02-25 18:27:39 +01:00
parent 5d08c34322
commit 0fff03038e
1 changed files with 43 additions and 7 deletions

View File

@ -38,6 +38,7 @@ import org.schabi.newpipe.extractor.utils.Utils;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -164,23 +165,58 @@ public class YoutubeStreamExtractor extends StreamExtractor {
@Override @Override
public Description getDescription() throws ParsingException { public Description getDescription() throws ParsingException {
assertPageFetched(); assertPageFetched();
// raw non-html description
try { // description with more info on links
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
} catch (Exception ignored) { }
try { try {
boolean htmlConversionRequired = false;
JsonArray descriptions = getVideoSecondaryInfoRenderer().getObject("description").getArray("runs"); JsonArray descriptions = getVideoSecondaryInfoRenderer().getObject("description").getArray("runs");
StringBuilder descriptionBuilder = new StringBuilder(descriptions.size()); StringBuilder descriptionBuilder = new StringBuilder(descriptions.size());
for (Object textObjectHolder : descriptions) { for (Object textObjectHolder : descriptions) {
JsonObject textHolder = (JsonObject) textObjectHolder; JsonObject textHolder = (JsonObject) textObjectHolder;
String text = textHolder.getString("text"); String text = textHolder.getString("text");
if (text != null) descriptionBuilder.append(text); if (textHolder.getObject("navigationEndpoint") != null) {
// The text is a link. Get the URL it points to and generate a HTML link of it
String internUrl = textHolder.getObject("navigationEndpoint").getObject("urlEndpoint").getString("url");
if (internUrl.startsWith("/redirect?")) {
// q parameter can be the first parameter
internUrl = internUrl.substring(10);
} }
String[] params = internUrl.split("&");
for (String param : params) {
if (param.charAt(0) == 'q') {
String url = java.net.URLDecoder.decode(param.substring(2), StandardCharsets.UTF_8.name());
if (url != null && !url.isEmpty()) {
descriptionBuilder.append("<a href=\"").append(url).append("\">").append(text).append("</a>");
htmlConversionRequired = true;
} else {
descriptionBuilder.append(text);
}
}
}
} else if (text != null) {
descriptionBuilder.append(text);
}
}
String description = descriptionBuilder.toString(); String description = descriptionBuilder.toString();
if (!description.isEmpty()) return new Description(description, Description.PLAIN_TEXT);
if (!description.isEmpty()) {
if (htmlConversionRequired) {
description = description.replaceAll("\\n", "<br>");
return new Description(description, Description.HTML);
}
return new Description(description, Description.PLAIN_TEXT);
}
} catch (Exception ignored) { } } catch (Exception ignored) { }
// raw non-html description
try {
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
} catch (Exception ignored) {
throw new ParsingException("Could not get description"); throw new ParsingException("Could not get description");
} }
}
@Override @Override
public int getAgeLimit() throws ParsingException { public int getAgeLimit() throws ParsingException {