├── .eslintrc.json ├── .github └── workflows │ └── tests.yaml ├── .gitignore ├── LICENSE ├── index.js ├── package-lock.json ├── package.json ├── readme.md └── tests └── test.js /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "standard", 3 | "plugins": [ 4 | "standard", 5 | "promise", 6 | "extra-rules" 7 | ], 8 | "parserOptions": { 9 | "ecmaVersion": 2017 10 | }, 11 | "globals": { 12 | "$": true 13 | }, 14 | "env": { 15 | "browser": true, 16 | "node": true 17 | }, 18 | "rules": { 19 | "quotes": ["error", "single"], 20 | "prefer-arrow-callback": [ "error", { "allowNamedFunctions": true } ], 21 | "consistent-return": 2, 22 | "no-var" : 2, 23 | "new-cap" : 0, 24 | "indent" : 0, 25 | "no-else-return" : 1, 26 | "semi" : [1, "always"], 27 | "space-unary-ops" : 2, 28 | "no-undef": 1, 29 | "no-unused-vars": 1, 30 | "extra-rules/no-commented-out-code": "warn", 31 | "keyword-spacing": [ 32 | "error", { 33 | "before": false, "after": false, "overrides": { 34 | "const": { 35 | "after": true 36 | }, 37 | "import": { 38 | "after": true 39 | }, 40 | "from": { 41 | "after": true 42 | }, 43 | "return": { 44 | "after": true 45 | }, 46 | "case": { 47 | "after": true 48 | } 49 | } 50 | }], 51 | "space-before-function-paren": 0, 52 | "space-before-blocks": ["error", "never"], 53 | "camelcase": 0, 54 | "handle-callback-err": ["error", "none"], 55 | "object-curly-spacing": ["error", "always"] 56 | } 57 | } -------------------------------------------------------------------------------- /.github/workflows/tests.yaml: -------------------------------------------------------------------------------- 1 | name: metaget-tests 2 | on: 3 | push: 4 | branches: [ master ] 5 | pull_request: 6 | branches: [ master ] 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | matrix: 12 | node-version: [10.x, 12.x] 13 | steps: 14 | - uses: actions/checkout@v2 15 | - name: Use Node.js ${{ matrix.node-version }} 16 | uses: actions/setup-node@v1 17 | with: 18 | node-version: ${{ matrix.node-version }} 19 | - name: npm install 20 | run: npm i 21 | - name: npm test 22 | run: npm run test 23 | env: 24 | test: true 25 | CI: true -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011-2020 Mark Moffat 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const cheerio = require('cheerio'); 2 | const got = require('got'); 3 | 4 | const fetch = async(uri, userArgs, callback) => { 5 | // Set defaults 6 | const options = { 7 | timeout: 5000, 8 | headers: { 9 | 'User-Agent': 'request' 10 | } 11 | }; 12 | 13 | // If no args are supplied, move callback 14 | if(typeof userArgs === 'function'){ 15 | callback = userArgs; 16 | } 17 | 18 | // override supplied args 19 | if(typeof userArgs === 'object'){ 20 | Object.keys(userArgs).forEach((arg) => { 21 | options[arg] = userArgs[arg]; 22 | }); 23 | } 24 | 25 | // Fetch the meta data 26 | let response; 27 | try{ 28 | response = await got.get(uri, { 29 | options 30 | }); 31 | }catch(ex){ 32 | return respond({}, ex.code, callback); 33 | } 34 | 35 | // Parse the meta data 36 | const $ = cheerio.load(response.body); 37 | const meta = $('meta'); 38 | const keys = Object.keys(meta); 39 | const metaData = {}; 40 | keys.forEach((key) => { 41 | if(meta[key].attribs !== undefined){ 42 | if(meta[key].attribs.property && meta[key].attribs.content){ 43 | metaData[meta[key].attribs.property] = meta[key].attribs.content; 44 | } 45 | if(meta[key].attribs.name && meta[key].attribs.content){ 46 | metaData[meta[key].attribs.name] = meta[key].attribs.content; 47 | } 48 | } 49 | }); 50 | 51 | // Response 52 | return respond(metaData, null, callback); 53 | }; 54 | 55 | // Allows for callback or promise response 56 | const respond = (data, error, callback) => { 57 | if(!callback){ 58 | if(error){ 59 | return Promise.reject(error); 60 | } 61 | return Promise.resolve(data); 62 | } 63 | return callback(error, data); 64 | }; 65 | 66 | module.exports = { 67 | fetch 68 | }; 69 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "metaget", 3 | "version": "1.0.7", 4 | "description": "A Node.js module to fetch HTML meta tags (including Open Graph) from a remote URL", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "jest tests/test.js" 8 | }, 9 | "keywords": [ 10 | "metatags", 11 | "metadata", 12 | "fetch", 13 | "meta", 14 | "remote", 15 | "meta", 16 | "tags", 17 | "html", 18 | "parse", 19 | "opengraph", 20 | "open graph" 21 | ], 22 | "author": "Mark Moffat (https://markmoffat.com)", 23 | "license": "MIT", 24 | "repository": { 25 | "type": "git", 26 | "url": "https://github.com/mrvautin/metaget.git" 27 | }, 28 | "dependencies": { 29 | "cheerio": "^1.0.0-rc.3", 30 | "got": "^10.6.0" 31 | }, 32 | "devDependencies": { 33 | "jest": "^25.2.7", 34 | "eslint": "^6.8.0", 35 | "eslint-config-standard": "^13.0.1", 36 | "eslint-plugin-extra-rules": "0.0.0-development", 37 | "eslint-plugin-import": "^2.18.2", 38 | "eslint-plugin-node": "^9.2.0", 39 | "eslint-plugin-promise": "^4.2.1", 40 | "eslint-plugin-standard": "^4.0.1" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # metaget 2 | 3 | A Node.js module to fetch HTML meta tags (including Open Graph) from a remote URL 4 | 5 | [![Github stars](https://img.shields.io/github/stars/mrvautin/metaget.svg?style=social&label=Star)](https://github.com/mrvautin/metaget) 6 | 7 | [![Actions Status](https://github.com/mrvautin/metaget/workflows/metaget-tests/badge.svg)](https://github.com/mrvautin/metaget/actions) 8 | 9 | ## Installation 10 | 11 | ``` 12 | npm install metaget --save 13 | ``` 14 | 15 | ## Usage 16 | 17 | Promise: 18 | ``` javascript 19 | const metaget = require('metaget'); 20 | try{ 21 | const metaResponse = await metaget.fetch('https://wordpress.com'); 22 | console.log('metaResponse', metaResponse); 23 | }catch(ex){ 24 | console.log('Fail', ex); 25 | } 26 | ``` 27 | 28 | Callback: 29 | ``` javascript 30 | const metaget = require('metaget'); 31 | metaget.fetch('https://wordpress.com', (err, metaResponse) => { 32 | if(err){ 33 | console.log(err); 34 | }else{ 35 | console.log(metaResponse); 36 | } 37 | }); 38 | ``` 39 | 40 | Response will be an Object containing all the meta tags from the URL. All tags are output in the example above. Some tags with illegal characters can be accessed by: 41 | 42 | ``` javascript 43 | metaResponse['og:title']; 44 | ``` 45 | 46 | ## Options 47 | 48 | It's possible to set any HTTP headers in the request. This can be done by specifying them as options in the call. If no options are provided the only default header is a User-Agent of "request". 49 | 50 | This is how you would specify a "User-Agent" of a Google Bot: 51 | 52 | ``` javascript 53 | try{ 54 | const metaResponse = await metaget.fetch('https://wordpress.com', { headers: { 'User-Agent': 'Googlebot' } }); 55 | console.log('metaResponse', metaResponse); 56 | }catch(ex){ 57 | console.log('Fail', ex); 58 | } 59 | ``` 60 | 61 | ## Contributing 62 | 63 | 1. Fork it! 64 | 2. Create your feature branch: `git checkout -b my-new-feature` 65 | 3. Commit your changes: `git commit -am 'Add some feature'` 66 | 4. Push to the branch: `git push origin my-new-feature` 67 | 5. Submit a pull request :D 68 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-undef */ 2 | const metaget = require('../index'); 3 | 4 | test('Fetch wordpress.com async/await', async () => { 5 | try{ 6 | const metaResponse = await metaget.fetch('https://wordpress.com'); 7 | expect(metaResponse['application-name']).toBe('WordPress.com'); 8 | expect(metaResponse['og:type']).toBe('website'); 9 | }catch(ex){} 10 | }); 11 | 12 | test('Fetch wordpress.com with callback', () => { 13 | metaget.fetch('https://wordpress.com', (err, metaResponse) => { 14 | if(err){ 15 | console.log(err); 16 | }else{ 17 | expect(metaResponse['application-name']).toBe('WordPress.com'); 18 | expect(metaResponse['og:type']).toBe('website'); 19 | } 20 | }); 21 | }); 22 | 23 | test('Fetch dud website', async () => { 24 | try{ 25 | await metaget.fetch('https://imadudwebsite.com'); 26 | }catch(ex){ 27 | expect(ex).toBe('ENOTFOUND'); 28 | } 29 | }); 30 | 31 | test('Fetch wordpress.com with custom User-Agent header', async () => { 32 | try{ 33 | const metaResponse = await metaget.fetch('https://wordpress.com', { headers: { 'User-Agent': 'Googlebot' } }); 34 | expect(metaResponse['application-name']).toBe('WordPress.com'); 35 | expect(metaResponse['og:type']).toBe('website'); 36 | }catch(ex){} 37 | }); 38 | --------------------------------------------------------------------------------