Skip to content

Commit ab5e2dd

Browse files
committed
setting up the repo
1 parent 104c241 commit ab5e2dd

43 files changed

Lines changed: 17317 additions & 1 deletion

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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
node_modules/
3+
example.js

.eslintrc.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"settings": {
3+
"react": {
4+
"version": "latest"
5+
}
6+
},
7+
"extends": [
8+
"airbnb",
9+
"plugin:promise/recommended"
10+
],
11+
"plugins": [
12+
"mocha",
13+
"promise"
14+
],
15+
"env": {
16+
"commonjs": true,
17+
"node": true
18+
},
19+
"rules": {
20+
"max-len": ["error", {
21+
"code": 120,
22+
"ignoreStrings": true,
23+
"ignoreTrailingComments": true
24+
}],
25+
"no-param-reassign": "off"
26+
}
27+
}

.github/workflows/node.js.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
buildAndTest:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
node-version: [10.x, 12.x, 14.x]
18+
steps:
19+
- uses: actions/checkout@v2
20+
- name: Use Node.js ${{ matrix.node-version }}
21+
uses: actions/setup-node@v1
22+
with:
23+
node-version: ${{ matrix.node-version }}
24+
- name: Install Dependencies
25+
run: npm ci
26+
- name: Linting
27+
run: npm run eslint
28+
- name: Unit Testing
29+
run: npm run mocha:unit
30+
- name: Integration Testing
31+
run: npm run mocha:int

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
lib-cov
2+
*.seed
3+
*.log
4+
*.csv
5+
*.dat
6+
*.out
7+
*.pid
8+
*.gz
9+
*.DS_Store
10+
11+
pids
12+
logs
13+
results
14+
node_modules
15+
16+
.nyc_output/
17+
coverage/
18+
19+
npm-debug.log
20+
.idea/

.snyk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities.
2+
version: v1.14.1
3+
ignore: {}
4+
# patches apply the minimum changes required to fix a vulnerability
5+
patch:
6+
SNYK-JS-LODASH-567746:
7+
- lodash:
8+
patched: '2020-05-30T23:04:33.532Z'
9+
- cheerio > lodash:
10+
patched: '2020-05-30T23:04:33.532Z'

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
3+
## 1.1.0
4+
- Setting up the openGraphScraperLite repo
5+
6+
## 1.0.0
7+
- First version of openGraphScraperLite!

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
1-
# openGraphScraperLite
1+
# openGraphScraperLite
2+
3+
[![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)
4+
[![Known Vulnerabilities](https://snyk.io/test/github/jshemas/openGraphScraperLite/badge.svg)](https://snyk.io/test/github/jshemas/openGraphScraperLite)
5+
6+
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.
7+
8+
## Installation
9+
10+
```bash
11+
npm install open-graph-scraper-lite
12+
```
13+
14+
## Usage
15+
16+
Callback Example:
17+
```javascript
18+
const ogs = require('open-graph-scraper-lite');
19+
const options = { url: 'http://ogp.me/' };
20+
ogs(options, (error, results, response) => {
21+
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.
22+
console.log('results:', results); // This contains all of the Open Graph results
23+
console.log('response:', response); // This contains the HTML of page
24+
});
25+
```
26+
27+
Promise Example:
28+
```javascript
29+
const ogs = require('open-graph-scraper-lite');
30+
const options = { url: 'http://ogp.me/' };
31+
ogs(options)
32+
.then((data) => {
33+
const { error, result, response } = data;
34+
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.
35+
console.log('result:', result); // This contains all of the Open Graph results
36+
console.log('response:', response); // This contains the HTML of page
37+
})
38+
```
39+
40+
## Results JSON
41+
42+
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...
43+
```javascript
44+
{
45+
ogTitle: 'Open Graph protocol',
46+
ogType: 'website',
47+
ogUrl: 'http://ogp.me/',
48+
ogDescription: 'The Open Graph protocol enables any web page to become a rich object in a social graph.',
49+
ogImage: {
50+
url: 'http://ogp.me/logo.png',
51+
width: '300',
52+
height: '300',
53+
type: 'image/png'
54+
},
55+
requestUrl: 'http://ogp.me/',
56+
success: true
57+
}
58+
```
59+
60+
## Options
61+
| Name | Info | Default Value | Required |
62+
|----------------------|----------------------------------------------------------------------------|---------------|----------|
63+
| url | URL of the site. | | x |
64+
| timeout | Timeout of the request | 2000 ms | |
65+
| html | You can pass in an HTML string to run ogs on it. (use without options.url) | | |
66+
| blacklist | Pass in an array of sites you don't want ogs to run on. | [] | |
67+
| onlyGetOpenGraphInfo | Only fetch open graph info and don't fall back on anything else. | false | |
68+
| ogImageFallback | Fetch other images if no open graph ones are found. | true | |
69+
| customMetaTags | Here you can define custom meta tags you want to scrape. | [] | |
70+
| allMedia | By default, OGS will only send back the first image/video it finds | false | |
71+
| retry | Number of times ogs will retry the request. | 2 | |
72+
| headers | An object containing request headers. Useful for setting the user-agent | {} | |
73+
| peekSize | Sets the peekSize for the request | 1024 | |
74+
| 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) | |
75+
76+
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.
77+
78+
Custom Meta Tag Example:
79+
```javascript
80+
const ogs = require('open-graph-scraper-lite');
81+
const options = {
82+
url: 'https://github.com/jshemas/openGraphScraper',
83+
customMetaTags: [{
84+
multiple: false, // is there more then one of these tags on a page (normally this is false)
85+
property: 'hostname', // meta tag name/property attribute
86+
fieldName: 'hostnameMetaTag', // name of the result variable
87+
}],
88+
};
89+
ogs(options)
90+
.then((data) => {
91+
const { error, result, response } = data;
92+
console.log('hostnameMetaTag:', result.hostnameMetaTag); // hostnameMetaTag: github.com
93+
})
94+
```
95+
96+
## Tests
97+
98+
Then you can run the tests by running...
99+
```bash
100+
npm run test
101+
```

example.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const ogs = require('open-graph-scraper-lite');
2+
3+
const options = {
4+
url: 'http://ogp.me/'
5+
};
6+
7+
ogs(options)
8+
.then((data) => {
9+
const { error, result, response } = data;
10+
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.
11+
console.log('result:', result); // This contains all of the Open Graph results
12+
console.log('response:', response); // This contains the HTML of page
13+
});

index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const openGraphScraperLite = require('./lib/openGraphScraperLite');
2+
3+
/*
4+
* run
5+
* @param string options - options the user has set
6+
* @param function callback and promise
7+
*/
8+
exports.run = async (options, callback) => {
9+
const hasCallback = typeof callback === 'function';
10+
if (hasCallback) {
11+
let results;
12+
try {
13+
results = await openGraphScraperLite(options);
14+
} catch (exception) {
15+
const returnError = {
16+
success: false,
17+
requestUrl: options.url,
18+
error: exception.message,
19+
errorDetails: exception,
20+
};
21+
return callback(true, returnError);
22+
}
23+
return callback(false, results.ogObject, results.response);
24+
}
25+
// eslint-disable-next-line no-async-promise-executor
26+
return new Promise(async (resolve, reject) => {
27+
let results;
28+
try {
29+
results = await openGraphScraperLite(options);
30+
} catch (exception) {
31+
const returnError = {
32+
error: true,
33+
result: {
34+
success: false,
35+
requestUrl: options.url,
36+
error: exception.message,
37+
errorDetails: exception,
38+
},
39+
};
40+
return reject(returnError);
41+
}
42+
const returnValues = {
43+
error: false,
44+
result: results.ogObject,
45+
response: results.response,
46+
};
47+
return resolve(returnValues);
48+
});
49+
};
50+
51+
module.exports = (options, callback) => exports.run(options, callback);

0 commit comments

Comments
 (0)