11import axios from 'axios' ;
2+ import handleError from './utils/handleError.js'
23
34/**
45 * Scrape and extract structured data from a webpage using ScrapeGraph AI.
@@ -8,13 +9,14 @@ import axios from 'axios';
89 * @param {string } prompt - Natural language prompt describing what data to extract
910 * @param {Object } [schema] - Optional schema object defining the output structure
1011 * @returns {Promise<string> } Extracted data in JSON format matching the provided schema
12+ * @throws - Will throw an error in case of an HTTP failure.
1113 */
1214export async function smartScraper ( apiKey , url , prompt , schema = null ) {
13- const endpoint = " https://api.scrapegraphai.com/v1/smartscraper" ;
15+ const endpoint = ' https://api.scrapegraphai.com/v1/smartscraper' ;
1416 const headers = {
15- " accept" : " application/json" ,
16- " SGAI-APIKEY" : apiKey ,
17- " Content-Type" : " application/json"
17+ ' accept' : ' application/json' ,
18+ ' SGAI-APIKEY' : apiKey ,
19+ ' Content-Type' : ' application/json'
1820 } ;
1921
2022 const payload = {
@@ -24,72 +26,39 @@ export async function smartScraper(apiKey, url, prompt, schema = null) {
2426
2527 if ( schema ) {
2628 payload . output_schema = {
27- description : schema . title || " Schema" ,
28- name : schema . title || " Schema" ,
29+ description : schema . title || ' Schema' ,
30+ name : schema . title || ' Schema' ,
2931 properties : schema . properties || { } ,
3032 required : schema . required || [ ]
3133 } ;
3234 }
3335
3436 try {
3537 const response = await axios . post ( endpoint , payload , { headers } ) ;
36- return JSON . stringify ( response . data ) ;
38+ return response . data ;
3739 } catch ( error ) {
38- if ( error . response ) {
39- if ( error . response . status === 403 ) {
40- return JSON . stringify ( {
41- error : "Access forbidden (403)" ,
42- message : "You do not have permission to access this resource."
43- } ) ;
44- }
45- return JSON . stringify ( {
46- error : "HTTP error occurred" ,
47- message : error . message ,
48- status_code : error . response . status
49- } ) ;
50- }
51- return JSON . stringify ( {
52- error : "An error occurred" ,
53- message : error . message
54- } ) ;
40+ handleError ( error )
5541 }
5642}
5743
5844/**
59- * Retrieve the status or the result of a scraping request. It also allows you to see the result of old requests.
45+ * Retrieve the status or the result of a smartScraper request. It also allows you to see the result of old requests.
6046 *
6147 * @param {string } apiKey - Your ScrapeGraph AI API key
62- * @param {string } requestId - The request ID associated with the feedback
48+ * @param {string } requestId - The request ID associated with the output of a smartScraper request.
6349 * @returns {Promise<string> } Information related to the status or result of a scraping request.
6450 */
65- export async function smartScraperInfo ( apiKey , requestId ) {
66- const endpoint = " https://api.scrapegraphai.com/v1/smartscraper/" + requestId ;
51+ export async function getSmartScraperRequest ( apiKey , requestId ) {
52+ const endpoint = ' https://api.scrapegraphai.com/v1/smartscraper/' + requestId ;
6753 const headers = {
68- " accept" : " application/json" ,
69- " SGAI-APIKEY" : apiKey ,
54+ ' accept' : ' application/json' ,
55+ ' SGAI-APIKEY' : apiKey ,
7056 } ;
7157
7258 try {
7359 const response = await axios . get ( endpoint , { headers } ) ;
74- return JSON . stringify ( response . data )
60+ return response . data ;
7561 } catch ( error ) {
76- if ( error . response ) {
77- if ( error . response . status === 403 ) {
78- return JSON . stringify ( {
79- error : "Access forbidden (403)" ,
80- message : "You do not have permission to access this resource."
81- } ) ;
82- }
83- return JSON . stringify ( {
84- error : "HTTP error occurred" ,
85- message : error . message ,
86- status_code : error . response . status
87- } ) ;
88- }
89- return JSON . stringify ( {
90- error : "An error occurred" ,
91- message : error . message
92- } ) ;
62+ handleError ( error )
9363 }
94-
9564}
0 commit comments