Skip to content

Commit ecfc27a

Browse files
committed
Implement fallback methods for getDescription() and getTextualUploadDate() in YouTubeStreamExtractor
1 parent eed29ea commit ecfc27a

1 file changed

Lines changed: 34 additions & 15 deletions

File tree

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

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@
3838

3939
import java.io.IOException;
4040
import java.io.UnsupportedEncodingException;
41-
import java.util.ArrayList;
42-
import java.util.Collections;
43-
import java.util.HashMap;
44-
import java.util.LinkedHashMap;
45-
import java.util.List;
46-
import java.util.Locale;
47-
import java.util.Map;
41+
import java.text.SimpleDateFormat;
42+
import java.util.*;
4843

4944
import javax.annotation.Nonnull;
5045
import javax.annotation.Nullable;
@@ -126,13 +121,28 @@ public String getTextualUploadDate() throws ParsingException {
126121
return null;
127122
}
128123

129-
// TODO: try videoPrimaryInfoRenderer.dateText.simpleText
124+
try {
125+
//return playerResponse.getObject("microformat").getObject("playerMicroformatRenderer").getString("publishDate");
126+
} catch (Exception ignored) {}
130127

131128
try {
132-
return playerResponse.getObject("microformat").getObject("playerMicroformatRenderer").getString("publishDate");
129+
JsonArray contents = initialData.getObject("contents").getObject("twoColumnWatchNextResults").getObject("results")
130+
.getObject("results").getArray("contents");
131+
for (Object c: contents) {
132+
String unformattedDate = "";
133+
try {
134+
JsonObject o = (JsonObject) c;
135+
unformattedDate = o.getObject("videoPrimaryInfoRenderer").getObject("dateText").getString("simpleText");
136+
137+
} catch (Exception ignored) {/* we got the wrong element form the array */}
138+
// TODO this parses English formatted dates only, we need a better approach to parse teh textual date
139+
Date d = new SimpleDateFormat("dd MMM yyy").parse(unformattedDate);
140+
return new SimpleDateFormat("yyyy-MM-dd").format(d);
141+
}
133142
} catch (Exception e) {
134-
throw new ParsingException("Could not get upload date");
143+
throw new ParsingException("Could not get upload date", e);
135144
}
145+
throw new ParsingException("Could not get upload date");
136146
}
137147

138148
@Override
@@ -165,13 +175,22 @@ public String getThumbnailUrl() throws ParsingException {
165175
@Override
166176
public Description getDescription() throws ParsingException {
167177
assertPageFetched();
168-
// TODO: Parse videoSecondaryInfoRenderer.description
178+
// raw non-html description
169179
try {
170-
// raw non-html description
171180
return new Description(playerResponse.getObject("videoDetails").getString("shortDescription"), Description.PLAIN_TEXT);
172-
} catch (Exception ignored) {
173-
throw new ParsingException("Could not get the description");
174-
}
181+
} catch (Exception ignored) { }
182+
try {
183+
JsonArray descriptions = getVideoSecondaryInfoRenderer().getObject("description").getArray("runs");
184+
StringBuilder descriptionBuilder = new StringBuilder(descriptions.size());
185+
for (Object textObjectHolder : descriptions) {
186+
JsonObject textHolder = (JsonObject) textObjectHolder;
187+
String text = textHolder.getString("text");
188+
if (text != null) descriptionBuilder.append(text);
189+
}
190+
String description = descriptionBuilder.toString();
191+
if (!description.isEmpty()) return new Description(description, Description.PLAIN_TEXT);
192+
} catch (Exception ignored) { }
193+
throw new ParsingException("Could not get description");
175194
}
176195

177196
@Override

0 commit comments

Comments
 (0)