Skip to content

Commit b425394

Browse files
authored
Merge pull request #731 from FireMasterK/short-description
Extract Video Short Description in YouTube.
2 parents 4e9d9bf + 6231396 commit b425394

8 files changed

Lines changed: 1340 additions & 0 deletions

File tree

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,19 @@ private OffsetDateTime getDateFromPremiere() throws ParsingException {
288288
throw new ParsingException("Could not parse date from premiere: \"" + startTime + "\"");
289289
}
290290
}
291+
292+
@Nullable
293+
@Override
294+
public String getShortDescription() throws ParsingException {
295+
296+
if (videoInfo.has("detailedMetadataSnippets")) {
297+
return getTextFromObject(videoInfo.getArray("detailedMetadataSnippets").getObject(0).getObject("snippetText"));
298+
}
299+
300+
if (videoInfo.has("descriptionSnippet")) {
301+
return getTextFromObject(videoInfo.getObject("descriptionSnippet"));
302+
}
303+
304+
return null;
305+
}
291306
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class StreamInfoItem extends InfoItem {
3232
private final StreamType streamType;
3333

3434
private String uploaderName;
35+
private String shortDescription;
3536
private String textualUploadDate;
3637
@Nullable
3738
private DateWrapper uploadDate;
@@ -92,6 +93,14 @@ public void setUploaderAvatarUrl(final String uploaderAvatarUrl) {
9293
this.uploaderAvatarUrl = uploaderAvatarUrl;
9394
}
9495

96+
public String getShortDescription() {
97+
return shortDescription;
98+
}
99+
100+
public void setShortDescription(final String shortDescription) {
101+
this.shortDescription = shortDescription;
102+
}
103+
95104
@Nullable
96105
public String getTextualUploadDate() {
97106
return textualUploadDate;

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,14 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor {
116116
@Nullable
117117
DateWrapper getUploadDate() throws ParsingException;
118118

119+
120+
/**
121+
* Get the video's short description.
122+
*
123+
* @return The video's short description or {@code null} if not provided by the service.
124+
* @throws ParsingException if there is an error in the extraction
125+
*/
126+
@Nullable
127+
default String getShortDescription() throws ParsingException { return null; }
128+
119129
}

extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemsCollector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws ParsingE
101101
} catch (Exception e) {
102102
addError(e);
103103
}
104+
try {
105+
resultItem.setShortDescription(extractor.getShortDescription());
106+
} catch (Exception e) {
107+
addError(e);
108+
}
104109

105110
return resultItem;
106111
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,34 @@ public void testUploaderAvatar() throws IOException, ExtractionException {
343343
}
344344
}
345345
}
346+
347+
public static class VideoDescription extends DefaultSearchExtractorTest {
348+
private static SearchExtractor extractor;
349+
private static final String QUERY = "44wLAzydRFU";
350+
351+
@BeforeClass
352+
public static void setUp() throws Exception {
353+
YoutubeParsingHelper.resetClientVersionAndKey();
354+
YoutubeParsingHelper.setNumberGenerator(new Random(1));
355+
NewPipe.init(new DownloaderFactory().getDownloader(RESOURCE_PATH + "video_description"));
356+
extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), "");
357+
extractor.fetchPage();
358+
}
359+
360+
@Override public SearchExtractor extractor() { return extractor; }
361+
@Override public StreamingService expectedService() { return YouTube; }
362+
@Override public String expectedName() { return QUERY; }
363+
@Override public String expectedId() { return QUERY; }
364+
@Override public String expectedUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
365+
@Override public String expectedOriginalUrlContains() { return "youtube.com/results?search_query=" + QUERY; }
366+
@Override public String expectedSearchString() { return QUERY; }
367+
@Nullable @Override public String expectedSearchSuggestion() { return null; }
368+
@Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; }
369+
370+
@Test
371+
public void testVideoDescription() throws IOException, ExtractionException {
372+
final List<InfoItem> items = extractor.getInitialPage().getItems();
373+
assertNotNull(((StreamInfoItem) items.get(0)).getShortDescription());
374+
}
375+
}
346376
}

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/search/video_description/generated_mock_0.json

Lines changed: 226 additions & 0 deletions
Large diffs are not rendered by default.

extractor/src/test/resources/org/schabi/newpipe/extractor/services/youtube/extractor/search/video_description/generated_mock_1.json

Lines changed: 259 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)