Skip to content

Commit 35a7a12

Browse files
committed
add refactoring of the library
1 parent 06f84c6 commit 35a7a12

31 files changed

+476
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

scrapegraph-js/credits.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import axios from 'axios';
2+
3+
/**
4+
* Retrieve credits from the API.
5+
*
6+
* @param {string} apiKey - Your ScrapeGraph AI API key
7+
* @returns {Promise<string>} Response from the API in JSON format
8+
*/
9+
export async function credits(apiKey) {
10+
const endpoint = "https://sgai-api.onrender.com/api/v1/credits";
11+
const headers = {
12+
"accept": "application/json",
13+
"SGAI-API-KEY": apiKey
14+
};
15+
16+
try {
17+
const response = await axios.get(endpoint, { headers });
18+
return JSON.stringify(response.data);
19+
} catch (error) {
20+
if (error.response) {
21+
return JSON.stringify({
22+
error: "HTTP error occurred",
23+
message: error.message,
24+
status_code: error.response.status
25+
});
26+
}
27+
return JSON.stringify({
28+
error: "An error occurred",
29+
message: error.message
30+
});
31+
}
32+
}

scrapegraph-js/feedback.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import axios from 'axios';
2+
3+
/**
4+
* Send feedback to the API.
5+
*
6+
* @param {string} apiKey - Your ScrapeGraph AI API key
7+
* @param {string} requestId - The request ID associated with the feedback
8+
* @param {number} rating - The rating score
9+
* @param {string} feedbackText - The feedback message to send
10+
* @returns {Promise<string>} Response from the API in JSON format
11+
*/
12+
export async function feedback(apiKey, requestId, rating, feedbackText) {
13+
const endpoint = "https://sgai-api.onrender.com/api/v1/feedback";
14+
const headers = {
15+
"accept": "application/json",
16+
"SGAI-API-KEY": apiKey,
17+
"Content-Type": "application/json"
18+
};
19+
20+
const feedbackData = {
21+
request_id: requestId,
22+
rating: rating,
23+
feedback_text: feedbackText
24+
};
25+
26+
try {
27+
const response = await axios.post(endpoint, feedbackData, { headers });
28+
return JSON.stringify(response.data);
29+
} catch (error) {
30+
if (error.response) {
31+
return JSON.stringify({
32+
error: "HTTP error occurred",
33+
message: error.message,
34+
status_code: error.response.status
35+
});
36+
}
37+
return JSON.stringify({
38+
error: "An error occurred",
39+
message: error.message
40+
});
41+
}
42+
}

scrapegraph-js/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { scrape } from './scrape.js';
2+
export { credits } from './credits.js';
3+
export { feedback } from './feedback.js';

scrapegraph-js/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "scrapegraph-js",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"main": "index.js",
6+
"dependencies": {
7+
"axios": "^1.6.0"
8+
}
9+
}

scrapegraph-js/readme.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# ScrapeGraph JS SDK
2+
3+
A JavaScript SDK for interacting with the ScrapeGraph AI API. This SDK provides easy-to-use functions for web scraping, managing credits, and submitting feedback.
4+
5+
## Installation
6+
7+
Install the package using npm:
8+
9+
```bash
10+
npm install scrapegraph-js
11+
```
12+
13+
## Usage
14+
15+
First, import the required functions:
16+
17+
```javascript
18+
import { scrape, credits, feedback } from 'scrapegraph-js';
19+
```
20+
21+
### Scraping Websites
22+
23+
```javascript
24+
const apiKey = 'your_api_key';
25+
const url = 'https://example.com';
26+
27+
// Basic scraping
28+
const result = await scrape(apiKey, url);
29+
console.log(JSON.parse(result));
30+
31+
// Scraping with custom options
32+
const options = {
33+
elements: ['h1', '.product-price'],
34+
wait_for: '.specific-element',
35+
javascript: true
36+
};
37+
38+
const customResult = await scrape(apiKey, url, options);
39+
console.log(JSON.parse(customResult));
40+
```
41+
42+
### Checking Credits
43+
44+
```javascript
45+
const apiKey = 'your_api_key';
46+
47+
const creditsInfo = await credits(apiKey);
48+
console.log(JSON.parse(creditsInfo));
49+
```
50+
51+
### Submitting Feedback
52+
53+
```javascript
54+
const apiKey = 'your_api_key';
55+
const requestId = 'request_id_from_scrape_response';
56+
const rating = 5;
57+
const feedbackText = 'Great results!';
58+
59+
const feedbackResponse = await feedback(apiKey, requestId, rating, feedbackText);
60+
console.log(JSON.parse(feedbackResponse));
61+
```
62+
63+
## API Reference
64+
65+
### scrape(apiKey, url[, options])
66+
67+
Scrapes a website and returns the extracted data.
68+
69+
Parameters:
70+
- `apiKey` (string): Your ScrapeGraph AI API key
71+
- `url` (string): The URL to scrape
72+
- `options` (object, optional):
73+
- `elements` (array): Specific elements to extract
74+
- `wait_for` (string): CSS selector to wait for before scraping
75+
- `javascript` (boolean): Enable JavaScript rendering
76+
77+
### credits(apiKey)
78+
79+
Retrieves your current credit balance.
80+
81+
Parameters:
82+
- `apiKey` (string): Your ScrapeGraph AI API key
83+
84+
### feedback(apiKey, requestId, rating, feedbackText)
85+
86+
Submits feedback for a scraping request.
87+
88+
Parameters:
89+
- `apiKey` (string): Your ScrapeGraph AI API key
90+
- `requestId` (string): The request ID from the scrape response
91+
- `rating` (number): Rating score
92+
- `feedbackText` (string): Feedback message
93+
94+
## Error Handling
95+
96+
All functions return JSON strings that you should parse. In case of errors, the response will include error details:
97+
98+
```javascript
99+
{
100+
"error": "HTTP error occurred",
101+
"message": "Error details",
102+
"status_code": 400
103+
}
104+
```
105+
106+
## License
107+
108+
MIT
109+
110+
## Support
111+
112+
For support, please visit [ScrapeGraph AI Documentation](https://sgai-api.onrender.com/docs).
113+
114+
115+
116+

scrapegraph-js/scrape.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import axios from 'axios';
2+
3+
/**
4+
* Scrape and extract structured data from a webpage using ScrapeGraph AI.
5+
*
6+
* @param {string} apiKey - Your ScrapeGraph AI API key
7+
* @param {string} url - The URL of the webpage to scrape
8+
* @param {string} prompt - Natural language prompt describing what data to extract
9+
* @param {Object} [schema] - Optional schema object defining the output structure
10+
* @returns {Promise<string>} Extracted data in JSON format matching the provided schema
11+
*/
12+
export async function scrape(apiKey, url, prompt, schema = null) {
13+
const endpoint = "https://sgai-api.onrender.com/api/v1/smartscraper";
14+
const headers = {
15+
"accept": "application/json",
16+
"SGAI-API-KEY": apiKey,
17+
"Content-Type": "application/json"
18+
};
19+
20+
const payload = {
21+
website_url: url,
22+
user_prompt: prompt
23+
};
24+
25+
if (schema) {
26+
payload.output_schema = {
27+
description: schema.title || "Schema",
28+
name: schema.title || "Schema",
29+
properties: schema.properties || {},
30+
required: schema.required || []
31+
};
32+
}
33+
34+
try {
35+
const response = await axios.post(endpoint, payload, { headers });
36+
return JSON.stringify(response.data);
37+
} 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+
});
55+
}
56+
}
File renamed without changes.

scrapegraph-py/CODE_OF_CONDUCT.md

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
We as members, contributors, and leaders pledge to make participation in our
6+
community a harassment-free experience for everyone, regardless of age, body
7+
size, visible or invisible disability, ethnicity, sex characteristics, gender
8+
identity and expression, level of experience, education, socio-economic status,
9+
nationality, personal appearance, race, religion, or sexual identity
10+
and orientation.
11+
12+
We pledge to act and interact in ways that contribute to an open, welcoming,
13+
diverse, inclusive, and healthy community.
14+
15+
## Our Standards
16+
17+
Examples of behavior that contributes to a positive environment for our
18+
community include:
19+
20+
* Demonstrating empathy and kindness toward other people
21+
* Being respectful of differing opinions, viewpoints, and experiences
22+
* Giving and gracefully accepting constructive feedback
23+
* Accepting responsibility and apologizing to those affected by our mistakes,
24+
and learning from the experience
25+
* Focusing on what is best not just for us as individuals, but for the
26+
overall community
27+
28+
Examples of unacceptable behavior include:
29+
30+
* The use of sexualized language or imagery, and sexual attention or
31+
advances of any kind
32+
* Trolling, insulting or derogatory comments, and personal or political attacks
33+
* Public or private harassment
34+
* Publishing others' private information, such as a physical or email
35+
address, without their explicit permission
36+
* Other conduct which could reasonably be considered inappropriate in a
37+
professional setting
38+
39+
## Enforcement Responsibilities
40+
41+
Community leaders are responsible for clarifying and enforcing our standards of
42+
acceptable behavior and will take appropriate and fair corrective action in
43+
response to any behavior that they deem inappropriate, threatening, offensive,
44+
or harmful.
45+
46+
Community leaders have the right and responsibility to remove, edit, or reject
47+
comments, commits, code, wiki edits, issues, and other contributions that are
48+
not aligned to this Code of Conduct, and will communicate reasons for moderation
49+
decisions when appropriate.
50+
51+
## Scope
52+
53+
This Code of Conduct applies within all community spaces, and also applies when
54+
an individual is officially representing the community in public spaces.
55+
Examples of representing our community include using an official e-mail address,
56+
posting via an official social media account, or acting as an appointed
57+
representative at an online or offline event.
58+
59+
## Enforcement
60+
61+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
62+
reported to the community leaders responsible for enforcement at
63+
64+
All complaints will be reviewed and investigated promptly and fairly.
65+
66+
All community leaders are obligated to respect the privacy and security of the
67+
reporter of any incident.
68+
69+
## Enforcement Guidelines
70+
71+
Community leaders will follow these Community Impact Guidelines in determining
72+
the consequences for any action they deem in violation of this Code of Conduct:
73+
74+
### 1. Correction
75+
76+
**Community Impact**: Use of inappropriate language or other behavior deemed
77+
unprofessional or unwelcome in the community.
78+
79+
**Consequence**: A private, written warning from community leaders, providing
80+
clarity around the nature of the violation and an explanation of why the
81+
behavior was inappropriate. A public apology may be requested.
82+
83+
### 2. Warning
84+
85+
**Community Impact**: A violation through a single incident or series
86+
of actions.
87+
88+
**Consequence**: A warning with consequences for continued behavior. No
89+
interaction with the people involved, including unsolicited interaction with
90+
those enforcing the Code of Conduct, for a specified period of time. This
91+
includes avoiding interactions in community spaces as well as external channels
92+
like social media. Violating these terms may lead to a temporary or
93+
permanent ban.
94+
95+
### 3. Temporary Ban
96+
97+
**Community Impact**: A serious violation of community standards, including
98+
sustained inappropriate behavior.
99+
100+
**Consequence**: A temporary ban from any sort of interaction or public
101+
communication with the community for a specified period of time. No public or
102+
private interaction with the people involved, including unsolicited interaction
103+
with those enforcing the Code of Conduct, is allowed during this period.
104+
Violating these terms may lead to a permanent ban.
105+
106+
### 4. Permanent Ban
107+
108+
**Community Impact**: Demonstrating a pattern of violation of community
109+
standards, including sustained inappropriate behavior, harassment of an
110+
individual, or aggression toward or disparagement of classes of individuals.
111+
112+
**Consequence**: A permanent ban from any sort of public interaction within
113+
the community.
114+
115+
## Attribution
116+
117+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118+
version 2.0, available at
119+
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
120+
121+
Community Impact Guidelines were inspired by [Mozilla's code of conduct
122+
enforcement ladder](https://github.com/mozilla/diversity).
123+
124+
[homepage]: https://www.contributor-covenant.org
125+
126+
For answers to common questions about this code of conduct, see the FAQ at
127+
https://www.contributor-covenant.org/faq. Translations are available at
128+
https://www.contributor-covenant.org/translations.

0 commit comments

Comments
 (0)