Skip to content

Commit

Permalink
refactor: just update the structure
Browse files Browse the repository at this point in the history
  • Loading branch information
JimmyLv committed Mar 7, 2023
1 parent 244b77c commit bb0fd6c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 33 deletions.
24 changes: 24 additions & 0 deletions lib/bilibili/fetchBilibiliSubtitleUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { sample } from "~/utils/fp";

export const fetchBilibiliSubtitleUrls = async (bvId: string) => {
const requestUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvId}`;
console.log(`fetch`, requestUrl, process.env.BILIBILI_SESSION_TOKEN);
const sessdata = sample(process.env.BILIBILI_SESSION_TOKEN?.split(","));
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=${sessdata}`
};
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();
// return json.data.View;
return json.data;
};
35 changes: 5 additions & 30 deletions lib/bilibili.ts → lib/fetchSubtitle.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,15 @@
import { VideoService } from "~/lib/types";
import {
fetchYoutubeSubtitle,
SUBTITLE_DOWNLOADER_URL,
} from "~/lib/youtube/fetchYoutubeSubtitle";
import { find, sample } from "~/utils/fp";

const fetchBilibiliSubtitles = async (bvId: string) => {
const requestUrl = `https://api.bilibili.com/x/web-interface/view?bvid=${bvId}`;
console.log(`fetch`, requestUrl, process.env.BILIBILI_SESSION_TOKEN);
const sessdata = sample(process.env.BILIBILI_SESSION_TOKEN?.split(","));
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=${sessdata}`,
};
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();
// return json.data.View;
return json.data;
};
import { fetchYoutubeSubtitleUrls, SUBTITLE_DOWNLOADER_URL } from "~/lib/youtube/fetchYoutubeSubtitleUrls";
import { find } from "~/utils/fp";
import { fetchBilibiliSubtitleUrls } from "./bilibili/fetchBilibiliSubtitleUrls";

export async function fetchSubtitle(
videoId: string,
service?: VideoService,
shouldShowTimestamp?: boolean
) {
if (service === VideoService.Youtube) {
const { title, subtitleList } = await fetchYoutubeSubtitle(videoId);
const { title, subtitleList } = await fetchYoutubeSubtitleUrls(videoId);
if (subtitleList?.length <= 0) {
return { title, subtitleList: null };
}
Expand Down Expand Up @@ -75,7 +50,7 @@ export async function fetchSubtitle(
// retries: 2,
// });
// @ts-ignore
const res = await fetchBilibiliSubtitles(videoId);
const res = await fetchBilibiliSubtitleUrls(videoId);
const title = res?.title;
const subtitleList = res?.subtitle?.list;
if (!subtitleList || subtitleList?.length < 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const SUBTITLE_DOWNLOADER_URL = "https://savesubs.com";
export async function fetchYoutubeSubtitle(videoId: string) {
export async function fetchYoutubeSubtitleUrls(videoId: string) {
const response = await fetch(SUBTITLE_DOWNLOADER_URL + "/action/extract", {
method: "POST",
body: JSON.stringify({
Expand Down
8 changes: 6 additions & 2 deletions pages/api/summarize.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 { fetchSubtitle } from "~/lib/bilibili";
import { fetchSubtitle } from "~/lib/fetchSubtitle";
import { OpenAIResult } from "~/lib/openai/OpenAIResult";
import { getChunckedTranscripts, getSummaryPrompt } from "~/lib/openai/prompt";
import { selectApiKeyAndActivatedLicenseKey } from "~/lib/openai/selectApiKeyAndActivatedLicenseKey";
Expand All @@ -27,7 +27,11 @@ export default async function handler(
if (!videoId) {
return new Response("No videoId in the request", { status: 500 });
}
const { title, subtitles } = await fetchSubtitle(videoId, service, shouldShowTimestamp);
const { title, subtitles } = await fetchSubtitle(
videoId,
service,
shouldShowTimestamp
);
if (!subtitles) {
console.error("No subtitle in the video: ", videoId);
return new Response("No subtitle in the video", { status: 501 });
Expand Down

1 comment on commit bb0fd6c

@vercel
Copy link

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