|
1 | | -# openGraphScraperLite |
| 1 | +# openGraphScraperLite |
| 2 | + |
| 3 | +[](https://github.com/jshemas/openGraphScraperLite/actions?query=branch%3Amaster) |
| 4 | +[](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 | +``` |
0 commit comments