Download and store transcript text (#6797)

This commit is contained in:
Tony Tam 2024-01-01 04:06:00 -08:00 committed by ByteHamster
parent 8adbad9b66
commit 27e9bf36b1
5 changed files with 90 additions and 0 deletions

View File

@ -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() {

View File

@ -513,4 +513,11 @@ public class FeedMedia implements Playable {
}
return super.equals(o);
}
public String getTranscriptFileUrl() {
if (getLocalFileUrl() == null) {
return null;
}
return getLocalFileUrl() + ".transcript";
}
}

View File

@ -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
}

View File

@ -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()) {

View File

@ -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);
}
}
}