Skip to content

Commit

Permalink
Extract stream duration as a Java 8 Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Jul 12, 2024
1 parent 592f159 commit 1ab1a80
Show file tree
Hide file tree
Showing 22 changed files with 106 additions and 93 deletions.
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
@@ -1,5 +1,10 @@
package org.schabi.newpipe.extractor.services.peertube;

import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN;
import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;

Expand All @@ -17,7 +22,6 @@
import org.schabi.newpipe.extractor.utils.JsonUtils;
import org.schabi.newpipe.extractor.utils.Parser;

import javax.annotation.Nonnull;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
Expand All @@ -27,10 +31,7 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.schabi.newpipe.extractor.Image.HEIGHT_UNKNOWN;
import static org.schabi.newpipe.extractor.Image.WIDTH_UNKNOWN;
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
import javax.annotation.Nonnull;

public final class PeertubeParsingHelper {
public static final String START_KEY = "start";
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 @@ -20,19 +20,12 @@

package org.schabi.newpipe.extractor.services.youtube;

import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonBuilder;
import com.grack.nanojson.JsonObject;
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;
Expand All @@ -51,15 +44,19 @@
import org.schabi.newpipe.extractor.utils.RandomStringFromAlphabetGenerator;
import org.schabi.newpipe.extractor.utils.Utils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 All @@ -71,8 +68,11 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
import static org.schabi.newpipe.extractor.utils.Utils.getStringResultFromRegexArray;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

public final class YoutubeParsingHelper {

Expand Down Expand Up @@ -310,21 +310,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 +342,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
Loading

0 comments on commit 1ab1a80

Please sign in to comment.