Skip to content

Commit

Permalink
Obtain stream length as a Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Sep 18, 2023
1 parent 7c7ceac commit 5c3115b
Show file tree
Hide file tree
Showing 21 changed files with 177 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import org.schabi.newpipe.extractor.timeago.PatternsHolder;
import org.schabi.newpipe.extractor.utils.Parser;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;

/**
* A helper class that is meant to be used by services that need to parse durations such as
Expand Down Expand Up @@ -68,45 +68,40 @@ public DateWrapper parse(final String textualDate) throws ParsingException {
}

/**
* Parses a textual duration into a duration computer number.
* Parses a textual duration into a {@link Duration} object.
*
* @param textualDuration the textual duration to parse
* @return the textual duration parsed, as a primitive {@code long}
* @throws ParsingException if the textual duration could not be parsed
* @return the textual duration parsed as a {@link Duration}
*/
public long parseDuration(final String textualDuration) throws ParsingException {
public Duration parseDuration(final String textualDuration) throws ParsingException {
// We can't use Matcher.results, as it is only available on Android 14 and above
final Matcher matcher = DURATION_PATTERN.matcher(textualDuration);
final var matcher = DURATION_PATTERN.matcher(textualDuration);
final List<MatchResult> results = new ArrayList<>();
while (matcher.find()) {
results.add(matcher.toMatchResult());
}

return results.stream()
.map(match -> {
final String digits = match.group(1);
final String word = match.group(2);

int amount;
try {
amount = Integer.parseInt(digits);
} catch (final NumberFormatException ignored) {
amount = 1;
}

final ChronoUnit unit;
try {
unit = parseChronoUnit(word);
} catch (final ParsingException ignored) {
return 0L;
}

return amount * unit.getDuration().getSeconds();
})
.filter(n -> n > 0)
.reduce(Long::sum)
.orElseThrow(() -> new ParsingException(
"Could not parse duration \"" + textualDuration + "\""));
var duration = Duration.ZERO;
for (final var match : results) {
final String digits = match.group(1);
final String word = match.group(2);

long amount;
try {
amount = Long.parseLong(digits);
} catch (final NumberFormatException ignored) {
amount = 1;
}

try {
duration = duration.plus(amount, parseChronoUnit(word));
} catch (final ParsingException ignored) {
}
}
if (duration.isZero()) {
throw new ParsingException("Could not parse duration \"" + textualDuration + "\"");
}
return duration;
}

private int parseTimeAgoAmount(final String textualDate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

package org.schabi.newpipe.extractor.services.bandcamp.extractors;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_URL;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromImageId;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.parseDate;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.time.Duration;
import java.util.List;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_URL;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromImageId;
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.parseDate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class BandcampRadioInfoItemExtractor implements StreamInfoItemExtractor {

Expand All @@ -26,13 +28,14 @@ public BandcampRadioInfoItemExtractor(final JsonObject radioShow) {
show = radioShow;
}

@Nonnull
@Override
public long getDuration() {
public Duration getDuration() {
/* Duration is only present in the more detailed information that has to be queried
separately. Therefore, over 300 queries would be needed every time the kiosk is opened if we
were to display the real value. */
//return query(show.getInt("id")).getLong("audio_duration");
return 0;
return Duration.ZERO;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromImageId;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper;

import javax.annotation.Nonnull;
import java.util.List;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromImageId;
import javax.annotation.Nonnull;

public class BandcampDiscographStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor {

Expand Down Expand Up @@ -43,9 +45,4 @@ public String getUrl() throws ParsingException {
public List<Image> getThumbnails() throws ParsingException {
return getImagesFromImageId(discograph.getLong("art_id"), true);
}

@Override
public long getDuration() {
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
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.stream.StreamExtractor;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.time.Duration;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nonnull;

public class BandcampPlaylistStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor {

private final JsonObject track;
Expand Down Expand Up @@ -46,9 +49,10 @@ public String getUrl() {
return getUploaderUrl() + track.getString("title_link");
}

@Nonnull
@Override
public long getDuration() {
return track.getLong("duration");
public Duration getDuration() {
return Duration.ofSeconds(track.getLong("duration"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package org.schabi.newpipe.extractor.services.bandcamp.extractors.streaminfoitem;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromSearchResult;

import org.jsoup.nodes.Element;
import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import javax.annotation.Nonnull;
import java.util.List;

import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.getImagesFromSearchResult;
import javax.annotation.Nonnull;

public class BandcampSearchStreamInfoItemExtractor extends BandcampStreamInfoItemExtractor {

Expand Down Expand Up @@ -47,9 +48,4 @@ public String getUrl() throws ParsingException {
public List<Image> getThumbnails() throws ParsingException {
return getImagesFromSearchResult(searchResult);
}

@Override
public long getDuration() {
return -1;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package org.schabi.newpipe.extractor.services.media_ccc.extractors;

import static org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCParsingHelper.getThumbnailsFromLiveStreamItem;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

import static org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCParsingHelper.getThumbnailsFromLiveStreamItem;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class MediaCCCLiveStreamKioskExtractor implements StreamInfoItemExtractor {

Expand Down Expand Up @@ -60,11 +62,6 @@ public boolean isAd() throws ParsingException {
return false;
}

@Override
public long getDuration() throws ParsingException {
return 0;
}

@Override
public long getViewCount() throws ParsingException {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import java.io.IOException;
import java.time.Duration;
import java.util.Comparator;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -64,7 +65,7 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
.map(JsonObject.class::cast)
.map(MediaCCCRecentKioskExtractor::new)
// #813 / voc/voctoweb#609 -> returns faulty data -> filter it out
.filter(extractor -> extractor.getDuration() > 0)
.filter(extractor -> extractor.getDuration().compareTo(Duration.ZERO) > 0)
.forEach(collector::commit);

return new InfoItemsPage<>(collector, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItemExtractor;
import org.schabi.newpipe.extractor.stream.StreamType;

import java.time.Duration;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
Expand Down Expand Up @@ -52,11 +53,12 @@ public boolean isAd() {
return false;
}

@Nonnull
@Override
public long getDuration() {
public Duration getDuration() {
// duration and length have the same value, see
// https://github.com/voc/voctoweb/blob/master/app/views/public/shared/_event.json.jbuilder
return event.getInt("duration");
return Duration.ofSeconds(event.getLong("duration"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package org.schabi.newpipe.extractor.services.media_ccc.extractors.infoItems;

import static org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCParsingHelper.getThumbnailsFromStreamItem;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.List;

import static org.schabi.newpipe.extractor.services.media_ccc.extractors.MediaCCCParsingHelper.getThumbnailsFromStreamItem;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class MediaCCCStreamInfoItemExtractor implements StreamInfoItemExtractor {
private final JsonObject event;
Expand All @@ -31,9 +34,10 @@ public boolean isAd() {
return false;
}

@Nonnull
@Override
public long getDuration() {
return event.getInt("length");
public Duration getDuration() {
return Duration.ofSeconds(event.getInt("length"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.schabi.newpipe.extractor.services.peertube.extractors;

import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getThumbnailsFromPlaylistOrVideoItem;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;

import com.grack.nanojson.JsonObject;

import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
Expand All @@ -9,12 +14,10 @@
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.JsonUtils;

import javax.annotation.Nonnull;
import java.time.Duration;
import java.util.List;

import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getAvatarsFromOwnerAccountOrVideoChannelObject;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.getThumbnailsFromPlaylistOrVideoItem;
import static org.schabi.newpipe.extractor.services.peertube.PeertubeParsingHelper.parseDateFrom;
import javax.annotation.Nonnull;

public class PeertubeStreamInfoItemExtractor implements StreamInfoItemExtractor {

Expand Down Expand Up @@ -99,9 +102,10 @@ public StreamType getStreamType() {
return item.getBoolean("isLive") ? StreamType.LIVE_STREAM : StreamType.VIDEO_STREAM;
}

@Nonnull
@Override
public long getDuration() {
return item.getLong("duration");
public Duration getDuration() {
return Duration.ofSeconds(item.getLong("duration"));
}

protected void setBaseUrl(final String baseUrl) {
Expand Down
Loading

0 comments on commit 5c3115b

Please sign in to comment.