Skip to content

Added markdownify functionality #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 19 additions & 2 deletions scrapegraph-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ yarn add scrapegraph-js

```javascript
import { smartScraper } from 'scrapegraph-js';
import 'dotenv/config';

// Initialize variables
const apiKey = process.env.SGAI_APIKEY; // Set your API key as an environment variable
Expand Down Expand Up @@ -85,7 +84,6 @@ Here is a real-world example:
```javascript
import { smartScraper } from 'scrapegraph-js';
import { z } from 'zod';
import 'dotenv/config';

const apiKey = 'your-api-key';
const url = 'https://scrapegraphai.com/';
Expand All @@ -107,6 +105,25 @@ const schema = z.object({
})();
```

### Markdownify
Converts a webpage into clean, well-structured markdown format.
```javascript
import { smartScraper } from 'scrapegraph-js';

const apiKey = "your_api_key";
const url = 'https://scrapegraphai.com/';

(async () => {
try {
const response = await markdownify(apiKey, url);
console.log(response);
} catch (error) {
console.error(error);
}
})();
```


### Checking API Credits

```javascript
Expand Down
35 changes: 35 additions & 0 deletions scrapegraph-js/examples/markdownify_example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { getMarkdownifyRequest, markdownify } from 'scrapegraph-js';
import fs from 'fs';
import 'dotenv/config';

// markdownify function example
const apiKey = process.env.SGAI_APIKEY;
const url = 'https://scrapegraphai.com/';

try {
const response = await markdownify(apiKey, url);
console.log(response);
saveFile(response.result);
} catch (error) {
console.error(error);
}

// helper function for save the file locally
function saveFile(output) {
try {
fs.writeFileSync('result.md', output);
console.log('Success!');
} catch (err) {
console.error('Error during the file writing: ', err);
}
}

// getMarkdownifyRequest function example
const requestId = '2563b972-cb6f-400b-be76-edb235458560';

try {
const response = await getMarkdownifyRequest(apiKey, requestId);
console.log(response);
} catch (error) {
console.log(error);
}
3 changes: 2 additions & 1 deletion scrapegraph-js/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { smartScraper, getSmartScraperRequest } from './src/smartScraper.js';
export { markdownify, getMarkdownifyRequest } from './src/markdownify.js';
export { getCredits } from './src/credits.js';
export { sendFeedback } from './src/feedback.js';
export { sendFeedback } from './src/feedback.js';
52 changes: 52 additions & 0 deletions scrapegraph-js/src/markdownify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import axios from 'axios';
import handleError from './utils/handleError.js';

/**
* Converts a webpage into clean, well-structured markdown format.
*
* @param {string} apiKey - Your ScrapeGraph AI API key.
* @param {string} url - The URL of the webpage to be converted.
* @returns {Promise<string>} A promise that resolves to the markdown representation of the webpage.
* @throws {Error} Throws an error if the HTTP request fails.
*/
export async function markdownify(apiKey, url){
const endpoint = 'https://api.scrapegraphai.com/v1/markdownify';
const headers = {
'accept': 'application/json',
'SGAI-APIKEY': apiKey,
};

const payload = {
website_url: url,
};

try {
const response = await axios.post(endpoint, payload, { headers });
return response.data;
} catch (error) {
handleError(error)
}
}

/**
* Retrieves the status or result of a markdownify request, with the option to review results from previous requests.
*
* @param {string} apiKey - Your ScrapeGraph AI API key.
* @param {string} requestId - The unique identifier for the markdownify request whose result you want to retrieve.
* @returns {Promise<string>} A promise that resolves with details about the status or outcome of the specified request.
* @throws {Error} Throws an error if the HTTP request fails.
*/
export async function getMarkdownifyRequest(apiKey, requestId){
const endpoint = 'https://api.scrapegraphai.com/v1/markdownify/' + requestId;
const headers = {
'accept': 'application/json',
'SGAI-APIKEY': apiKey,
};

try {
const response = await axios.get(endpoint, { headers });
return response.data;
} catch (error) {
handleError(error)
}
}
Loading