Update scrape.ts
This commit is contained in:
parent
23b2190e5d
commit
5b8aed26dd
@ -12,10 +12,15 @@ export async function scrapeHelper(
|
|||||||
team_id: string,
|
team_id: string,
|
||||||
crawlerOptions: any,
|
crawlerOptions: any,
|
||||||
pageOptions: any
|
pageOptions: any
|
||||||
) : Promise<{ success: boolean; error?: string; data?: Document }> {
|
): Promise<{
|
||||||
|
success: boolean;
|
||||||
|
error?: string;
|
||||||
|
data?: Document;
|
||||||
|
returnCode?: number;
|
||||||
|
}> {
|
||||||
const url = req.body.url;
|
const url = req.body.url;
|
||||||
if (!url) {
|
if (!url) {
|
||||||
throw new Error("Url is required");
|
return { success: false, error: "Url is required", returnCode: 400 };
|
||||||
}
|
}
|
||||||
|
|
||||||
const a = new WebScraperDataProvider();
|
const a = new WebScraperDataProvider();
|
||||||
@ -34,7 +39,7 @@ export async function scrapeHelper(
|
|||||||
(doc: { content?: string }) => doc.content && doc.content.trim().length > 0
|
(doc: { content?: string }) => doc.content && doc.content.trim().length > 0
|
||||||
);
|
);
|
||||||
if (filteredDocs.length === 0) {
|
if (filteredDocs.length === 0) {
|
||||||
return { success: true, error: "No pages found" };
|
return { success: true, error: "No page found", returnCode: 200 };
|
||||||
}
|
}
|
||||||
const { success, credit_usage } = await billTeam(
|
const { success, credit_usage } = await billTeam(
|
||||||
team_id,
|
team_id,
|
||||||
@ -43,12 +48,15 @@ export async function scrapeHelper(
|
|||||||
if (!success) {
|
if (!success) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
error: "Failed to bill team. Insufficient credits or subscription not found.",
|
error:
|
||||||
|
"Failed to bill team. Insufficient credits or subscription not found.",
|
||||||
|
returnCode: 402,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
data: filteredDocs[0],
|
data: filteredDocs[0],
|
||||||
|
returnCode: 200,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,8 +94,8 @@ export async function scrapeController(req: Request, res: Response) {
|
|||||||
logJob({
|
logJob({
|
||||||
success: result.success,
|
success: result.success,
|
||||||
message: result.error,
|
message: result.error,
|
||||||
num_docs: result.data.length,
|
num_docs: 1,
|
||||||
docs: result.data,
|
docs: [result.data],
|
||||||
time_taken: 0,
|
time_taken: 0,
|
||||||
team_id: team_id,
|
team_id: team_id,
|
||||||
mode: "scrape",
|
mode: "scrape",
|
||||||
@ -96,7 +104,6 @@ export async function scrapeController(req: Request, res: Response) {
|
|||||||
pageOptions: pageOptions,
|
pageOptions: pageOptions,
|
||||||
});
|
});
|
||||||
return res.json(result);
|
return res.json(result);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return res.status(500).json({ error: error.message });
|
return res.status(500).json({ error: error.message });
|
||||||
|
Loading…
Reference in New Issue
Block a user