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

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