From f40fc0aa4fa5565d88de2e96207a79aeee987000 Mon Sep 17 00:00:00 2001 From: AudricV <74829229+AudricV@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:07:37 +0200 Subject: [PATCH] [YouTube] Add support for new crisis meta info action data The new action data can return multiple contact actions instead of only one, which will be concatenated by a new line return. This commit fixes tests of the CrisisResources test class of YoutubeSearchExtractorTest. --- .../youtube/YoutubeMetaInfoHelper.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMetaInfoHelper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMetaInfoHelper.java index c8410d057..48587e0cb 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMetaInfoHelper.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeMetaInfoHelper.java @@ -171,7 +171,31 @@ public final class YoutubeMetaInfoHelper { // usually an encouragement like "We are with you" final String title = getTextFromObjectOrThrow(r.getObject("title"), "title"); // usually a phone number - final String action = getTextFromObjectOrThrow(r.getObject("actionText"), "action"); + final String action; + if (r.has("actionText")) { + action = getTextFromObjectOrThrow(r.getObject("actionText"), "action"); + } else if (r.has("contacts")) { + final JsonArray contacts = r.getArray("contacts"); + final StringBuilder stringBuilder = new StringBuilder(); + int i = 0; + final int contactsSize = contacts.size(); + if (contactsSize != 0) { + // Loop over contacts item from the first contact to the last one, if there is + // not only one, in order to not add an unneeded line return + for (; i < contactsSize - 1; i++) { + stringBuilder.append(getTextFromObjectOrThrow(contacts.getObject(i) + .getObject("actionText"), "contacts.actionText")); + stringBuilder.append("\n"); + } + // Add the latest contact without an extra line return + stringBuilder.append(getTextFromObjectOrThrow(contacts.getObject(i) + .getObject("actionText"), "contacts.actionText")); + } + + action = stringBuilder.toString(); + } else { + action = null; + } // usually details about the phone number final String details = getTextFromObjectOrThrow(r.getObject("detailsText"), "details"); // usually the name of an association @@ -179,7 +203,8 @@ public final class YoutubeMetaInfoHelper { "urlText"); metaInfo.setTitle(title); - metaInfo.setContent(new Description(details + "\n" + action, Description.PLAIN_TEXT)); + metaInfo.setContent(new Description(isNullOrEmpty(action) + ? details : details + "\n" + action, Description.PLAIN_TEXT)); metaInfo.addUrlText(urlText); // usually the webpage of the association