Skip to content

Commit

Permalink
fix: not prompt with timestamp when not selected
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 8, 2023
1 parent aa3fb66 commit c5d4f43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
23 changes: 12 additions & 11 deletions lib/bilibili/fetchBilibiliSubtitle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import { fetchBilibiliSubtitleUrls } from "~/lib/bilibili/fetchBilibiliSubtitleUrls";
import { reduceBilibiliSubtitleTimestamp } from "~/utils/reduceSubtitleTimestamp";

export async function fetchBilibiliSubtitle(videoId: string) {
// const res = await pRetry(async () => await fetchBilibiliSubtitles(videoId), {
// onFailedAttempt: (error) => {
// console.log(
// `Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
// );
// },
// retries: 2,
// });
// @ts-ignore
export async function fetchBilibiliSubtitle(videoId: string, shouldShowTimestamp?: boolean) {
const res = await fetchBilibiliSubtitleUrls(videoId);
const { title, desc, dynamic } = res || {};
const descriptionText = desc + "\n" + dynamic;
Expand All @@ -27,6 +18,16 @@ export async function fetchBilibiliSubtitle(videoId: string) {

const subtitleResponse = await fetch(subtitleUrl);
const subtitles = await subtitleResponse.json();
const transcripts = reduceBilibiliSubtitleTimestamp(subtitles?.body);
const transcripts = reduceBilibiliSubtitleTimestamp(subtitles?.body, shouldShowTimestamp);
return { title, subtitlesArray: transcripts, descriptionText };
}

// const res = await pRetry(async () => await fetchBilibiliSubtitles(videoId), {
// onFailedAttempt: (error) => {
// console.log(
// `Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
// );
// },
// retries: 2,
// });
// @ts-ignore
2 changes: 1 addition & 1 deletion lib/fetchSubtitle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export async function fetchSubtitle(
if (service === VideoService.Youtube) {
return await fetchYoutubeSubtitle(videoId, shouldShowTimestamp);
}
return await fetchBilibiliSubtitle(videoId);
return await fetchBilibiliSubtitle(videoId, shouldShowTimestamp);
}
14 changes: 9 additions & 5 deletions utils/reduceSubtitleTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ export function reduceYoutubeSubtitleTimestamp(
return reduceSubtitleTimestamp<YoutubeSubtitleItem>(
subtitles,
(i) => i.start,
(i) => i.lines.join(" ")
(i) => i.lines.join(" "),
true
);
}

export function reduceBilibiliSubtitleTimestamp(
subtitles: Array<BilibiliSubtitleItem> = []
subtitles: Array<BilibiliSubtitleItem> = [],
shouldShowTimestamp?: boolean
): Array<CommonSubtitleItem> {
return reduceSubtitleTimestamp<BilibiliSubtitleItem>(
subtitles,
(i) => i.from,
(i) => i.content
(i) => i.content,
shouldShowTimestamp
);
}
export function reduceSubtitleTimestamp<T>(
subtitles: Array<T> = [],
getStart: (i: T) => number,
getText: (i: T) => string
getText: (i: T) => string,
shouldShowTimestamp?: boolean
): Array<CommonSubtitleItem> {
// 把字幕数组总共分成 20 组
const TOTAL_GROUP_COUNT = 30;
Expand All @@ -47,7 +51,7 @@ export function reduceSubtitleTimestamp<T>(
accumulator[groupIndex] = {
// 5.88 -> 5.9
// text: current.start.toFixed() + ": ",
text: getStart(current) + " - ",
text: shouldShowTimestamp ? getStart(current) + " - " : "",
index: groupIndex,
};
}
Expand Down

1 comment on commit c5d4f43

@vercel
Copy link

@vercel vercel bot commented on c5d4f43 Mar 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.