From 85411fea2ed8b170a8e545cf454be8119d64da9f Mon Sep 17 00:00:00 2001 From: 0xboobface <0xboobface@gmail.com> Date: Sat, 20 Apr 2019 14:39:44 +0200 Subject: [PATCH] Handle empty playlists more gracefully Check, if a playlist is empty, before parsing it with open-m3u8. If it is empty, return an empty SegmentPlaylist instead of throwing a ParseException --- .../download/AbstractHlsDownload.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/ctbrec/recorder/download/AbstractHlsDownload.java b/common/src/main/java/ctbrec/recorder/download/AbstractHlsDownload.java index 1646794e..c6763d24 100644 --- a/common/src/main/java/ctbrec/recorder/download/AbstractHlsDownload.java +++ b/common/src/main/java/ctbrec/recorder/download/AbstractHlsDownload.java @@ -1,5 +1,6 @@ package ctbrec.recorder.download; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -33,7 +34,6 @@ import ctbrec.Config; import ctbrec.Model; import ctbrec.io.HttpClient; import ctbrec.io.HttpException; -import ctbrec.sites.fc2live.Fc2Live; import okhttp3.Request; import okhttp3.Response; @@ -53,28 +53,31 @@ public abstract class AbstractHlsDownload implements Download { this.client = client; } - protected SegmentPlaylist getNextSegments(String segments) throws IOException, ParseException, PlaylistException { - URL segmentsUrl = new URL(segments); + protected SegmentPlaylist getNextSegments(String segmentsURL) throws IOException, ParseException, PlaylistException { + URL segmentsUrl = new URL(segmentsURL); Request request = new Request.Builder() .url(segmentsUrl) .header("Accept", "*/*") .header("Accept-Language", "en-US,en;q=0.5") .header("User-Agent", Config.getInstance().getSettings().httpUserAgent) - .header("Origin", Fc2Live.BASE_URL) - .header("Referer", Fc2Live.BASE_URL) + .header("Origin", model.getSite().getBaseUrl()) + .header("Referer", model.getSite().getBaseUrl()) .header("Connection", "keep-alive") .build(); try(Response response = client.execute(request)) { if(response.isSuccessful()) { - // String body = response.body().string(); - // InputStream inputStream = new ByteArrayInputStream(body.getBytes("utf-8")); - // LOG.debug("Segments {}", body); - InputStream inputStream = response.body().byteStream(); + String body = response.body().string(); + if(!body.contains("#EXT-X-STREAM-INF")) { + // no segments, empty playlist + return new SegmentPlaylist(segmentsURL); + } + + InputStream inputStream = new ByteArrayInputStream(body.getBytes("utf-8")); PlaylistParser parser = new PlaylistParser(inputStream, Format.EXT_M3U, Encoding.UTF_8, ParsingMode.LENIENT); Playlist playlist = parser.parse(); if(playlist.hasMediaPlaylist()) { MediaPlaylist mediaPlaylist = playlist.getMediaPlaylist(); - SegmentPlaylist lsp = new SegmentPlaylist(segments); + SegmentPlaylist lsp = new SegmentPlaylist(segmentsURL); lsp.seq = mediaPlaylist.getMediaSequenceNumber(); lsp.targetDuration = mediaPlaylist.getTargetDuration(); List tracks = mediaPlaylist.getTracks();