Merge pull request #619 from TeamPiped/no-streams-found-fix

Use stream extractor directly to fix no streams found error.
This commit is contained in:
Kavin 2023-06-06 02:34:14 +01:00 committed by GitHub
commit 164ee66e46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 7 deletions

View File

@ -27,7 +27,8 @@ import org.apache.commons.lang3.exception.ExceptionUtils;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -40,6 +41,7 @@ import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
import static io.activej.http.HttpHeaders.*; import static io.activej.http.HttpHeaders.*;
import static io.activej.http.HttpMethod.*; import static io.activej.http.HttpMethod.*;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static me.kavin.piped.consts.Constants.YOUTUBE_SERVICE;
import static me.kavin.piped.consts.Constants.mapper; import static me.kavin.piped.consts.Constants.mapper;
public class ServerLauncher extends MultithreadedHttpServerLauncher { public class ServerLauncher extends MultithreadedHttpServerLauncher {
@ -96,15 +98,25 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
try { try {
Sentry.setExtra("videoId", videoId); Sentry.setExtra("videoId", videoId);
StreamInfo info = StreamInfo.getInfo(url); var extractor = YOUTUBE_SERVICE.getStreamExtractor(videoId);
extractor.fetchPage();
Multithreading.runAsync(() -> { Multithreading.runAsync(() -> {
if (info.getUploadDate() != null && System.currentTimeMillis() - info.getUploadDate().offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) {
DateWrapper uploadDate;
try {
uploadDate = extractor.getUploadDate();
} catch (ParsingException e) {
throw new RuntimeException(e);
}
if (uploadDate != null && System.currentTimeMillis() - uploadDate.offsetDateTime().toInstant().toEpochMilli() < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) {
try { try {
MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo( MatrixHelper.sendEvent("video.piped.stream.info", new FederatedVideoInfo(
StringUtils.substring(info.getUrl(), -11), StringUtils.substring(info.getUploaderUrl(), -24), StringUtils.substring(extractor.getUrl(), -11), StringUtils.substring(extractor.getUploaderUrl(), -24),
info.getName(), extractor.getName(),
info.getDuration(), info.getViewCount()) extractor.getLength(), extractor.getViewCount())
); );
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
@ -112,7 +124,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} }
}); });
VideoHelpers.handleNewVideo(info, entry.getPublishedDate().getTime(), null); VideoHelpers.handleNewVideo(extractor, entry.getPublishedDate().getTime(), null);
} catch (Exception e) { } catch (Exception e) {
ExceptionHandler.handle(e); ExceptionHandler.handle(e);
} }

View File

@ -6,9 +6,12 @@ import me.kavin.piped.consts.Constants;
import me.kavin.piped.utils.obj.db.Video; import me.kavin.piped.utils.obj.db.Video;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.StatelessSession; import org.hibernate.StatelessSession;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream.StreamExtractor;
import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.extractor.stream.StreamInfo;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.util.Optional;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -63,6 +66,40 @@ public class VideoHelpers {
} }
} }
public static void handleNewVideo(StreamExtractor extractor, long time, me.kavin.piped.utils.obj.db.Channel channel) throws ParsingException {
if (channel == null)
channel = DatabaseHelper.getChannelFromId(
extractor.getUploaderUrl().substring("https://www.youtube.com/channel/".length()));
long infoTime = Optional.ofNullable(extractor.getUploadDate())
.map(date -> date.offsetDateTime().toInstant().toEpochMilli())
.orElseGet(System::currentTimeMillis);
if (channel != null
&& (System.currentTimeMillis() - infoTime) < TimeUnit.DAYS.toMillis(Constants.FEED_RETENTION)) {
try (StatelessSession s = DatabaseSessionFactory.createStatelessSession()) {
if (!DatabaseHelper.doesVideoExist(s, extractor.getId())) {
Video video = new Video(extractor.getId(), extractor.getName(), extractor.getViewCount(), extractor.getLength(),
Math.max(infoTime, time), extractor.getThumbnailUrl(), extractor.isShortFormContent(), channel);
var tr = s.beginTransaction();
try {
s.insert(video);
tr.commit();
} catch (Exception e) {
tr.rollback();
ExceptionHandler.handle(e);
}
}
}
}
}
public static boolean isShort(String videoId) throws Exception { public static boolean isShort(String videoId) throws Exception {
final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder( final byte[] body = JsonWriter.string(prepareDesktopJsonBuilder(