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 335875d
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 112 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 @@ -4,7 +4,6 @@
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;

import org.schabi.newpipe.extractor.Page;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.downloader.Downloader;
Expand All @@ -16,11 +15,10 @@
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Comparator;

import javax.annotation.Nonnull;

public class MediaCCCRecentKiosk extends KioskExtractor<StreamInfoItem> {

public static final String KIOSK_ID = "recent";
Expand Down Expand Up @@ -64,7 +62,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
@@ -1,21 +1,20 @@
package org.schabi.newpipe.extractor.services.media_ccc.extractors;

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.linkHandler.MediaCCCConferenceLinkHandlerFactory;
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.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

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

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

public class MediaCCCRecentKioskExtractor implements StreamInfoItemExtractor {
Expand Down Expand Up @@ -53,10 +52,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
@@ -1,14 +1,14 @@
package org.schabi.newpipe.extractor.services.soundcloud.extractors;

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 java.time.Duration;
import java.util.List;

import static org.schabi.newpipe.extractor.services.soundcloud.SoundcloudParsingHelper.getAllImagesFromArtworkOrAvatarUrl;
Expand All @@ -35,8 +35,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
@@ -1,11 +1,6 @@
package org.schabi.newpipe.extractor.services.youtube.extractors;

import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem;
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 All @@ -14,10 +9,13 @@
import org.schabi.newpipe.extractor.stream.StreamType;
import org.schabi.newpipe.extractor.utils.Utils;

import java.util.List;

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

import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getTextFromObject;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getThumbnailsFromInfoItem;
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;

/**
* A {@link StreamInfoItemExtractor} for YouTube's {@code reelItemRenderers}.
Expand Down Expand Up @@ -90,11 +88,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
Loading

0 comments on commit 335875d

Please sign in to comment.