Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obtain stream length as a Duration #1106

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

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;
Expand All @@ -26,13 +26,14 @@ public BandcampRadioInfoItemExtractor(final JsonObject radioShow) {
show = radioShow;
}

@Nonnull
@Override
public long getDuration() {
public Duration getDurationObject() {
/* 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.ofSeconds(query(show.getInt("id")).getLong("audio_duration"));
return Duration.ZERO;
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,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 @@ -11,6 +11,7 @@

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

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

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,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
Expand Up @@ -60,11 +60,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 @@ -64,7 +64,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.getDurationObject().isZero())
.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 @@ -53,10 +54,11 @@ public boolean isAd() {
}

@Override
public long getDuration() {
@Nonnull
public Duration getDurationObject() {
// 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
Expand Up @@ -10,6 +10,7 @@

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;
Expand All @@ -32,8 +33,9 @@ public boolean isAd() {
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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;
Expand Down Expand Up @@ -100,8 +101,9 @@ public StreamType getStreamType() {
}

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

protected void setBaseUrl(final String baseUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.schabi.newpipe.extractor.stream.StreamType;

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

import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.getAllImagesFromArtworkOrAvatarUrl;
Expand All @@ -35,8 +36,9 @@ public String getName() {
}

@Override
public long getDuration() {
return itemObject.getLong("duration") / 1000L;
@Nonnull
public Duration getDurationObject() {
return Duration.ofMillis(itemObject.getLong("duration"));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import com.grack.nanojson.JsonWriter;

import org.jsoup.nodes.Entities;

import org.schabi.newpipe.extractor.Image;
import org.schabi.newpipe.extractor.Image.ResolutionLevel;
import org.schabi.newpipe.extractor.downloader.Response;
Expand All @@ -56,10 +56,12 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -310,21 +312,22 @@ public static boolean isY2ubeURL(@Nonnull final URL url) {
* @return the duration in seconds
* @throws ParsingException when more than 3 separators are found
*/
public static int parseDurationString(@Nonnull final String input)
throws ParsingException, NumberFormatException {
public static Duration parseDurationString(@Nonnull final String input)
throws ParsingException {
// If time separator : is not detected, try . instead
final String[] splitInput = input.contains(":")
? input.split(":")
: input.split("\\.");

final int[] units = {24, 60, 60, 1};
final int offset = units.length - splitInput.length;
final var units = List.of(ChronoUnit.DAYS, ChronoUnit.HOURS, ChronoUnit.MINUTES,
ChronoUnit.SECONDS);
final int offset = units.size() - splitInput.length;
if (offset < 0) {
throw new ParsingException("Error duration string with unknown format: " + input);
}
int duration = 0;
Duration duration = Duration.ZERO;
for (int i = 0; i < splitInput.length; i++) {
duration = units[i + offset] * (duration + convertDurationToInt(splitInput[i]));
duration = duration.plus(convertDurationToInt(splitInput[i]), units.get(i + offset));
}
return duration;
}
Expand All @@ -341,11 +344,7 @@ public static int parseDurationString(@Nonnull final String input)
* @return The converted integer or 0 if the conversion failed.
*/
private static int convertDurationToInt(final String input) {
if (input == null || input.isEmpty()) {
return 0;
}

final String clearedInput = Utils.removeNonDigitCharacters(input);
final String clearedInput = input != null ? Utils.removeNonDigitCharacters(input) : "";
try {
return Integer.parseInt(clearedInput);
} catch (final NumberFormatException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public boolean isAd() {
return false;
}

@Override
public long getDuration() {
// Not available when fetching through the feed endpoint.
return -1;
}

@Override
public long getViewCount() {
return Long.parseLong(entryElement.getElementsByTag("media:statistics").first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.schabi.newpipe.extractor.utils.Utils;

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

import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
Expand Down Expand Up @@ -67,7 +68,8 @@ public boolean isAd() {
}

@Override
public long getDuration() throws ParsingException {
@Nonnull
public Duration getDurationObject() throws ParsingException {
final String duration = descriptionElements.getObject(descriptionElements.size() - 1)
.getString("text");
if (!isNullOrEmpty(duration)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

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;
Expand Down Expand Up @@ -90,11 +89,6 @@ public boolean isAd() throws ParsingException {
return false;
}

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

@Override
public String getUploaderName() throws ParsingException {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.time.Duration;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -136,10 +136,11 @@ public String getName() throws ParsingException {
throw new ParsingException("Could not get name");
}

@Nonnull
@Override
public long getDuration() throws ParsingException {
public Duration getDurationObject() throws ParsingException {
if (getStreamType() == StreamType.LIVE_STREAM) {
return -1;
return Duration.ZERO;
}

String duration = getTextFromObject(videoInfo.getObject("lengthText"));
Expand Down Expand Up @@ -169,7 +170,7 @@ public long getDuration() throws ParsingException {
if (isPremiere()) {
// Premieres can be livestreams, so the duration is not available in this
// case
return -1;
return Duration.ZERO;
}

throw new ParsingException("Could not get duration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

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

/**
Expand All @@ -40,7 +41,8 @@ public class StreamInfoItem extends InfoItem {
@Nullable
private DateWrapper uploadDate;
private long viewCount = -1;
private long duration = -1;
@Nonnull
private Duration duration = Duration.ZERO;
Isira-Seneviratne marked this conversation as resolved.
Show resolved Hide resolved

private String uploaderUrl = null;
@Nonnull
Expand Down Expand Up @@ -76,12 +78,21 @@ public void setViewCount(final long viewCount) {
this.viewCount = viewCount;
}

public long getDuration() {
@Nonnull
public Duration getDurationObject() {
return duration;
}

public void setDurationObject(@Nonnull final Duration durationObject) {
this.duration = durationObject;
}

public long getDuration() {
return duration.toSeconds();
}

public void setDuration(final long duration) {
this.duration = duration;
this.duration = Duration.ofSeconds(duration);
}

public String getUploaderUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

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

public interface StreamInfoItemExtractor extends InfoItemExtractor {
Expand All @@ -48,12 +49,25 @@ public interface StreamInfoItemExtractor extends InfoItemExtractor {
boolean isAd() throws ParsingException;

/**
* Get the stream duration in seconds
* Get the stream duration as a {@link Duration}.
*
* @return the stream duration in seconds or -1 if no duration is available
* @return the stream duration in seconds or {@link Duration#ZERO} if no duration is available
* @throws ParsingException if there is an error in the extraction
*/
long getDuration() throws ParsingException;
@Nonnull
default Duration getDurationObject() throws ParsingException {
return Duration.ZERO;
}

/**
* Get the stream duration in seconds.
*
* @return the stream duration in seconds or 0 if no duration is available
* @throws ParsingException if there is an error in the extraction
*/
default long getDuration() throws ParsingException {
return getDurationObject().toSeconds();
}

/**
* Parses the number of views
Expand Down
Loading
Loading