mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 10:52:08 +01:00
Refactor transcript fetching (#5)
* refactor: share transcript fetch helper * Update lib/fetchTranscript.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update lib/fetchTranscript.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
32
lib/fetchTranscript.ts
Normal file
32
lib/fetchTranscript.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Fetches transcript content from a URL with optional authentication
|
||||||
|
* @param url The URL to fetch the transcript from
|
||||||
|
* @param username Optional username for Basic Auth
|
||||||
|
* @param password Optional password for Basic Auth
|
||||||
|
* @returns The transcript content or null if fetching fails
|
||||||
|
*/
|
||||||
|
export async function fetchTranscriptContent(
|
||||||
|
url: string,
|
||||||
|
username?: string,
|
||||||
|
password?: string
|
||||||
|
): Promise<string | null> {
|
||||||
|
try {
|
||||||
|
const authHeader =
|
||||||
|
username && password
|
||||||
|
? "Basic " + Buffer.from(`${username}:${password}`).toString("base64")
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: authHeader ? { Authorization: authHeader } : {},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
process.stderr.write(`Error fetching transcript from ${url}: ${response.statusText}\n`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return await response.text();
|
||||||
|
} catch (error) {
|
||||||
|
process.stderr.write(`Failed to fetch transcript from ${url}: ${error}\n`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@
|
|||||||
import cron from "node-cron";
|
import cron from "node-cron";
|
||||||
import { prisma } from "./prisma";
|
import { prisma } from "./prisma";
|
||||||
import { fetchAndParseCsv } from "./csvFetcher";
|
import { fetchAndParseCsv } from "./csvFetcher";
|
||||||
|
import { fetchTranscriptContent } from "./fetchTranscript";
|
||||||
|
|
||||||
interface SessionCreateData {
|
interface SessionCreateData {
|
||||||
id: string;
|
id: string;
|
||||||
@ -10,41 +11,6 @@ interface SessionCreateData {
|
|||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches transcript content from a URL with optional authentication
|
|
||||||
* @param url The URL to fetch the transcript from
|
|
||||||
* @param username Optional username for Basic Auth
|
|
||||||
* @param password Optional password for Basic Auth
|
|
||||||
* @returns The transcript content or null if fetching fails
|
|
||||||
*/
|
|
||||||
async function fetchTranscriptContent(
|
|
||||||
url: string,
|
|
||||||
username?: string,
|
|
||||||
password?: string
|
|
||||||
): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const authHeader =
|
|
||||||
username && password
|
|
||||||
? "Basic " + Buffer.from(`${username}:${password}`).toString("base64")
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
|
||||||
headers: authHeader ? { Authorization: authHeader } : {},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
process.stderr.write(
|
|
||||||
`Error fetching transcript: ${response.statusText}\n`
|
|
||||||
);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return await response.text();
|
|
||||||
} catch (error) {
|
|
||||||
process.stderr.write(`Failed to fetch transcript: ${error}\n`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function startScheduler() {
|
export function startScheduler() {
|
||||||
cron.schedule("*/15 * * * *", async () => {
|
cron.schedule("*/15 * * * *", async () => {
|
||||||
const companies = await prisma.company.findMany();
|
const companies = await prisma.company.findMany();
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { fetchAndParseCsv } from "../../../lib/csvFetcher";
|
import { fetchAndParseCsv } from "../../../lib/csvFetcher";
|
||||||
import { prisma } from "../../../lib/prisma";
|
import { prisma } from "../../../lib/prisma";
|
||||||
|
import { fetchTranscriptContent } from "../../../lib/fetchTranscript";
|
||||||
|
|
||||||
interface SessionCreateData {
|
interface SessionCreateData {
|
||||||
id: string;
|
id: string;
|
||||||
@ -11,41 +12,6 @@ interface SessionCreateData {
|
|||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches transcript content from a URL with optional authentication
|
|
||||||
* @param url The URL to fetch the transcript from
|
|
||||||
* @param username Optional username for Basic Auth
|
|
||||||
* @param password Optional password for Basic Auth
|
|
||||||
* @returns The transcript content or null if fetching fails
|
|
||||||
*/
|
|
||||||
async function fetchTranscriptContent(
|
|
||||||
url: string,
|
|
||||||
username?: string,
|
|
||||||
password?: string
|
|
||||||
): Promise<string | null> {
|
|
||||||
try {
|
|
||||||
const authHeader =
|
|
||||||
username && password
|
|
||||||
? "Basic " + Buffer.from(`${username}:${password}`).toString("base64")
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
|
||||||
headers: authHeader ? { Authorization: authHeader } : {},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
process.stderr.write(
|
|
||||||
`Error fetching transcript: ${response.statusText}\n`
|
|
||||||
);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return await response.text();
|
|
||||||
} catch (error) {
|
|
||||||
process.stderr.write(`Failed to fetch transcript: ${error}\n`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
res: NextApiResponse
|
res: NextApiResponse
|
||||||
|
|||||||
Reference in New Issue
Block a user