Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

feat: retry summarize video if it fails, max 3 times

Christian Feie7a271479077
diff --git a/lib/retry-promise.js b/lib/retry-promise.js
new file mode 100644
index 0000000..687e52e
--- /dev/null
+++ b/lib/retry-promise.js
@@ -0,0 +1,19 @@
+export default function retry (fnPromise, retries = 3) {
+ return new Promise((resolve, reject) => {
+ const attempt = (n) => {
+ fnPromise()
+ .then(resolve)
+ .catch(async (error) => {
+ if (n === 1) {
+ console.error('All attempts failed', error)
+ reject(error)
+ } else {
+ console.log(`Retrying... (${n - 1} attempts left)`)
+ await new Promise(resolve => setTimeout(resolve, 3000))
+ attempt(n - 1)
+ }
+ })
+ }
+ attempt(retries)
+ })
+}
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index 1e7d612..6c140a7 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -1,10 +1,11 @@
/* eslint-disable no-async-promise-executor */
import fs from 'fs'
-import { getVideoSubtitles } from '../lib/youtube.js'
-import * as llm from '../lib/llm/index.js'
+import { getVideoSubtitles } from './youtube.js'
+import * as llm from './llm/index.js'
+import retry from './retry-promise.js'
export async function summarizeVideo (id, repo, llmSettings = {}, callback = () => {}) {
- return new Promise(async (resolve, reject) => {
+ return retry(() => new Promise(async (resolve, reject) => {
try {
const transcript = await getVideoSubtitles(id, callback)
let cleanedTranscript = cleanTranscript(transcript)
@@ -29,7 +30,7 @@ Do not describe or mention the video itself. Simply summarize the points it make
console.error('Error summarizing video', id, error.message)
reject(error)
}
- })
+ }))
}
export function cleanTranscript (transcript) {
const lines = []