Update close for try witch resources

This commit is contained in:
xjoao 2020-05-22 13:58:45 -04:00
parent 84f4760349
commit f3042b6b45

View File

@ -133,37 +133,35 @@ public class YoutubeManager {
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
YoutubePlaylist playlist;
try (BufferedReader br = new BufferedReader(isr)) {
playlist = null;
String inputLine;
while ((inputLine = br.readLine()) != null) {
if (inputLine.contains("window[\"ytInitialData\"]")) {
JsonObject obj = new JsonParser().parse(inputLine.substring(inputLine.indexOf("{")).replace(";", "")).getAsJsonObject();
YoutubePlaylist playlist = null;
JsonObject meta = obj.get("microformat").getAsJsonObject().get("microformatDataRenderer").getAsJsonObject();
String name = meta.get("title").getAsString();
String description = meta.get("description").getAsString();
String inputLine;
while ((inputLine = br.readLine()) != null) {
if (inputLine.contains("window[\"ytInitialData\"]")) {
JsonObject obj = new JsonParser().parse(inputLine.substring(inputLine.indexOf("{")).replace(";", "")).getAsJsonObject();
ArrayList<YoutubeVideo> videos = new ArrayList<>();
JsonObject meta = obj.get("microformat").getAsJsonObject().get("microformatDataRenderer").getAsJsonObject();
String name = meta.get("title").getAsString();
String description = meta.get("description").getAsString();
JsonArray rawVideos = obj.get("contents").getAsJsonObject().get("twoColumnBrowseResultsRenderer").getAsJsonObject().get("tabs").getAsJsonArray().get(0).getAsJsonObject().get("tabRenderer").getAsJsonObject().get("content").getAsJsonObject().get("sectionListRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("itemSectionRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("playlistVideoListRenderer").getAsJsonObject().get("contents").getAsJsonArray();
ArrayList<YoutubeVideo> videos = new ArrayList<>();
for (JsonElement rawVideo : rawVideos) {
JsonObject videoData = rawVideo.getAsJsonObject().get("playlistVideoRenderer").getAsJsonObject();
if (!videoData.has("lengthSeconds")) continue; // removed videos
videos.add(new YoutubeVideo(videoData.get("videoId").getAsString(), Integer.valueOf(videoData.get("lengthSeconds").getAsString())));
}
JsonArray rawVideos = obj.get("contents").getAsJsonObject().get("twoColumnBrowseResultsRenderer").getAsJsonObject().get("tabs").getAsJsonArray().get(0).getAsJsonObject().get("tabRenderer").getAsJsonObject().get("content").getAsJsonObject().get("sectionListRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("itemSectionRenderer").getAsJsonObject().get("contents").getAsJsonArray().get(0).getAsJsonObject().get("playlistVideoListRenderer").getAsJsonObject().get("contents").getAsJsonArray();
playlist = new YoutubePlaylist(playlistId, name, description, videos);
for (JsonElement rawVideo : rawVideos) {
JsonObject videoData = rawVideo.getAsJsonObject().get("playlistVideoRenderer").getAsJsonObject();
if (!videoData.has("lengthSeconds")) continue; // removed videos
videos.add(new YoutubeVideo(videoData.get("videoId").getAsString(), Integer.valueOf(videoData.get("lengthSeconds").getAsString())));
break;
}
playlist = new YoutubePlaylist(playlistId, name, description, videos);
break;
}
}
br.close();
this.playlistCache.put(playlistId, playlist);
return playlist;