Skip to content

Commit e9644e6

Browse files
mauriciocolliTobiGr
authored andcommitted
[YouTube] Handle video premiere's date and duration
1 parent 2a470ac commit e9644e6

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeStreamInfoItemExtractor.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.grack.nanojson.JsonArray;
44
import com.grack.nanojson.JsonObject;
5-
65
import org.schabi.newpipe.extractor.exceptions.ParsingException;
76
import org.schabi.newpipe.extractor.localization.DateWrapper;
87
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
@@ -13,10 +12,11 @@
1312
import org.schabi.newpipe.extractor.utils.Utils;
1413

1514
import javax.annotation.Nullable;
15+
import java.text.SimpleDateFormat;
16+
import java.util.Calendar;
17+
import java.util.Date;
1618

17-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.fixThumbnailUrl;
18-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getTextFromObject;
19-
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.getUrlFromNavigationEndpoint;
19+
import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper.*;
2020

2121
/*
2222
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
@@ -86,7 +86,9 @@ public String getName() throws ParsingException {
8686

8787
@Override
8888
public long getDuration() throws ParsingException {
89-
if (getStreamType() == StreamType.LIVE_STREAM) return -1;
89+
if (getStreamType() == StreamType.LIVE_STREAM || isPremiere()) {
90+
return -1;
91+
}
9092

9193
String duration = null;
9294

@@ -165,7 +167,16 @@ public String getUploaderUrl() throws ParsingException {
165167

166168
@Nullable
167169
@Override
168-
public String getTextualUploadDate() {
170+
public String getTextualUploadDate() throws ParsingException {
171+
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
172+
return null;
173+
}
174+
175+
if (isPremiere()) {
176+
final Date date = getDateFromPremiere().getTime();
177+
return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
178+
}
179+
169180
try {
170181
return getTextFromObject(videoInfo.getObject("publishedTimeText"));
171182
} catch (Exception e) {
@@ -177,7 +188,15 @@ public String getTextualUploadDate() {
177188
@Nullable
178189
@Override
179190
public DateWrapper getUploadDate() throws ParsingException {
180-
String textualUploadDate = getTextualUploadDate();
191+
if (getStreamType().equals(StreamType.LIVE_STREAM)) {
192+
return null;
193+
}
194+
195+
if (isPremiere()) {
196+
return new DateWrapper(getDateFromPremiere());
197+
}
198+
199+
final String textualUploadDate = getTextualUploadDate();
181200
if (timeAgoParser != null && textualUploadDate != null && !textualUploadDate.isEmpty()) {
182201
try {
183202
return timeAgoParser.parse(textualUploadDate);
@@ -236,7 +255,26 @@ private boolean isPremium() {
236255
return true;
237256
}
238257
}
239-
} catch (Exception ignored) {}
258+
} catch (Exception ignored) {
259+
}
240260
return false;
241261
}
262+
263+
private boolean isPremiere() {
264+
return videoInfo.has("upcomingEventData");
265+
}
266+
267+
private Calendar getDateFromPremiere() throws ParsingException {
268+
final JsonObject upcomingEventData = videoInfo.getObject("upcomingEventData");
269+
final String startTime = upcomingEventData.getString("startTime");
270+
271+
try {
272+
final long startTimeTimestamp = Long.parseLong(startTime);
273+
final Calendar calendar = Calendar.getInstance();
274+
calendar.setTime(new Date(startTimeTimestamp * 1000L));
275+
return calendar;
276+
} catch (Exception e) {
277+
throw new ParsingException("Could not parse date from premiere: \"" + startTime + "\"");
278+
}
279+
}
242280
}

0 commit comments

Comments
 (0)