Skip to content

Commit

Permalink
Introduce class that indicates when the time ago is an approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciocolli committed Nov 3, 2019
1 parent 6ca4c89 commit 3d21ef5
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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) {
Expand Down Expand Up @@ -67,11 +68,12 @@ public void setTextualPublishedTime(String textualPublishedTime) {
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

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;
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,24 @@ public TimeAgoParser(PatternsHolder patternsHolder) {
}

/**
* 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.
* <p>
* 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<TimeAgoUnit, Map<String, Integer>> caseUnitEntry : patternsHolder.specialCases().entrySet()) {
final TimeAgoUnit timeAgoUnit = caseUnitEntry.getKey();
for (Map.Entry<String, Integer> caseMapToAmountEntry : caseUnitEntry.getValue().entrySet()) {
final String caseText = caseMapToAmountEntry.getKey();
final Integer caseAmount = caseMapToAmountEntry.getValue();

if (textualDateMatches(textualDate, caseText)) {
return getCalendar(caseAmount, timeAgoUnit);
return getResultFor(caseAmount, timeAgoUnit);
}
}
}
Expand All @@ -61,8 +62,8 @@ public Calendar parse(String textualDate) throws ParsingException {
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 {
Expand Down Expand Up @@ -110,8 +111,9 @@ private boolean textualDateMatches(String textualDate, String agoPhrase) {
}
}

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:
Expand All @@ -128,28 +130,32 @@ private Calendar getCalendar(int timeAgoAmount, TimeAgoUnit timeAgoUnit) {

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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
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 {
Expand All @@ -38,8 +35,8 @@ public String getTextualUploadDate() throws ParsingException {

@Nonnull
@Override
public Calendar getUploadDate() throws ParsingException {
return MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate());
public DateWrapper getUploadDate() throws ParsingException {
return new DateWrapper(MediaCCCParsingHelper.parseDateFrom(getTextualUploadDate()));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

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 {

Expand Down Expand Up @@ -47,14 +48,16 @@ public String getUploaderUrl() throws ParsingException {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
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;

Expand Down Expand Up @@ -58,8 +57,8 @@ public String getTextualUploadDate() {

@Nonnull
@Override
public Calendar getUploadDate() throws ParsingException {
return SoundcloudParsingHelper.parseDate(getTextualUploadDate());
public DateWrapper getUploadDate() throws ParsingException {
return new DateWrapper(SoundcloudParsingHelper.parseDate(getTextualUploadDate()));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

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 {
Expand Down Expand Up @@ -48,8 +47,8 @@ public String getTextualUploadDate() {
}

@Override
public Calendar getUploadDate() throws ParsingException {
return SoundcloudParsingHelper.parseDate(getTextualUploadDate());
public DateWrapper getUploadDate() throws ParsingException {
return new DateWrapper(SoundcloudParsingHelper.parseDate(getTextualUploadDate()));
}

private String getCreatedAt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
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 {

Expand Down Expand Up @@ -55,8 +56,9 @@ public String getTextualPublishedTime() throws ParsingException {
}
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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.*;
Expand Down Expand Up @@ -134,14 +135,14 @@ public String getTextualUploadDate() throws ParsingException {
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -141,6 +142,7 @@ public String getUploaderUrl() throws ParsingException {
}
}

@Nullable
@Override
public String getTextualUploadDate() throws ParsingException {
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
Expand Down Expand Up @@ -173,17 +175,15 @@ public String getTextualUploadDate() throws ParsingException {
}
}

@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();
Expand Down
Loading

0 comments on commit 3d21ef5

Please sign in to comment.