diff options
author | Tony Tam <149837+tonytamsf@users.noreply.github.com> | 2024-01-01 04:06:00 -0800 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2024-05-18 18:58:01 +0200 |
commit | 27e9bf36b1696ea2f35cc342964e9659087a5948 (patch) | |
tree | 5d031cfcd8b14ac0835b8521d5262bf9d8367c4f | |
parent | 8adbad9b6692ea41136212f5a2e189a49e6169f0 (diff) | |
download | AntennaPod-27e9bf36b1696ea2f35cc342964e9659087a5948.zip |
Download and store transcript text (#6797)
5 files changed, 90 insertions, 0 deletions
diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedItem.java b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedItem.java index bc95dc85f..0f5a3f4bb 100644 --- a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedItem.java +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedItem.java @@ -45,6 +45,7 @@ public class FeedItem implements Serializable { private String podcastIndexChapterUrl; private String podcastIndexTranscriptUrl; private String podcastIndexTranscriptType; + private String podcastIndexTranscriptText; private int state; public static final int NEW = -1; @@ -462,6 +463,14 @@ public class FeedItem implements Serializable { } } + public String getPodcastIndexTranscriptText() { + return podcastIndexTranscriptText; + } + + public String setPodcastIndexTranscriptText(String str) { + return podcastIndexTranscriptText = str; + } + @NonNull @Override public String toString() { diff --git a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java index 02c221611..76a891c15 100644 --- a/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java +++ b/model/src/main/java/de/danoeh/antennapod/model/feed/FeedMedia.java @@ -513,4 +513,11 @@ public class FeedMedia implements Playable { } return super.equals(o); } + + public String getTranscriptFileUrl() { + if (getLocalFileUrl() == null) { + return null; + } + return getLocalFileUrl() + ".transcript"; + } } diff --git a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/episode/MediaDownloadedHandler.java b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/episode/MediaDownloadedHandler.java index cf9ec17e1..b856d1b67 100644 --- a/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/episode/MediaDownloadedHandler.java +++ b/net/download/service/src/main/java/de/danoeh/antennapod/net/download/service/episode/MediaDownloadedHandler.java @@ -10,6 +10,8 @@ import de.danoeh.antennapod.model.MediaMetadataRetrieverCompat; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.net.sync.serviceinterface.SynchronizationQueueSink; import de.danoeh.antennapod.ui.chapters.ChapterUtils; +import de.danoeh.antennapod.ui.chapters.PodcastIndexTranscriptUtils; +import org.apache.commons.lang3.StringUtils; import org.greenrobot.eventbus.EventBus; import java.io.File; @@ -64,6 +66,15 @@ public class MediaDownloadedHandler implements Runnable { if (media.getItem() != null && media.getItem().getPodcastIndexChapterUrl() != null) { ChapterUtils.loadChaptersFromUrl(media.getItem().getPodcastIndexChapterUrl(), false); } + FeedItem item = media.getItem(); + if (item != null && item.getPodcastIndexTranscriptUrl() != null) { + String transcript = PodcastIndexTranscriptUtils.loadTranscriptFromUrl( + item.getPodcastIndexTranscriptType(), item.getPodcastIndexTranscriptUrl(), false); + if (!StringUtils.isEmpty(transcript)) { + item.setPodcastIndexTranscriptText(transcript); + PodcastIndexTranscriptUtils.storeTranscript(media, transcript); + } + } } catch (InterruptedIOException ignore) { // Ignore } diff --git a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java index 11e1ad751..2106eae39 100644 --- a/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java +++ b/storage/database/src/main/java/de/danoeh/antennapod/storage/database/DBWriter.java @@ -119,6 +119,14 @@ public class DBWriter { media.setLocalFileUrl(null); localDelete = true; } else if (media.getLocalFileUrl() != null) { + // delete transcript file before the media file because the fileurl is needed + if (media.getTranscriptFileUrl() != null) { + File transcriptFile = new File(media.getTranscriptFileUrl()); + if (transcriptFile.exists() && !transcriptFile.delete()) { + Log.d(TAG, "Deletion of transcript file failed."); + } + } + // delete downloaded media file File mediaFile = new File(media.getLocalFileUrl()); if (mediaFile.exists() && !mediaFile.delete()) { diff --git a/ui/chapters/src/main/java/de/danoeh/antennapod/ui/chapters/PodcastIndexTranscriptUtils.java b/ui/chapters/src/main/java/de/danoeh/antennapod/ui/chapters/PodcastIndexTranscriptUtils.java new file mode 100644 index 000000000..9add3db23 --- /dev/null +++ b/ui/chapters/src/main/java/de/danoeh/antennapod/ui/chapters/PodcastIndexTranscriptUtils.java @@ -0,0 +1,55 @@ +package de.danoeh.antennapod.ui.chapters; + +import android.util.Log; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.Charset; + +import de.danoeh.antennapod.model.feed.FeedMedia; +import de.danoeh.antennapod.net.common.AntennapodHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.apache.commons.io.IOUtils; + +public class PodcastIndexTranscriptUtils { + + private static final String TAG = "PodcastIndexTranscript"; + + public static String loadTranscriptFromUrl(String type, String url, boolean forceRefresh) { + StringBuilder str = new StringBuilder(); + Response response = null; + try { + Log.d(TAG, "Downloading transcript URL " + url.toString()); + Request request = new Request.Builder().url(url).build(); + response = AntennapodHttpClient.getHttpClient().newCall(request).execute(); + if (response.isSuccessful() && response.body() != null) { + str.append(response.body().string()); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (response != null) { + response.close(); + } + } + return str.toString(); + } + + public static void storeTranscript(FeedMedia media, String transcript) { + File transcriptFile = new File(media.getTranscriptFileUrl()); + FileOutputStream ostream = null; + try { + if (!transcriptFile.exists() && transcriptFile.createNewFile()) { + ostream = new FileOutputStream(transcriptFile); + ostream.write(transcript.getBytes(Charset.forName("UTF-8"))); + ostream.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(ostream); + } + } +} |