Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scrapegraph-js/examples/getCredits_example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getCredits } from "scrapegraph-sdk";
import { getCredits } from 'scrapegraph-sdk';
import 'dotenv/config';

try {
Expand Down
4 changes: 2 additions & 2 deletions scrapegraph-js/examples/getSmartScraperRequest_example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getSmartScraperRequest } from "scrapegraph-sdk";
import { getSmartScraperRequest } from 'scrapegraph-sdk';
import 'dotenv/config';

try {
const apiKey = process.env.SGAI_APIKEY;
const requestId = "3fa85f64-5717-4562-b3fc-2c963f66afa6"
const requestId = '3fa85f64-5717-4562-b3fc-2c963f66afa6'

const requestInfo = await getSmartScraperRequest(apiKey, requestId);

Expand Down
6 changes: 3 additions & 3 deletions scrapegraph-js/examples/sendFeedback_example.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { sendFeedback } from "scrapegraph-sdk";
import { sendFeedback } from 'scrapegraph-sdk';
import 'dotenv/config';

try {
const apiKey = process.env.SGAI_APIKEY;
const requestId = "16a63a80-c87f-4cde-b005-e6c3ecda278b";
const requestId = '16a63a80-c87f-4cde-b005-e6c3ecda278b';
const rating = 5;
const feedbackMessage = "This is a test feedback message.";
const feedbackMessage = 'This is a test feedback message.';

const feedback_response = await sendFeedback(apiKey, requestId, rating, feedbackMessage);
console.log(feedback_response);
Expand Down
6 changes: 3 additions & 3 deletions scrapegraph-js/examples/smartScraper_example.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { smartScraper } from "scrapegraph-sdk";
import { smartScraper } from 'scrapegraph-sdk';
import 'dotenv/config';

try {
const apiKey = process.env.SGAI_APIKEY;
const url = "https://scrapegraphai.com";
const prompt = "What does the company do?";
const url = 'https://scrapegraphai.com';
const prompt = 'What does the company do?';

const response = await smartScraper(apiKey, url, prompt);

Expand Down
23 changes: 7 additions & 16 deletions scrapegraph-js/src/credits.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
import axios from 'axios';
import handleError from './utils/handleError.js';

/**
* Retrieve credits from the API.
*
* @param {string} apiKey - Your ScrapeGraph AI API key
* @returns {Promise<string>} Response from the API in JSON format
*/
export async function credits(apiKey) {
const endpoint = "https://api.scrapegraphai.com/v1/credits";
export async function getCredits(apiKey) {
const endpoint = 'https://api.scrapegraphai.com/v1/credits';
const headers = {
"accept": "application/json",
"SGAI-APIKEY": apiKey
'accept': 'application/json',
'SGAI-APIKEY': apiKey
};

try {
const response = await axios.get(endpoint, { headers });
return JSON.stringify(response.data);
return response.data;
} catch (error) {
if (error.response) {
return JSON.stringify({
error: "HTTP error occurred",
message: error.message,
status_code: error.response.status
});
}
return JSON.stringify({
error: "An error occurred",
message: error.message
});
handleError(error)
}
}
27 changes: 9 additions & 18 deletions scrapegraph-js/src/feedback.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import axios from 'axios';
import handleError from './utils/handleError.js';

/**
* Send feedback to the API.
*
* @param {string} apiKey - Your ScrapeGraph AI API key
* @param {string} requestId - The request ID associated with the feedback
* @param {number} rating - The rating score
* @param {string} feedbackText - The feedback message to send
* @param {string} feedbackText - Optional feedback message to send
* @returns {Promise<string>} Response from the API in JSON format
*/
export async function feedback(apiKey, requestId, rating, feedbackText) {
const endpoint = "https://api.scrapegraphai.com/v1/feedback";
export async function sendFeedback(apiKey, requestId, rating, feedbackText = null) {
const endpoint = 'https://api.scrapegraphai.com/v1/feedback';
const headers = {
"accept": "application/json",
"SGAI-APIKEY": apiKey,
"Content-Type": "application/json"
'accept': 'application/json',
'SGAI-APIKEY': apiKey,
'Content-Type': 'application/json'
};

const feedbackData = {
Expand All @@ -25,18 +26,8 @@ export async function feedback(apiKey, requestId, rating, feedbackText) {

try {
const response = await axios.post(endpoint, feedbackData, { headers });
return JSON.stringify(response.data);
return response.data;
} catch (error) {
if (error.response) {
return JSON.stringify({
error: "HTTP error occurred",
message: error.message,
status_code: error.response.status
});
}
return JSON.stringify({
error: "An error occurred",
message: error.message
});
handleError(error);
}
}
67 changes: 18 additions & 49 deletions scrapegraph-js/src/smartScraper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios';
import handleError from './utils/handleError.js'

/**
* Scrape and extract structured data from a webpage using ScrapeGraph AI.
Expand All @@ -8,13 +9,14 @@ import axios from 'axios';
* @param {string} prompt - Natural language prompt describing what data to extract
* @param {Object} [schema] - Optional schema object defining the output structure
* @returns {Promise<string>} Extracted data in JSON format matching the provided schema
* @throws - Will throw an error in case of an HTTP failure.
*/
export async function smartScraper(apiKey, url, prompt, schema = null) {
const endpoint = "https://api.scrapegraphai.com/v1/smartscraper";
const endpoint = 'https://api.scrapegraphai.com/v1/smartscraper';
const headers = {
"accept": "application/json",
"SGAI-APIKEY": apiKey,
"Content-Type": "application/json"
'accept': 'application/json',
'SGAI-APIKEY': apiKey,
'Content-Type': 'application/json'
};

const payload = {
Expand All @@ -24,72 +26,39 @@ export async function smartScraper(apiKey, url, prompt, schema = null) {

if (schema) {
payload.output_schema = {
description: schema.title || "Schema",
name: schema.title || "Schema",
description: schema.title || 'Schema',
name: schema.title || 'Schema',
properties: schema.properties || {},
required: schema.required || []
};
}

try {
const response = await axios.post(endpoint, payload, { headers });
return JSON.stringify(response.data);
return response.data;
} catch (error) {
if (error.response) {
if (error.response.status === 403) {
return JSON.stringify({
error: "Access forbidden (403)",
message: "You do not have permission to access this resource."
});
}
return JSON.stringify({
error: "HTTP error occurred",
message: error.message,
status_code: error.response.status
});
}
return JSON.stringify({
error: "An error occurred",
message: error.message
});
handleError(error)
}
}

/**
* Retrieve the status or the result of a scraping request. It also allows you to see the result of old requests.
* Retrieve the status or the result of a smartScraper request. It also allows you to see the result of old requests.
*
* @param {string} apiKey - Your ScrapeGraph AI API key
* @param {string} requestId - The request ID associated with the feedback
* @param {string} requestId - The request ID associated with the output of a smartScraper request.
* @returns {Promise<string>} Information related to the status or result of a scraping request.
*/
export async function smartScraperInfo(apiKey, requestId) {
const endpoint = "https://api.scrapegraphai.com/v1/smartscraper/" + requestId;
export async function getSmartScraperRequest(apiKey, requestId) {
const endpoint = 'https://api.scrapegraphai.com/v1/smartscraper/' + requestId;
const headers = {
"accept": "application/json",
"SGAI-APIKEY": apiKey,
'accept': 'application/json',
'SGAI-APIKEY': apiKey,
};

try {
const response = await axios.get(endpoint, { headers });
return JSON.stringify(response.data)
return response.data;
} catch (error) {
if (error.response) {
if (error.response.status === 403) {
return JSON.stringify({
error: "Access forbidden (403)",
message: "You do not have permission to access this resource."
});
}
return JSON.stringify({
error: "HTTP error occurred",
message: error.message,
status_code: error.response.status
});
}
return JSON.stringify({
error: "An error occurred",
message: error.message
});
handleError(error)
}

}
40 changes: 40 additions & 0 deletions scrapegraph-js/src/utils/handleError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class HttpError extends Error {
constructor(statusCode, title, detail) {
super(HttpError.makeMessage(statusCode, title, detail));
this.statusCode = statusCode;
this.title = title;
this.detail = detail;
}

static makeMessage(statusCode, title, detail) {
let message = '';

message += statusCode ? `${statusCode} - ` : '(unknown status code) - ';
message += title ? `${title} - ` : '(unknown error message) - ';
message += detail ? `${JSON.stringify(detail)}` : '(unknown error detail)';

return message;
}
}

class NetworkError extends Error {
constructor(message) {
super(message);
}
}

class UnexpectedError extends Error {
constructor(message) {
super(message);
}
}

export default function handleError(error) {
if (error.response) {
throw new HttpError(error.response.status, error.response.statusText, error.response.data.detail)
} else if (error.request) {
throw new NetworkError('Impossible to contact the server. Check your internet connection.');
} else {
throw new UnexpectedError(`${error.message}`);
}
}
Loading