|
3 | 3 | import com.grack.nanojson.JsonArray; |
4 | 4 | import com.grack.nanojson.JsonObject; |
5 | 5 | import com.grack.nanojson.JsonParser; |
6 | | - |
7 | 6 | import org.mozilla.javascript.Context; |
8 | 7 | import org.mozilla.javascript.Function; |
9 | 8 | import org.mozilla.javascript.ScriptableObject; |
|
36 | 35 | import org.schabi.newpipe.extractor.utils.Parser; |
37 | 36 | import org.schabi.newpipe.extractor.utils.Utils; |
38 | 37 |
|
| 38 | +import javax.annotation.Nonnull; |
| 39 | +import javax.annotation.Nullable; |
39 | 40 | import java.io.IOException; |
40 | 41 | import java.io.UnsupportedEncodingException; |
41 | 42 | import java.text.SimpleDateFormat; |
|
49 | 50 | import java.util.Locale; |
50 | 51 | import java.util.Map; |
51 | 52 |
|
52 | | -import javax.annotation.Nonnull; |
53 | | -import javax.annotation.Nullable; |
54 | | - |
55 | 53 | import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.fixThumbnailUrl; |
56 | 54 | import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getJsonResponse; |
57 | 55 | import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject; |
@@ -102,6 +100,7 @@ public class DecryptException extends ParsingException { |
102 | 100 | private JsonObject videoPrimaryInfoRenderer; |
103 | 101 | private JsonObject videoSecondaryInfoRenderer; |
104 | 102 | private int ageLimit; |
| 103 | + private boolean newJsonScheme; |
105 | 104 |
|
106 | 105 | @Nonnull |
107 | 106 | private List<SubtitlesInfo> subtitlesInfos = new ArrayList<>(); |
@@ -156,20 +155,23 @@ public String getTextualUploadDate() throws ParsingException { |
156 | 155 | TimeAgoParser timeAgoParser = TimeAgoPatternsManager.getTimeAgoParserFor(Localization.fromLocalizationCode("en")); |
157 | 156 | Calendar parsedTime = timeAgoParser.parse(time).date(); |
158 | 157 | return new SimpleDateFormat("yyyy-MM-dd").format(parsedTime.getTime()); |
159 | | - } catch (Exception ignored) {} |
| 158 | + } catch (Exception ignored) { |
| 159 | + } |
160 | 160 |
|
161 | 161 | try { // Premiered Feb 21, 2020 |
162 | 162 | Date d = new SimpleDateFormat("MMM dd, YYYY", Locale.ENGLISH).parse(time); |
163 | 163 | return new SimpleDateFormat("yyyy-MM-dd").format(d.getTime()); |
164 | | - } catch (Exception ignored) {} |
| 164 | + } catch (Exception ignored) { |
| 165 | + } |
165 | 166 | } |
166 | 167 |
|
167 | 168 | try { |
168 | 169 | // TODO: this parses English formatted dates only, we need a better approach to parse the textual date |
169 | 170 | Date d = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH).parse( |
170 | 171 | getTextFromObject(getVideoPrimaryInfoRenderer().getObject("dateText"))); |
171 | 172 | return new SimpleDateFormat("yyyy-MM-dd").format(d); |
172 | | - } catch (Exception ignored) {} |
| 173 | + } catch (Exception ignored) { |
| 174 | + } |
173 | 175 | throw new ParsingException("Could not get upload date"); |
174 | 176 | } |
175 | 177 |
|
@@ -360,7 +362,8 @@ public String getUploaderName() throws ParsingException { |
360 | 362 | try { |
361 | 363 | uploaderName = getTextFromObject(getVideoSecondaryInfoRenderer().getObject("owner") |
362 | 364 | .getObject("videoOwnerRenderer").getObject("title")); |
363 | | - } catch (ParsingException ignored) { } |
| 365 | + } catch (ParsingException ignored) { |
| 366 | + } |
364 | 367 |
|
365 | 368 | if (isNullOrEmpty(uploaderName)) { |
366 | 369 | uploaderName = playerResponse.getObject("videoDetails").getString("author"); |
@@ -650,27 +653,23 @@ public void onFetchPage(@Nonnull Downloader downloader) throws IOException, Extr |
650 | 653 | } else { |
651 | 654 | ageLimit = NO_AGE_LIMIT; |
652 | 655 | JsonObject playerConfig; |
| 656 | + initialData = initialAjaxJson.getObject(3).getObject("response"); |
653 | 657 |
|
654 | | - // sometimes at random YouTube does not provide the player config, |
655 | | - // so just retry the same request three times |
656 | | - int attempts = 2; |
657 | | - while (true) { |
658 | | - playerConfig = initialAjaxJson.getObject(2).getObject("player", null); |
659 | | - if (playerConfig != null) { |
660 | | - break; |
661 | | - } |
| 658 | + // sometimes at random YouTube does not provide the player config |
| 659 | + playerConfig = initialAjaxJson.getObject(2).getObject("player", null); |
662 | 660 |
|
663 | | - if (attempts <= 0) { |
664 | | - throw new ParsingException( |
665 | | - "YouTube did not provide player config even after three attempts"); |
666 | | - } |
667 | | - initialAjaxJson = getJsonResponse(url, getExtractorLocalization()); |
668 | | - --attempts; |
| 661 | + if (playerConfig == null) { |
| 662 | + newJsonScheme = true; |
| 663 | + final EmbeddedInfo info = getEmbeddedInfo(); |
| 664 | + final String videoInfoUrl = getVideoInfoUrl(getId(), info.sts); |
| 665 | + final String infoPageResponse = downloader.get(videoInfoUrl, getExtractorLocalization()).responseBody(); |
| 666 | + videoInfoPage.putAll(Parser.compatParseMap(infoPageResponse)); |
| 667 | + playerUrl = info.url; |
| 668 | + } else { |
| 669 | + playerArgs = getPlayerArgs(playerConfig); |
| 670 | + playerUrl = getPlayerUrl(playerConfig); |
669 | 671 | } |
670 | | - initialData = initialAjaxJson.getObject(3).getObject("response"); |
671 | 672 |
|
672 | | - playerArgs = getPlayerArgs(playerConfig); |
673 | | - playerUrl = getPlayerUrl(playerConfig); |
674 | 673 | } |
675 | 674 |
|
676 | 675 | playerResponse = getPlayerResponse(); |
@@ -718,6 +717,10 @@ private String getPlayerUrl(final JsonObject playerConfig) throws ParsingExcepti |
718 | 717 | private JsonObject getPlayerResponse() throws ParsingException { |
719 | 718 | try { |
720 | 719 | String playerResponseStr; |
| 720 | + if (newJsonScheme) { |
| 721 | + return initialAjaxJson.getObject(2).getObject("playerResponse"); |
| 722 | + } |
| 723 | + |
721 | 724 | if (playerArgs != null) { |
722 | 725 | playerResponseStr = playerArgs.getString("player_response"); |
723 | 726 | } else { |
@@ -988,7 +991,8 @@ private Map<String, ItagItem> getItags(String streamingDataKey, ItagItem.ItagTyp |
988 | 991 |
|
989 | 992 | urlAndItags.put(streamUrl, itagItem); |
990 | 993 | } |
991 | | - } catch (UnsupportedEncodingException ignored) {} |
| 994 | + } catch (UnsupportedEncodingException ignored) { |
| 995 | + } |
992 | 996 | } |
993 | 997 | } |
994 | 998 |
|
|
0 commit comments