Skip to content

Commit f7992b8

Browse files
committed
More refactoring...
1 parent 92a2455 commit f7992b8

File tree

2 files changed

+13
-92
lines changed

2 files changed

+13
-92
lines changed

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

@@ -1282,7 +1283,7 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
12821283
final ItagInfo<I> itagInfo = new ItagInfo<>(itagFormat, streamUrl);
12831284

12841285
if (itagFormat instanceof BaseAudioItagFormat) {
1285-
final Integer averageBitrate = getIntegerFromJson(formatData, "averageBitrate");
1286+
final Integer averageBitrate = getNullableInteger(formatData, "averageBitrate");
12861287
if (averageBitrate != null) {
12871288
itagInfo.setAverageBitrate((int) Math.round(averageBitrate / 1000d));
12881289
}
@@ -1293,15 +1294,15 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
12931294
} catch (final Exception ignore) {
12941295
// Ignore errors - leave default value
12951296
}
1296-
itagInfo.setAudioChannels(getIntegerFromJson(formatData, "audioChannels"));
1297+
itagInfo.setAudioChannels(getNullableInteger(formatData, "audioChannels"));
12971298
}
12981299
if (itagFormat instanceof VideoItagFormat) {
1299-
itagInfo.setHeight(getIntegerFromJson(formatData, "height"));
1300-
itagInfo.setWidth(getIntegerFromJson(formatData, "width"));
1301-
itagInfo.setFps(getIntegerFromJson(formatData, "fps"));
1300+
itagInfo.setHeight(getNullableInteger(formatData, "height"));
1301+
itagInfo.setWidth(getNullableInteger(formatData, "width"));
1302+
itagInfo.setFps(getNullableInteger(formatData, "fps"));
13021303
}
13031304

1304-
itagInfo.setBitRate(getIntegerFromJson(formatData, "bitRate"));
1305+
itagInfo.setBitRate(getNullableInteger(formatData, "bitRate"));
13051306
itagInfo.setQuality(formatData.getString("quality"));
13061307

13071308
final String mimeType = formatData.getString("mimeType", "");
@@ -1327,96 +1328,11 @@ private <I extends ItagFormat<?>> ItagInfo<I> buildItagInfo(
13271328
}
13281329

13291330
itagInfo.setType(formatData.getString("type"));
1330-
itagInfo.setTargetDurationSec(getIntegerFromJson(formatData, "targetDurationSec"));
1331+
itagInfo.setTargetDurationSec(getNullableInteger(formatData, "targetDurationSec"));
13311332

13321333
return itagInfo;
13331334
}
13341335

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