Skip to content

Commit 99cddfb

Browse files
committed
More refactoring...
1 parent 2f1cb86 commit 99cddfb

2 files changed

Lines changed: 13 additions & 92 deletions

File tree

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

Lines changed: 8 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareAndroidMobileJsonBuilder;
3636
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareDesktopJsonBuilder;
3737
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.prepareIosMobileJsonBuilder;
38+
import static org.schabi.newpipe.extractor.utils.JsonUtils.getNullableInteger;
3839
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
3940
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
4041

@@ -1286,7 +1287,7 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
12861287
final ItagInfo<I> itagInfo = new ItagInfo<>(itagFormat, streamUrl);
12871288

12881289
if (itagFormat instanceof BaseAudioItagFormat) {
1289-
final Integer averageBitrate = getIntegerFromJson(formatData, "averageBitrate");
1290+
final Integer averageBitrate = getNullableInteger(formatData, "averageBitrate");
12901291
if (averageBitrate != null) {
12911292
itagInfo.setAverageBitrate((int) Math.round(averageBitrate / 1000d));
12921293
}
@@ -1297,15 +1298,15 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
12971298
} catch (final Exception ignore) {
12981299
// Ignore errors - leave default value
12991300
}
1300-
itagInfo.setAudioChannels(getIntegerFromJson(formatData, "audioChannels"));
1301+
itagInfo.setAudioChannels(getNullableInteger(formatData, "audioChannels"));
13011302
}
13021303
if (itagFormat instanceof VideoItagFormat) {
1303-
itagInfo.setHeight(getIntegerFromJson(formatData, "height"));
1304-
itagInfo.setWidth(getIntegerFromJson(formatData, "width"));
1305-
itagInfo.setFps(getIntegerFromJson(formatData, "fps"));
1304+
itagInfo.setHeight(getNullableInteger(formatData, "height"));
1305+
itagInfo.setWidth(getNullableInteger(formatData, "width"));
1306+
itagInfo.setFps(getNullableInteger(formatData, "fps"));
13061307
}
13071308

1308-
itagInfo.setBitRate(getIntegerFromJson(formatData, "bitRate"));
1309+
itagInfo.setBitRate(getNullableInteger(formatData, "bitRate"));
13091310
itagInfo.setQuality(formatData.getString("quality"));
13101311

13111312
final String mimeType = formatData.getString("mimeType", "");
@@ -1331,96 +1332,11 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
13311332
}
13321333

13331334
itagInfo.setType(formatData.getString("type"));
1334-
itagInfo.setTargetDurationSec(getIntegerFromJson(formatData, "targetDurationSec"));
1335+
itagInfo.setTargetDurationSec(getNullableInteger(formatData, "targetDurationSec"));
13351336

13361337
return itagInfo;
13371338
}
13381339

1339-
// TODO
1340-
private static Integer getIntegerFromJson(final JsonObject jsonObject, final String key) {
1341-
return (Integer) jsonObject.getNumber(key);
1342-
}
1343-
1344-
// private ItagInfo buildItagInfo(
1345-
// @Nonnull final String videoId,
1346-
// @Nonnull final JsonObject formatData,
1347-
// @Nonnull final ItagItem itagItem,
1348-
// @Nonnull final ItagItem.ItagType itagType,
1349-
// @Nonnull final String contentPlaybackNonce) throws IOException, ExtractionException {
1350-
// String streamUrl;
1351-
// if (formatData.has("url")) {
1352-
// streamUrl = formatData.getString("url");
1353-
// } else {
1354-
// // This url has an obfuscated signature
1355-
// final String cipherString = formatData.has(CIPHER)
1356-
// ? formatData.getString(CIPHER)
1357-
// : formatData.getString(SIGNATURE_CIPHER);
1358-
// final Map<String, String> cipher = Parser.compatParseMap(cipherString);
1359-
// streamUrl = cipher.get("url") + "&" + cipher.get("sp") + "="
1360-
// + deobfuscateSignature(cipher.get("s"));
1361-
// }
1362-
//
1363-
// // Add the content playback nonce to the stream URL
1364-
// streamUrl += "&" + CPN + "=" + contentPlaybackNonce;
1365-
//
1366-
// // Decrypt the n parameter if it is present
1367-
// streamUrl = tryDecryptUrl(streamUrl, videoId);
1368-
//
1369-
// final JsonObject initRange = formatData.getObject("initRange");
1370-
// final JsonObject indexRange = formatData.getObject("indexRange");
1371-
// final String mimeType = formatData.getString("mimeType", EMPTY_STRING);
1372-
// final String codec = mimeType.contains("codecs")
1373-
// ? mimeType.split("\"")[1]
1374-
// : EMPTY_STRING;
1375-
//
1376-
// itagItem.setBitrate(formatData.getInt("bitrate"));
1377-
// itagItem.setWidth(formatData.getInt("width"));
1378-
// itagItem.setHeight(formatData.getInt("height"));
1379-
// itagItem.setInitStart(Integer.parseInt(initRange.getString("start", "-1")));
1380-
// itagItem.setInitEnd(Integer.parseInt(initRange.getString("end", "-1")));
1381-
// itagItem.setIndexStart(Integer.parseInt(indexRange.getString("start", "-1")));
1382-
// itagItem.setIndexEnd(Integer.parseInt(indexRange.getString("end", "-1")));
1383-
// itagItem.setQuality(formatData.getString("quality"));
1384-
// itagItem.setCodec(codec);
1385-
//
1386-
// if (isLive() || isPostLive()) {
1387-
// itagItem.setTargetDurationSec(formatData.getInt("targetDurationSec"));
1388-
// }
1389-
//
1390-
// if (itagType == ItagItem.ItagType.VIDEO || itagType == ItagItem.ItagType.VIDEO_ONLY) {
1391-
// itagItem.setFps(formatData.getInt("fps"));
1392-
// }
1393-
// if (itagType == ItagItem.ItagType.AUDIO) {
1394-
// // YouTube returns the audio sample rate as a string
1395-
// itagItem.setSampleRate(Integer.parseInt(formatData.getString("audioSampleRate")));
1396-
// itagItem.setAudioChannels(formatData.getInt("audioChannels"));
1397-
// }
1398-
//
1399-
// // YouTube return the content length and the approximate duration as strings
1400-
// itagItem.setContentLength(Long.parseLong(formatData.getString(
1401-
// "contentLength",
1402-
// String.valueOf(CONTENT_LENGTH_UNKNOWN))));
1403-
// itagItem.setApproxDurationMs(Long.parseLong(formatData.getString(
1404-
// "approxDurationMs",
1405-
// String.valueOf(APPROX_DURATION_MS_UNKNOWN))));
1406-
//
1407-
// final ItagInfo itagInfo = new ItagInfo(streamUrl, itagItem);
1408-
//
1409-
// if (streamType == StreamType.VIDEO_STREAM) {
1410-
// itagInfo.setIsUrl(!formatData.getString("type", "")
1411-
// .equalsIgnoreCase("FORMAT_STREAM_TYPE_OTF"));
1412-
// } else {
1413-
// // We are currently not able to generate DASH manifests for running
1414-
// // livestreams, so because of the requirements of StreamInfo
1415-
// // objects, return these streams as DASH URL streams (even if they
1416-
// // are not playable).
1417-
// // Ended livestreams are returned as non URL streams
1418-
// itagInfo.setIsUrl(streamType != StreamType.POST_LIVE_STREAM);
1419-
// }
1420-
//
1421-
// return itagInfo;
1422-
// }
1423-
14241340
@Nonnull
14251341
@Override
14261342
public List<Frameset> getFrames() throws ExtractionException {

extractor/src/main/java/org/schabi/newpipe/extractor/utils/JsonUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,9 @@ public static List<String> getStringListFromJsonArray(@Nonnull final JsonArray a
161161
.map(String.class::cast)
162162
.collect(Collectors.toList());
163163
}
164+
165+
public static Integer getNullableInteger(@Nonnull final JsonObject jsonObject,
166+
@Nonnull final String key) {
167+
return (Integer) jsonObject.getNumber(key);
168+
}
164169
}

0 commit comments

Comments
 (0)