From 147cd00ddb510131d4bd965defef35023ddd3e65 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Sat, 21 Jul 2018 21:44:41 +0700 Subject: [PATCH] Refactor date/year parsing --- source/net/filebot/web/OMDbClient.java | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/source/net/filebot/web/OMDbClient.java b/source/net/filebot/web/OMDbClient.java index 1b90c6a9..bc1c0de1 100644 --- a/source/net/filebot/web/OMDbClient.java +++ b/source/net/filebot/web/OMDbClient.java @@ -21,11 +21,13 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.TreeMap; import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import javax.swing.Icon; @@ -177,15 +179,7 @@ public class OMDbClient implements MovieIdentificationService { fields.put(MovieInfo.Property.vote_count, getVoteCount(data.get("imdbVotes"))); fields.put(MovieInfo.Property.imdb_id, data.get("imdbID")); fields.put(MovieInfo.Property.poster_path, data.get("poster")); - - // convert release date to yyyy-MM-dd - SimpleDate release = parsePartialDate(data.get("released"), "d MMM yyyy"); - if (release == null) { - release = parsePartialDate(data.get("released"), "yyyy"); - } - if (release != null) { - fields.put(MovieInfo.Property.release_date, release.toString()); - } + fields.put(MovieInfo.Property.release_date, getReleaseDate(data.get("released"))); // convert lists Pattern delim = Pattern.compile(","); @@ -216,6 +210,16 @@ public class OMDbClient implements MovieIdentificationService { } } + private String getReleaseDate(String value) { + if ("N/A".equals(value)) { + return null; + } + + return Stream.of("d MMM yyyy", "yyyy").map(f -> { + return parsePartialDate(value, f); + }).filter(Objects::nonNull).map(Objects::toString).findFirst().orElse(null); + } + private SimpleDate parsePartialDate(String value, String format) { if (value != null && value.length() > 0) { try {