From 3d21ef5dba2c99ec533969e6c681809de62e70e7 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Sun, 3 Nov 2019 15:45:25 -0300 Subject: [PATCH] Introduce class that indicates when the time ago is an approximation --- .../extractor/comments/CommentsInfoItem.java | 10 +++-- .../comments/CommentsInfoItemExtractor.java | 6 ++- .../extractor/localization/DateWrapper.java | 39 +++++++++++++++++++ .../extractor/localization/TimeAgoParser.java | 36 ++++++++++------- .../extractors/MediaCCCStreamExtractor.java | 9 ++--- .../MediaCCCStreamInfoItemExtractor.java | 9 +++-- .../soundcloud/SoundcloudStreamExtractor.java | 7 ++-- .../SoundcloudStreamInfoItemExtractor.java | 7 ++-- .../YoutubeCommentsInfoItemExtractor.java | 6 ++- .../extractors/YoutubeStreamExtractor.java | 5 ++- .../YoutubeStreamInfoItemExtractor.java | 10 ++--- .../extractor/stream/StreamExtractor.java | 5 +-- .../newpipe/extractor/stream/StreamInfo.java | 16 ++++---- .../extractor/stream/StreamInfoItem.java | 16 ++------ .../stream/StreamInfoItemExtractor.java | 12 +++--- .../extractor/services/DefaultTests.java | 5 ++- .../MediaCCCStreamExtractorTest.java | 3 +- .../SoundcloudStreamExtractorDefaultTest.java | 3 +- .../YoutubeChannelLocalizationTest.java | 15 +++---- ...utubeStreamExtractorAgeRestrictedTest.java | 5 +-- ...utubeStreamExtractorControversialTest.java | 3 +- .../YoutubeStreamExtractorDefaultTest.java | 4 +- 22 files changed, 139 insertions(+), 92 deletions(-) create mode 100644 extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java index 7e225026e..6d6290716 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItem.java @@ -1,8 +1,9 @@ package org.schabi.newpipe.extractor.comments; import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.localization.DateWrapper; -import java.util.Calendar; +import javax.annotation.Nullable; public class CommentsInfoItem extends InfoItem { @@ -12,7 +13,7 @@ public class CommentsInfoItem extends InfoItem { private String authorThumbnail; private String authorEndpoint; private String textualPublishedTime; - private Calendar publishedTime; + @Nullable private DateWrapper publishedTime; private int likeCount; public CommentsInfoItem(int serviceId, String url, String name) { @@ -67,11 +68,12 @@ public class CommentsInfoItem extends InfoItem { this.textualPublishedTime = textualPublishedTime; } - public Calendar getPublishedTime() { + @Nullable + public DateWrapper getPublishedTime() { return publishedTime; } - public void setPublishedTime(Calendar publishedTime) { + public void setPublishedTime(@Nullable DateWrapper publishedTime) { this.publishedTime = publishedTime; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java index 4560f0441..7b40f8fa6 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/comments/CommentsInfoItemExtractor.java @@ -2,8 +2,9 @@ package org.schabi.newpipe.extractor.comments; import org.schabi.newpipe.extractor.InfoItemExtractor; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.localization.DateWrapper; -import java.util.Calendar; +import javax.annotation.Nullable; public interface CommentsInfoItemExtractor extends InfoItemExtractor { String getCommentId() throws ParsingException; @@ -12,6 +13,7 @@ public interface CommentsInfoItemExtractor extends InfoItemExtractor { String getAuthorThumbnail() throws ParsingException; String getAuthorEndpoint() throws ParsingException; String getTextualPublishedTime() throws ParsingException; - Calendar getPublishedTime() throws ParsingException; + @Nullable + DateWrapper getPublishedTime() throws ParsingException; int getLikeCount() throws ParsingException; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java new file mode 100644 index 000000000..433287852 --- /dev/null +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/DateWrapper.java @@ -0,0 +1,39 @@ +package org.schabi.newpipe.extractor.localization; + +import edu.umd.cs.findbugs.annotations.NonNull; + +import java.io.Serializable; +import java.util.Calendar; + +/** + * A wrapper class that provides a field to describe if the date is precise or just an approximation. + */ +public class DateWrapper implements Serializable { + @NonNull private final Calendar date; + private final boolean isApproximation; + + public DateWrapper(@NonNull Calendar date) { + this(date, false); + } + + public DateWrapper(@NonNull Calendar date, boolean isApproximation) { + this.date = date; + this.isApproximation = isApproximation; + } + + /** + * @return the wrapped date. + */ + @NonNull + public Calendar date() { + return date; + } + + /** + * @return if the date is considered is precise or just an approximation (e.g. service only returns an approximation + * like 2 weeks ago instead of a precise date). + */ + public boolean isApproximation() { + return isApproximation; + } +} diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java index 3d2e99237..aae9b14c4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/localization/TimeAgoParser.java @@ -31,15 +31,16 @@ public class TimeAgoParser { } /** - * Parses a textual date in the format '2 days ago' into a Calendar representation. - * Beginning with weeks ago, marks the date as approximated by setting minutes, seconds - * and milliseconds to 0. + * Parses a textual date in the format '2 days ago' into a Calendar representation which is then wrapped in a + * {@link DateWrapper} object. + *

+ * Beginning with days ago, the date is considered as an approximation. * * @param textualDate The original date as provided by the streaming service - * @return The parsed (approximated) time + * @return The parsed time (can be approximated) * @throws ParsingException if the time unit could not be recognized */ - public Calendar parse(String textualDate) throws ParsingException { + public DateWrapper parse(String textualDate) throws ParsingException { for (Map.Entry> caseUnitEntry : patternsHolder.specialCases().entrySet()) { final TimeAgoUnit timeAgoUnit = caseUnitEntry.getKey(); for (Map.Entry caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) { @@ -47,7 +48,7 @@ public class TimeAgoParser { final Integer caseAmount = caseMapToAmountEntry.getValue(); if (textualDateMatches(textualDate, caseText)) { - return getCalendar(caseAmount, timeAgoUnit); + return getResultFor(caseAmount, timeAgoUnit); } } } @@ -61,8 +62,8 @@ public class TimeAgoParser { timeAgoAmount = 1; } - TimeAgoUnit timeAgoUnit = parseTimeAgoUnit(textualDate); - return getCalendar(timeAgoAmount, timeAgoUnit); + final TimeAgoUnit timeAgoUnit = parseTimeAgoUnit(textualDate); + return getResultFor(timeAgoAmount, timeAgoUnit); } private int parseTimeAgoAmount(String textualDate) throws NumberFormatException { @@ -110,8 +111,9 @@ public class TimeAgoParser { } } - private Calendar getCalendar(int timeAgoAmount, TimeAgoUnit timeAgoUnit) { - Calendar calendarTime = getNow(); + private DateWrapper getResultFor(int timeAgoAmount, TimeAgoUnit timeAgoUnit) { + final Calendar calendarTime = getNow(); + boolean isApproximation = false; switch (timeAgoUnit) { case SECONDS: @@ -128,28 +130,32 @@ public class TimeAgoParser { case DAYS: calendarTime.add(Calendar.DAY_OF_MONTH, -timeAgoAmount); - markApproximatedTime(calendarTime); + isApproximation = true; break; case WEEKS: calendarTime.add(Calendar.WEEK_OF_YEAR, -timeAgoAmount); - markApproximatedTime(calendarTime); + isApproximation = true; break; case MONTHS: calendarTime.add(Calendar.MONTH, -timeAgoAmount); - markApproximatedTime(calendarTime); + isApproximation = true; break; case YEARS: calendarTime.add(Calendar.YEAR, -timeAgoAmount); // Prevent `PrettyTime` from showing '12 months ago'. calendarTime.add(Calendar.DAY_OF_MONTH, -1); - markApproximatedTime(calendarTime); + isApproximation = true; break; } - return calendarTime; + if (isApproximation) { + markApproximatedTime(calendarTime); + } + + return new DateWrapper(calendarTime, isApproximation); } private Calendar getNow() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java index 16e5e0d52..a1dfd2454 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/MediaCCCStreamExtractor.java @@ -10,15 +10,12 @@ import org.schabi.newpipe.extractor.downloader.Downloader; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.linkhandler.LinkHandler; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.*; import javax.annotation.Nonnull; import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; import java.util.List; public class MediaCCCStreamExtractor extends StreamExtractor { @@ -38,8 +35,8 @@ public class MediaCCCStreamExtractor extends StreamExtractor { @Nonnull @Override - public Calendar getUploadDate() throws ParsingException { - return MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate()); + public DateWrapper getUploadDate() throws ParsingException { + return new DateWrapper(MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate())); } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCStreamInfoItemExtractor.java index 085c115b8..76347b39b 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/media_ccc/extractors/infoItems/MediaCCCStreamInfoItemExtractor.java @@ -2,11 +2,12 @@ package org.schabi.newpipe.extractor.services.media_ccc.extractors.infoItems; import com.grack.nanojson.JsonObject; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCParsingHelper; import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamType; -import java.util.Calendar; +import javax.annotation.Nullable; public class MediaCCCStreamInfoItemExtractor implements StreamInfoItemExtractor { @@ -47,14 +48,16 @@ public class MediaCCCStreamInfoItemExtractor implements StreamInfoItemExtractor return event.getString("conference_url"); } + @Nullable @Override public String getTextualUploadDate() throws ParsingException { return event.getString("release_date"); } + @Nullable @Override - public Calendar getUploadDate() throws ParsingException { - return MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate()); + public DateWrapper getUploadDate() throws ParsingException { + return new DateWrapper(MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate())); } @Override diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java index 0fa636e7f..c9c44c991 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractor.java @@ -9,15 +9,14 @@ import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.linkhandler.LinkHandler; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.*; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; -import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -58,8 +57,8 @@ public class SoundcloudStreamExtractor extends StreamExtractor { @Nonnull @Override - public Calendar getUploadDate() throws ParsingException { - return SoundcloudParsingHelper.parseDate(getTextualUploadDate()); + public DateWrapper getUploadDate() throws ParsingException { + return new DateWrapper(SoundcloudParsingHelper.parseDate(getTextualUploadDate())); } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamInfoItemExtractor.java index 7453c5ada..da0ca1b54 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamInfoItemExtractor.java @@ -2,11 +2,10 @@ package org.schabi.newpipe.extractor.services.soundcloud; import com.grack.nanojson.JsonObject; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamType; -import java.util.Calendar; - import static org.schabi.newpipe.extractor.utils.Utils.replaceHttpWithHttps; public class SoundcloudStreamInfoItemExtractor implements StreamInfoItemExtractor { @@ -48,8 +47,8 @@ public class SoundcloudStreamInfoItemExtractor implements StreamInfoItemExtracto } @Override - public Calendar getUploadDate() throws ParsingException { - return SoundcloudParsingHelper.parseDate(getTextualUploadDate()); + public DateWrapper getUploadDate() throws ParsingException { + return new DateWrapper(SoundcloudParsingHelper.parseDate(getTextualUploadDate())); } private String getCreatedAt() { diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java index f3e6df2a4..3fa0e388e 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeCommentsInfoItemExtractor.java @@ -5,10 +5,11 @@ import com.grack.nanojson.JsonObject; import org.schabi.newpipe.extractor.comments.CommentsInfoItemExtractor; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.localization.TimeAgoParser; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.utils.JsonUtils; import org.schabi.newpipe.extractor.utils.Utils; -import java.util.Calendar; +import javax.annotation.Nullable; public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtractor { @@ -55,8 +56,9 @@ public class YoutubeCommentsInfoItemExtractor implements CommentsInfoItemExtract } } + @Nullable @Override - public Calendar getPublishedTime() throws ParsingException { + public DateWrapper getPublishedTime() throws ParsingException { String textualPublishedTime = getTextualPublishedTime(); if (timeAgoParser != null && textualPublishedTime != null && !textualPublishedTime.isEmpty()) { return timeAgoParser.parse(textualPublishedTime); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java index 7bafff098..671db465d 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamExtractor.java @@ -23,6 +23,7 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ReCaptchaException; import org.schabi.newpipe.extractor.linkhandler.LinkHandler; import org.schabi.newpipe.extractor.localization.TimeAgoParser; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.youtube.ItagItem; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper; import org.schabi.newpipe.extractor.stream.*; @@ -134,14 +135,14 @@ public class YoutubeStreamExtractor extends StreamExtractor { } @Override - public Calendar getUploadDate() throws ParsingException { + public DateWrapper getUploadDate() throws ParsingException { final String textualUploadDate = getTextualUploadDate(); if (textualUploadDate == null) { return null; } - return YoutubeParsingHelper.parseDateFrom(textualUploadDate); + return new DateWrapper(YoutubeParsingHelper.parseDateFrom(textualUploadDate)); } @Nonnull diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java index 98a484d3a..514b4fb0a 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java @@ -4,6 +4,7 @@ import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.localization.TimeAgoParser; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper; import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor; import org.schabi.newpipe.extractor.stream.StreamType; @@ -141,6 +142,7 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { } } + @Nullable @Override public String getTextualUploadDate() throws ParsingException { if (getStreamType().equals(StreamType.LIVE_STREAM)) { @@ -173,17 +175,15 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor { } } + @Nullable @Override - public Calendar getUploadDate() throws ParsingException { + public DateWrapper getUploadDate() throws ParsingException { if (getStreamType().equals(StreamType.LIVE_STREAM)) { return null; } if (isVideoReminder()) { - final Calendar calendar = getDateFromReminder(); - if (calendar != null) { - return calendar; - } + return new DateWrapper(getDateFromReminder()); } String textualUploadDate = getTextualUploadDate(); diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java index 7957de2a5..d697dff2c 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamExtractor.java @@ -26,13 +26,12 @@ import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.linkhandler.LinkHandler; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.utils.Parser; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.io.IOException; -import java.util.ArrayList; -import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -72,7 +71,7 @@ public abstract class StreamExtractor extends Extractor { * @see #getTextualUploadDate() */ @Nullable - public abstract Calendar getUploadDate() throws ParsingException; + public abstract DateWrapper getUploadDate() throws ParsingException; /** * This will return the url to the thumbnail of the stream. Try to return the medium resolution here. diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java index 8a049faa0..34ea703c2 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfo.java @@ -1,19 +1,19 @@ package org.schabi.newpipe.extractor.stream; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - import org.schabi.newpipe.extractor.Info; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException; import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.utils.DashMpdParser; import org.schabi.newpipe.extractor.utils.ExtractorHelper; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + /* * Created by Christian Schabesberger on 26.08.15. * @@ -278,7 +278,7 @@ public class StreamInfo extends Info { private StreamType streamType; private String thumbnailUrl = ""; private String textualUploadDate; - private Calendar uploadDate; + private DateWrapper uploadDate; private long duration = -1; private int ageLimit = -1; private String description; @@ -342,11 +342,11 @@ public class StreamInfo extends Info { this.textualUploadDate = textualUploadDate; } - public Calendar getUploadDate() { + public DateWrapper getUploadDate() { return uploadDate; } - public void setUploadDate(Calendar uploadDate) { + public void setUploadDate(DateWrapper uploadDate) { this.uploadDate = uploadDate; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java index 202188a79..6623c1efe 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItem.java @@ -21,9 +21,9 @@ package org.schabi.newpipe.extractor.stream; */ import org.schabi.newpipe.extractor.InfoItem; +import org.schabi.newpipe.extractor.localization.DateWrapper; import javax.annotation.Nullable; -import java.util.Calendar; /** * Info object for previews of unopened videos, eg search results, related videos @@ -33,7 +33,7 @@ public class StreamInfoItem extends InfoItem { private String uploaderName; private String textualUploadDate; - private Calendar uploadDate; + @Nullable private DateWrapper uploadDate; private long viewCount = -1; private long duration = -1; @@ -80,10 +80,6 @@ public class StreamInfoItem extends InfoItem { this.uploaderUrl = uploaderUrl; } - /** - * @return The original textual upload date as returned by the streaming service. - * @see #getUploadDate() - */ @Nullable public String getTextualUploadDate() { return textualUploadDate; @@ -93,16 +89,12 @@ public class StreamInfoItem extends InfoItem { this.textualUploadDate = uploadDate; } - /** - * @return The (approximated) date and time this item was uploaded or {@code null}. - * @see #getTextualUploadDate() - */ @Nullable - public Calendar getUploadDate() { + public DateWrapper getUploadDate() { return uploadDate; } - public void setUploadDate(Calendar uploadDate) { + public void setUploadDate(@Nullable DateWrapper uploadDate) { this.uploadDate = uploadDate; } diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java index 154e74651..113f3a87f 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/stream/StreamInfoItemExtractor.java @@ -2,8 +2,9 @@ package org.schabi.newpipe.extractor.stream; import org.schabi.newpipe.extractor.InfoItemExtractor; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.localization.DateWrapper; -import java.util.Calendar; +import javax.annotation.Nullable; /* * Created by Christian Schabesberger on 28.02.16. @@ -69,26 +70,27 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor { * The original textual date provided by the service. Should be used as a fallback if * {@link #getUploadDate()} isn't provided by the service, or it fails for some reason. * - * @return The original textual date provided by the service. + * @return The original textual date provided by the service or {@code null} if not provided. * @throws ParsingException if there is an error in the extraction * @see #getUploadDate() */ + @Nullable String getTextualUploadDate() throws ParsingException; /** * Extracts the upload date and time of this item and parses it. *

* If the service doesn't provide an exact time, an approximation can be returned. - * The approximation should be marked by setting seconds and milliseconds to zero. *
* If the service doesn't provide any date at all, then {@code null} should be returned. *

* - * @return The (approximated) date and time this item was uploaded or {@code null}. + * @return The date and time (can be approximated) this item was uploaded or {@code null}. * @throws ParsingException if there is an error in the extraction * or the extracted date couldn't be parsed. * @see #getTextualUploadDate() */ - Calendar getUploadDate() throws ParsingException; + @Nullable + DateWrapper getUploadDate() throws ParsingException; } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java index 3b839fd89..8cf27094a 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java @@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.services; import org.schabi.newpipe.extractor.InfoItem; import org.schabi.newpipe.extractor.ListExtractor; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import java.util.Calendar; @@ -31,9 +32,9 @@ public final class DefaultTests { final String textualUploadDate = streamInfoItem.getTextualUploadDate(); if (textualUploadDate != null && !textualUploadDate.isEmpty()) { - final Calendar uploadDate = streamInfoItem.getUploadDate(); + final DateWrapper uploadDate = streamInfoItem.getUploadDate(); assertNotNull("No parsed upload date", uploadDate); - assertTrue("Upload date not in the past", uploadDate.before(Calendar.getInstance())); + assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance())); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCStreamExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCStreamExtractorTest.java index 9aa1ef025..befd840f4 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCStreamExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/MediaCCCStreamExtractorTest.java @@ -14,6 +14,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; +import static java.util.Objects.requireNonNull; import static junit.framework.TestCase.assertEquals; import static org.schabi.newpipe.extractor.ServiceList.MediaCCC; @@ -95,6 +96,6 @@ public class MediaCCCStreamExtractorTest implements BaseExtractorTest { public void testGetUploadDate() throws ParsingException, ParseException { final Calendar instance = Calendar.getInstance(); instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2018-05-11")); - Assert.assertEquals(instance, extractor.getUploadDate()); + assertEquals(instance, requireNonNull(extractor.getUploadDate()).date()); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java index 9610a43af..8a3883fd6 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudStreamExtractorDefaultTest.java @@ -16,6 +16,7 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; +import static java.util.Objects.requireNonNull; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.SoundCloud; @@ -80,7 +81,7 @@ public class SoundcloudStreamExtractorDefaultTest { public void testGetUploadDate() throws ParsingException, ParseException { final Calendar instance = Calendar.getInstance(); instance.setTime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss +0000").parse("2016/07/31 18:18:07 +0000")); - Assert.assertEquals(instance, extractor.getUploadDate()); + assertEquals(instance, requireNonNull(extractor.getUploadDate()).date()); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java index 731225af2..29f3d0323 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java @@ -7,6 +7,7 @@ import org.schabi.newpipe.extractor.ListExtractor; import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.channel.ChannelExtractor; import org.schabi.newpipe.extractor.localization.Localization; +import org.schabi.newpipe.extractor.localization.DateWrapper; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import java.text.SimpleDateFormat; @@ -61,9 +62,9 @@ public class YoutubeChannelLocalizationTest { String debugMessage = "[" + String.format("%02d", i) + "] " + currentLocalization.getLocalizationCode() + " → " + item.getName() + "\n:::: " + item.getStreamType() + ", views = " + item.getViewCount(); - final Calendar uploadDate = item.getUploadDate(); + final DateWrapper uploadDate = item.getUploadDate(); if (uploadDate != null) { - String dateAsText = dateFormat.format(uploadDate.getTime()); + String dateAsText = dateFormat.format(uploadDate.date().getTime()); debugMessage += "\n:::: " + item.getTextualUploadDate() + "\n:::: " + dateAsText; } @@ -102,17 +103,17 @@ public class YoutubeChannelLocalizationTest { final StreamInfoItem referenceItem = referenceList.get(i); final StreamInfoItem currentItem = currentList.get(i); - final Calendar referenceUploadDate = referenceItem.getUploadDate(); - final Calendar currentUploadDate = currentItem.getUploadDate(); + final DateWrapper referenceUploadDate = referenceItem.getUploadDate(); + final DateWrapper currentUploadDate = currentItem.getUploadDate(); final String referenceDateString = referenceUploadDate == null ? "null" : - dateFormat.format(referenceUploadDate.getTime()); + dateFormat.format(referenceUploadDate.date().getTime()); final String currentDateString = currentUploadDate == null ? "null" : - dateFormat.format(currentUploadDate.getTime()); + dateFormat.format(currentUploadDate.date().getTime()); long difference = -1; if (referenceUploadDate != null && currentUploadDate != null) { - difference = Math.abs(referenceUploadDate.getTimeInMillis() - currentUploadDate.getTimeInMillis()); + difference = Math.abs(referenceUploadDate.date().getTimeInMillis() - currentUploadDate.date().getTimeInMillis()); } final boolean areTimeEquals = difference < 5 * 60 * 1000L; diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java index c626651aa..992a8cd92 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java @@ -1,7 +1,6 @@ package org.schabi.newpipe.extractor.services.youtube.stream; import org.junit.BeforeClass; -import org.junit.Ignore; import org.junit.Test; import org.schabi.newpipe.DownloaderTestImpl; import org.schabi.newpipe.extractor.MediaFormat; @@ -18,9 +17,9 @@ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; -import java.util.Date; import java.util.List; +import static java.util.Objects.requireNonNull; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; @@ -93,7 +92,7 @@ public class YoutubeStreamExtractorAgeRestrictedTest { public void testGetUploadDate() throws ParsingException, ParseException { final Calendar instance = Calendar.getInstance(); instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2017-01-25")); - assertEquals(instance, extractor.getUploadDate()); + assertEquals(instance, requireNonNull(extractor.getUploadDate()).date()); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java index 214f6beca..7faec8051 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; +import static java.util.Objects.requireNonNull; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; @@ -92,7 +93,7 @@ public class YoutubeStreamExtractorControversialTest { public void testGetUploadDate() throws ParsingException, ParseException { final Calendar instance = Calendar.getInstance(); instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2010-09-09")); - assertEquals(instance, extractor.getUploadDate()); + assertEquals(instance, requireNonNull(extractor.getUploadDate()).date()); } @Test diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java index 27638efe9..e715144a9 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java @@ -17,9 +17,9 @@ import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; -import java.util.Date; import java.util.List; +import static java.util.Objects.*; import static org.junit.Assert.*; import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; @@ -119,7 +119,7 @@ public class YoutubeStreamExtractorDefaultTest { public void testGetUploadDate() throws ParsingException, ParseException { final Calendar instance = Calendar.getInstance(); instance.setTime(new SimpleDateFormat("yyyy-MM-dd").parse("2015-10-22")); - Assert.assertEquals(instance, extractor.getUploadDate()); + assertEquals(instance, requireNonNull(extractor.getUploadDate()).date()); } @Test