From 9d1d73137e2b335c85d3d1fb785478689eb7e910 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 18 Oct 2021 22:35:44 +0200 Subject: [PATCH] opt in YoutubeAPI --- src/main/de/mrbesen/youtubecrawler/YoutubeAPI.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/de/mrbesen/youtubecrawler/YoutubeAPI.java b/src/main/de/mrbesen/youtubecrawler/YoutubeAPI.java index 7cce399..844a955 100644 --- a/src/main/de/mrbesen/youtubecrawler/YoutubeAPI.java +++ b/src/main/de/mrbesen/youtubecrawler/YoutubeAPI.java @@ -101,10 +101,13 @@ public class YoutubeAPI { private Crawler.Video getVid(JSONObject json) { String vdid = json.getString("id"); JSONObject snippet = json.getJSONObject("snippet"); - String title = snippet.getString("title"); //maxlen: 100 - long published = getDate(snippet.getString("publishedAt")); - String channel = snippet.getString("channelTitle"); - String tags = snippet.getJSONArray("tags").toList().stream().map(o -> (String) o).collect(Collectors.joining(",")); // max len: ~500 + String title = snippet.optString("title", ""); //maxlen: 100 + long published = getDate(snippet.optString("publishedAt", "")); + String channel = snippet.optString("channelTitle", ""); + + String tags = ""; + if(snippet.optJSONArray("tags") != null) + tags = snippet.getJSONArray("tags").toList().stream().map(o -> (String) o).collect(Collectors.joining(",")); // max len: ~500 byte category = 0; try { @@ -112,7 +115,7 @@ public class YoutubeAPI { } catch(NumberFormatException e) {} JSONObject contentDetails = json.getJSONObject("contentDetails"); - int duration = (int) getDuration(contentDetails.getString("duration")); + int duration = (int) getDuration(contentDetails.optString("duration", "")); boolean live = !snippet.getString("liveBroadcastContent").equalsIgnoreCase("none"); String langCode = snippet.optString("defaultLanguage", snippet.optString("defaultAudioLanguage", ""));