|
38 | 38 |
|
39 | 39 | import java.io.IOException; |
40 | 40 | 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.*; |
48 | 43 |
|
49 | 44 | import javax.annotation.Nonnull; |
50 | 45 | import javax.annotation.Nullable; |
@@ -126,13 +121,28 @@ public String getTextualUploadDate() throws ParsingException { |
126 | 121 | return null; |
127 | 122 | } |
128 | 123 |
|
129 | | - // TODO: try videoPrimaryInfoRenderer.dateText.simpleText |
| 124 | + try { |
| 125 | + //return playerResponse.getObject("microformat").getObject("playerMicroformatRenderer").getString("publishDate"); |
| 126 | + } catch (Exception ignored) {} |
130 | 127 |
|
131 | 128 | 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 | + } |
133 | 142 | } catch (Exception e) { |
134 | | - throw new ParsingException("Could not get upload date"); |
| 143 | + throw new ParsingException("Could not get upload date", e); |
135 | 144 | } |
| 145 | + throw new ParsingException("Could not get upload date"); |
136 | 146 | } |
137 | 147 |
|
138 | 148 | @Override |
@@ -165,13 +175,22 @@ public String getThumbnailUrl() throws ParsingException { |
165 | 175 | @Override |
166 | 176 | public Description getDescription() throws ParsingException { |
167 | 177 | assertPageFetched(); |
168 | | - // TODO: Parse videoSecondaryInfoRenderer.description |
| 178 | + // raw non-html description |
169 | 179 | try { |
170 | | - // raw non-html description |
171 | 180 | 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"); |
175 | 194 | } |
176 | 195 |
|
177 | 196 | @Override |
|
0 commit comments