Skip to content

Commit 85d7581

Browse files
authored
Merge pull request #9 from jshemas/rebuild
rebuild library in typescript and no longer makes HTTP requests
2 parents ae69de6 + 112932e commit 85d7581

71 files changed

Lines changed: 8797 additions & 15039 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
coverage/
2+
dist/
23
node_modules/
3-
example.js

.eslintrc.json

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
{
2-
"settings": {
3-
"react": {
4-
"version": "latest"
5-
}
6-
},
72
"extends": [
8-
"airbnb",
3+
"airbnb-base",
4+
"airbnb-typescript/base",
95
"plugin:promise/recommended"
106
],
117
"plugins": [
12-
"mocha",
138
"promise"
149
],
15-
"env": {
16-
"commonjs": true,
17-
"node": true
10+
"parserOptions": {
11+
"project": "./tsconfig.json"
1812
},
1913
"rules": {
14+
"import/no-named-as-default": 0,
2015
"max-len": ["error", {
2116
"code": 120,
2217
"ignoreStrings": true,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**To Reproduce**
14+
Steps to reproduce the behavior:
15+
16+
**Expected behavior**
17+
A clear and concise description of what you expected to happen.
18+
19+
**Actual behavior**
20+
A clear and concise description of what is happening.
21+
22+
**Screenshots**
23+
If applicable, add screenshots to help explain your problem.
24+
25+
**Additional context**
26+
Add any other context about the problem here.
27+
28+
- OS: [e.g. iOS/Windows/Linux]
29+
- Node Version: [e.g. 18]
30+
- openGraphScraperLite Version:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/node.js.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,23 @@ jobs:
1313
buildAndTest:
1414
runs-on: ubuntu-latest
1515
strategy:
16+
fail-fast: false
1617
matrix:
17-
node-version: [10.x, 12.x, 14.x]
18+
node-version:
19+
- 16
20+
- 18
21+
- 20
1822
steps:
19-
- uses: actions/checkout@v2
23+
- uses: actions/checkout@v3
2024
- name: Use Node.js ${{ matrix.node-version }}
21-
uses: actions/setup-node@v1
25+
uses: actions/setup-node@v3
2226
with:
2327
node-version: ${{ matrix.node-version }}
2428
- name: Install Dependencies
25-
run: npm ci
29+
run: npm install
2630
- name: Linting
2731
run: npm run eslint
32+
- name: Running Typescript
33+
run: npm run build
2834
- name: Unit Testing
2935
run: npm run mocha:unit
30-
- name: Integration Testing
31-
run: npm run mocha:int

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
# Change Log
22

3+
## 2.0.0
4+
5+
- This library no longer does HTTP requests, it will now just pull Open Graph and Twitter Card info from HTML pass into it.
6+
37
## 1.1.1
8+
49
- Fixing issues with ky import
510

611
## 1.1.0
12+
713
- Setting up the openGraphScraperLite repo
814

915
## 1.0.0
16+
1017
- First version of openGraphScraperLite!

README.md

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,82 @@
1-
# This module was meant to be a proof of concept. You will probably run into a bunch of cors issues using this since it's making the requests from the browser. Using a reverse proxy libraries like https://github.com/Rob--W/cors-anywhere might help solve the problem, but you are probably better off making the requests in a backend service then pass in the html into openGraphScraper.
2-
31
# openGraphScraperLite
42

53
[![Node.js CI](https://github.com/jshemas/openGraphScraperLite/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/jshemas/openGraphScraperLite/actions?query=branch%3Amaster)
64
[![Known Vulnerabilities](https://snyk.io/test/github/jshemas/openGraphScraperLite/badge.svg)](https://snyk.io/test/github/jshemas/openGraphScraperLite)
75

8-
A simple javascript module for scraping Open Graph and Twitter Card info off a site. For Node.js usage, we recommend `open-graph-scraper` by the same people.
6+
A simple javascript module for scraping Open Graph and Twitter Card info from given HTML. For Node.js usage, we recommend `open-graph-scraper` by the same people and can do HTTP requests.
97

108
## Installation
119

1210
```bash
13-
npm install open-graph-scraper-lite
11+
npm install open-graph-scraper-lite --save
1412
```
1513

1614
## Usage
1715

18-
Callback Example:
19-
```javascript
20-
const ogs = require('open-graph-scraper-lite');
21-
const options = { url: 'http://ogp.me/' };
22-
ogs(options, (error, results, response) => {
23-
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
24-
console.log('results:', results); // This contains all of the Open Graph results
25-
console.log('response:', response); // This contains the HTML of page
26-
});
27-
```
28-
29-
Promise Example:
3016
```javascript
31-
const ogs = require('open-graph-scraper-lite');
32-
const options = { url: 'http://ogp.me/' };
17+
const ogs = require('open-graph-scraper');
18+
const options = {
19+
html: `<html><head>
20+
<link rel="icon" type="image/png" href="https://bar.com/foo.png" />
21+
<meta charset="utf-8" />
22+
<meta property="og:description" name="og:description" content="html description example" />
23+
<meta property="og:image" name="og:image" content="https://www.foo.com/bar.jpg" />
24+
<meta property="og:title" name="og:title" content="foobar" />
25+
<meta property="og:type" name="og:type" content="website" />
26+
</head></html>`
27+
};
3328
ogs(options)
3429
.then((data) => {
35-
const { error, result, response } = data;
36-
console.log('error:', error); // This is returns true or false. True if there was a error. The error it self is inside the results object.
37-
console.log('result:', result); // This contains all of the Open Graph results
38-
console.log('response:', response); // This contains the HTML of page
30+
const { result } = data;
31+
console.log('result:', result);
3932
})
4033
```
4134

4235
## Results JSON
4336

44-
Check the return for a ```success``` flag. If success is set to true, then the url input was valid. Otherwise it will be set to false. The above example will return something like...
4537
```javascript
46-
{
47-
ogTitle: 'Open Graph protocol',
38+
result: {
39+
ogDescription: 'html description example',
40+
ogTitle: 'foobar',
4841
ogType: 'website',
49-
ogUrl: 'http://ogp.me/',
50-
ogDescription: 'The Open Graph protocol enables any web page to become a rich object in a social graph.',
51-
ogImage: {
52-
url: 'http://ogp.me/logo.png',
53-
width: '300',
54-
height: '300',
55-
type: 'image/png'
56-
},
57-
requestUrl: 'http://ogp.me/',
42+
ogImage: [ { url: 'https://www.foo.com/bar.jpg', type: 'jpg' } ],
43+
favicon: 'https://bar.com/foo.png',
44+
charset: 'utf-8',
5845
success: true
5946
}
6047
```
6148

6249
## Options
50+
6351
| Name | Info | Default Value | Required |
6452
|----------------------|----------------------------------------------------------------------------|---------------|----------|
65-
| url | URL of the site. | | x |
66-
| timeout | Timeout of the request | 2000 ms | |
67-
| html | You can pass in an HTML string to run ogs on it. (use without options.url) | | |
68-
| blacklist | Pass in an array of sites you don't want ogs to run on. | [] | |
53+
| html | You can pass in an HTML string to run ogs on it. (use without options.url) | x | |
6954
| onlyGetOpenGraphInfo | Only fetch open graph info and don't fall back on anything else. | false | |
70-
| ogImageFallback | Fetch other images if no open graph ones are found. | true | |
7155
| customMetaTags | Here you can define custom meta tags you want to scrape. | [] | |
72-
| allMedia | By default, OGS will only send back the first image/video it finds | false | |
73-
| retry | Number of times ogs will retry the request. | 2 | |
74-
| headers | An object containing request headers. Useful for setting the user-agent | {} | |
75-
| peekSize | Sets the peekSize for the request | 1024 | |
76-
| urlValidatorSettings | Sets the options used by validator.js for testing the URL | [Here](https://github.com/jshemas/openGraphScraper/blob/master/lib/openGraphScraper.js#L21-L36) | |
7756

78-
Note: `open-graph-scraper-lite` uses [ky](https://github.com/sindresorhus/ky) for requests and most of [ky's options](https://github.com/sindresorhus/ky#api) should work as `open-graph-scraper-lite` options.
57+
## Custom Meta Tag Example
7958

80-
Custom Meta Tag Example:
8159
```javascript
82-
const ogs = require('open-graph-scraper-lite');
60+
const ogs = require('open-graph-scraper');
8361
const options = {
84-
url: 'https://github.com/jshemas/openGraphScraper',
62+
html: `<html><head>
63+
<link rel="icon" type="image/png" href="https://bar.com/foo.png" />
64+
<meta charset="utf-8" />
65+
<meta property="og:description" name="og:description" content="html description example" />
66+
<meta property="og:image" name="og:image" content="https://www.foo.com/bar.jpg" />
67+
<meta property="og:title" name="og:title" content="foobar" />
68+
<meta property="og:type" name="og:type" content="website" />
69+
<meta name="hostname" content="github.com">
70+
</head></html>`,
8571
customMetaTags: [{
86-
multiple: false, // is there more then one of these tags on a page (normally this is false)
72+
multiple: false, // is there more than one of these tags on a page (normally this is false)
8773
property: 'hostname', // meta tag name/property attribute
8874
fieldName: 'hostnameMetaTag', // name of the result variable
8975
}],
9076
};
9177
ogs(options)
9278
.then((data) => {
93-
const { error, result, response } = data;
94-
console.log('hostnameMetaTag:', result.hostnameMetaTag); // hostnameMetaTag: github.com
79+
const { result } = data;
80+
console.log('hostnameMetaTag:', result.customMetaTags.hostnameMetaTag); // hostnameMetaTag: github.com
9581
})
9682
```
97-
98-
## Tests
99-
100-
Then you can run the tests by running...
101-
```bash
102-
npm run test
103-
```

dist/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { OpenGraphScraperOptions, OgObject } from './lib/types';
2+
/**
3+
* `open-graph-scraper` uses [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for http requests
4+
* for scraping Open Graph and Twitter Card info off a website.
5+
*
6+
* @param {object} options - The options used by Open Graph Scraper
7+
* @param {boolean} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on anything else.
8+
* @param {object} [options.customMetaTags] - Here you can define custom meta tags you want to scrape.
9+
* @param {string} options.html - You can pass in an HTML string to run ogs on it. (use without options.url)
10+
* @returns {Promise} Promise Object with the Open Graph results
11+
*/
12+
export default function run(options: OpenGraphScraperOptions): Promise<ErrorResult | SuccessResult>;
13+
export type SuccessResult = {
14+
error: false;
15+
html: string;
16+
result: OgObject;
17+
};
18+
export type ErrorResult = {
19+
error: true;
20+
html: undefined;
21+
result: OgObject;
22+
};

dist/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
/* eslint-disable max-len, import/no-import-module-exports */
7+
const openGraphScraper_1 = __importDefault(require("./lib/openGraphScraper"));
8+
/**
9+
* `open-graph-scraper` uses [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) for http requests
10+
* for scraping Open Graph and Twitter Card info off a website.
11+
*
12+
* @param {object} options - The options used by Open Graph Scraper
13+
* @param {boolean} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on anything else.
14+
* @param {object} [options.customMetaTags] - Here you can define custom meta tags you want to scrape.
15+
* @param {string} options.html - You can pass in an HTML string to run ogs on it. (use without options.url)
16+
* @returns {Promise} Promise Object with the Open Graph results
17+
*/
18+
async function run(options) {
19+
let results;
20+
try {
21+
results = await (0, openGraphScraper_1.default)(options);
22+
}
23+
catch (error) {
24+
const exception = error;
25+
const returnError = {
26+
error: true,
27+
result: {
28+
success: false,
29+
error: exception.message,
30+
errorDetails: exception,
31+
},
32+
html: undefined,
33+
};
34+
// eslint-disable-next-line @typescript-eslint/no-throw-literal
35+
throw returnError;
36+
}
37+
const returnSuccess = {
38+
error: false,
39+
result: results.ogObject,
40+
html: results.html,
41+
};
42+
return returnSuccess;
43+
}
44+
exports.default = run;
45+
module.exports = run;

dist/lib/extract.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
2+
/**
3+
* extract all of the meta tags needed for ogs
4+
*
5+
* @param {sting} body - the body of the fetch request
6+
* @param {object} options - options for ogs
7+
* @return {object} object with ogs results
8+
*
9+
*/
10+
export default function extractMetaTags(body: string, options: OpenGraphScraperOptions): OgObjectInteral;

0 commit comments

Comments
 (0)