Coder Social home page Coder Social logo

brahma-dev / metafetch Goto Github PK

View Code? Open in Web Editor NEW
21.0 3.0 7.0 241 KB

NodeJS package that fetches a given URL's title, description, images, links etc.

Home Page: https://www.npmjs.org/package/metafetch

License: MIT License

JavaScript 53.04% TypeScript 46.96%
javascript meta-tags scraping-websites scraper

metafetch's Introduction

Build Status Coverage Coverage Known Vulnerabilities

Try metafetch on RunKit

Metafetch fetches a given URL's title, description, images, links etc.

Installation

Use NPM to install:

npm install metafetch

Usage

import metafetch from 'metafetch';

metafetch.fetch('http://www.facebook.com'[, options]).then(function(meta) {
    console.log('title: ', meta.title);
    console.log('description: ', meta.description);
    console.log('type: ', meta.type);
    console.log('url: ', meta.url);
    console.log('ampURL: ', meta.ampURL);
    console.log('siteName: ', meta.siteName);
    console.log('charset: ', meta.charset);
    console.log('image: ', meta.image);
    console.log('meta: ', meta.meta);
    console.log('images: ', meta.images);
    console.log('links: ', meta.links);
    console.log('headers: ', meta.headers);
    console.log('language: ', meta.language);
}).catch(console.error);

Optional flags to disable parsing images and links and http timeout or headers

metafetch.fetch('http://www.facebook.com', {
    userAgent: "User Agent/Defaults to Firefox 123 (February 20, 2024)",
    flags: {
        images: false,
        links: false,
        language: false
    },
    http: {
        timeout: 30000,
        headers: {
            Accept: "*/*"
        }
    }
}).then(function(meta) {
    console.log('title: ', meta.title);
    console.log('description: ', meta.description);
    console.log('type: ', meta.type);
    console.log('url: ', meta.url);
    console.log('ampURL: ', meta.ampURL);
    console.log('siteName: ', meta.siteName);
    console.log('charset: ', meta.charset);
    console.log('image: ', meta.image);
    console.log('meta: ', meta.meta);
    console.log('headers: ', meta.headers);
    console.log('language: ', meta.language);
}).catch(console.error);;

Set User Agent across instance

metafetch.setUserAgent("PersonalBot");

Multiple instances with different User Agent

import { Metafetch } from 'metafetch';
const instance0 = new Metafetch("Bot 0");
const instance1 = new Metafetch("Bot 1");

-- or --

const instance0 = new Metafetch();
instance0.setUserAgent("Bot 0")
const instance1 = new Metafetch();
instance1.setUserAgent("Bot 1")

Response Data

  • title : Page title.
  • description : Page description or og:description meta tag.
  • image : og:image meta tag.
  • url : Page url or og:url meta tag.
  • ampURL : URL from amphtml tag or null.
  • images : All images on this page.
  • links : All links on this page.
  • meta : All the meta tags that with a property or name attribute. e.g <meta property="author" content="Example">, <meta name="description" content="Example.">
  • headers : HTTP headers, lowercasing field names much like node does.
  • language : Content language (ISO 639-1) based on meta data/headers, falling back to detection by franc.

License

(The MIT License)

Copyright (c) 2024 Brahma Dev;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

metafetch's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

metafetch's Issues

Charset problem

When I request some pages with this lib, some page characters are broken. (JP, CN, KO ...)
I founded this in restler dependency, it has old version iconv-lite.
Furthermore, I think the library is no more maintain.

So, how about update api client library?

How to have Custom User-Agent?

I would like to insert my own custom user-agent while making the request just like Facebook or Linkedin.

This is possible?

timeout not respected

Describe the bug
For some URLs, the timeout param passed into the fetch call is not respected, the call never returns and never throws an error.

  • Node.js version: v16.15
  • OS & version: MacOS Monterey 12.3.1

Actual behavior

Request goes into the void, never timing out.

Expected behavior

Request should time out after the specified amount of time.
...

Code to reproduce

(might need to try a different url, or set the timeout lower, but this one reproduces the issue most of the time for me)

metafetch.fetch(
  'https://www.seattletimes.com/nation-world/witnesses-myanmar-air-attack-kills-13-including-7-children/',
  {
    flags: { links: false, images: false },
    http: { timeout: 2500, headers: { 'Accept-Encoding': '*' }
  }
}).then(metadata => {
  console.log(metadata);
}).catch(e => {
  console.log(e);
});

Looking through the lib, it looks like the timeout param is not actually passed into the axios request
Screen Shot 2022-09-20 at 2 45 01 PM

adding timeout: http_options.timeout, to the axios request options objects seems to correct the issue

UnhandledPromiseRejectionWarning -- Catch() missing

I received following error:
On inspection I found that fetch() was written in promise without catch().
Can you please check this.

UnhandledPromiseRejectionWarning: TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters
at new ClientRequest (_http_client.js:115:13)
at Object.request (http.js:42:10)
at Request.request (/var/app/current/node_modules/superagent/lib/node/index.js:622:31)
at Request.end (/var/app/current/node_modules/superagent/lib/node/index.js:764:8)
at /var/app/current/node_modules/metafetch/index.js:203:74
at new Promise ()
at Object.Client.fetch (/var/app/current/node_modules/metafetch/index.js:138:9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.