Skip to content

Commit

Permalink
feat: upgrade the bilibili api
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 3, 2023
1 parent 51a7044 commit c2c922c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 64 deletions.
2 changes: 1 addition & 1 deletion middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Redis } from "@upstash/redis";
import type { NextFetchEvent, NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { activateLicenseKey, validateLicenseKey } from "./utils/3rd/lemon";
import { validateLicenseKey } from "./utils/3rd/lemon";
import { checkOpenaiApiKey } from "./utils/3rd/openai";
import { ratelimit } from "./utils/3rd/upstash";
import { isDev } from "./utils/env";
Expand Down
86 changes: 41 additions & 45 deletions pages/api/summarize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,49 @@ export default async function handler(
if (!bvId) {
return new Response("No bvid in the request", { status: 500 });
}
try {
const { title, subtitles } = await fetchSubtitle(bvId);
if (!subtitles) {
console.error("No subtitle in the video: ", bvId);
return new Response("No subtitle in the video", { status: 501 });
}
// @ts-ignore
const transcripts = subtitles.body.map((item, index) => {
return {
text: `${item.from}: ${item.content}`,
index,
};
});
// console.log("========transcripts========", transcripts);
const text = getChunckedTranscripts(transcripts, transcripts);
const prompt = getSummaryPrompt(title, text, true);
const { title, subtitles } = await fetchSubtitle(bvId);
if (!subtitles) {
console.error("No subtitle in the video: ", bvId);
return new Response("No subtitle in the video", { status: 501 });
}
// @ts-ignore
const transcripts = subtitles.body.map((item, index) => {
return {
text: `${item.from}: ${item.content}`,
index,
};
});
// console.log("========transcripts========", transcripts);
const text = getChunckedTranscripts(transcripts, transcripts);
const prompt = getSummaryPrompt(title, text, true);

try {
apiKey && console.log("========use user apiKey========");
isDev && console.log("prompt", prompt);
const payload = {
model: "gpt-3.5-turbo",
messages: [{ role: "user" as const, content: prompt }],
temperature: 0.5,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: apiKey ? 400 : 300,
stream: false,
n: 1,
};
try {
apiKey && console.log("========use user apiKey========");
isDev && console.log("prompt", prompt);
const payload = {
model: "gpt-3.5-turbo",
messages: [{ role: "user" as const, content: prompt }],
temperature: 0.5,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
max_tokens: apiKey ? 400 : 300,
stream: false,
n: 1,
};

const result = await OpenAIResult(payload, apiKey);
// TODO: add better logging when dev or prod
console.log("result", result);
const redis = Redis.fromEnv();
const data = await redis.set(bvId, result);
console.log(`bvId ${bvId} cached:`, data);
const result = await OpenAIResult(payload, apiKey);
// TODO: add better logging when dev or prod
console.log("result", result);
const redis = Redis.fromEnv();
const data = await redis.set(bvId, result);
console.log(`bvId ${bvId} cached:`, data);

return NextResponse.json(result);
} catch (error: any) {
console.log("API error", error, error.message);
return NextResponse.json({
errorMessage: error.message,
});
}
} catch (e) {
return new Response("No subtitle in the video", { status: 501 });
return NextResponse.json(result);
} catch (error: any) {
console.log("API error", error, error.message);
return NextResponse.json({
errorMessage: error.message,
});
}
}
42 changes: 24 additions & 18 deletions utils/3rd/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
import pRetry from "p-retry";

const run = async (bvId: string) => {
const requestUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvId}`;
console.log(`fetch`, requestUrl);
const headers = {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
Host: "api.bilibili.com",
Cookie: `SESSDATA=${process.env.BILIBILI_SESSION_TOKEN}`,
};
const response = await fetch(requestUrl, {
method: "GET",
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
headers,
referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
});
const json = await response.json();
const subtitleList = json.data?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
throw new Error("no subtitle");
}

return json;
// return json.data.View;
return json.data;
};

export async function fetchSubtitle(bvId: string) {
const res = await pRetry(() => run(bvId), {
onFailedAttempt: (error) => {
console.log(
`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
);
},
retries: 2,
});
// const res = await pRetry(async () => await run(bvId), {
// onFailedAttempt: (error) => {
// console.log(
// `Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`
// );
// },
// retries: 2,
// });
// @ts-ignore
const title = res.data?.title;
const subtitleList = res.data?.subtitle?.list;
const res = await run(bvId);
const title = res?.title;
const subtitleList = res?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
return { title, subtitles: null };
}
Expand Down

1 comment on commit c2c922c

@vercel
Copy link

@vercel vercel bot commented on c2c922c Mar 3, 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.