npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-lens-image-sources-api

v1.0.1

Published

A Node.js API for retrieving image sources using Google Lens.

Downloads

4

Readme

Google Lens Image Sources API

This project provides an API for uploading an image URL and retrieving related sources using Google Lens. It leverages Puppeteer for web scraping and Puppeteer Cluster for efficient browser management, ensuring high performance and resource optimization.

Features

  • Image Upload: Accepts an image URL and processes it using Google Lens.
  • Related Sources Extraction: Extracts related sources including title, source, link, thumbnails, and dimensions.
  • Concurrency Management: Utilizes Puppeteer Cluster for handling multiple requests concurrently.
  • Rate Limiting: Limits the number of requests per IP to prevent abuse.
  • Caching: Caches results for faster subsequent requests with an option to bypass cache.
  • Error Handling: Centralized error handling ensures robust performance.

Prerequisites

  • Node.js (version 14.x or later)
  • npm (version 6.x or later)

Installation

  1. Clone the repository:

    git clone https://github.com/shishir1337/Google-Lens-Image-Sources-API.git
  2. Navigate to the project directory:

    cd Google-Lens-Image-Sources-API
  3. Install the dependencies:

    npm install

Usage

  1. Start the server:

    npm start
  2. The server will run on http://localhost:3000.

  3. API Endpoint:

    • POST /api/upload

    • Request Body:

      {
          "imageUrl": "https://example.com/image.jpg",
          "no_cache": false
      }
    • Response:

      {
          "image_sources": [
              {
                  "position": 1,
                  "title": "Example Title",
                  "source": "Example Source",
                  "source_logo": "https://example.com/logo.png",
                  "link": "https://example.com",
                  "thumbnail": "https://example.com/thumbnail.jpg",
                  "actual_image_width": 800,
                  "actual_image_height": 600
              },
              ...
          ]
      }
    • no_cache Parameter (Optional):

      • true to bypass the cache and fetch fresh results.
      • false (or omitted) to use cached results if available.

Implementation Details

Rate Limiting

Limits the number of requests to 10 per minute per IP to prevent abuse. This is implemented using the express-rate-limit package.

Caching

Results are cached for 1 hour to improve performance and reduce redundant processing. The node-cache package is used for caching. You can bypass caching by setting the no_cache parameter to true.

Concurrency Management

Utilizes Puppeteer Cluster to handle multiple requests concurrently, optimizing resource usage and improving throughput. Puppeteer Cluster manages a pool of browser instances, allowing multiple pages to be processed in parallel.

Error Handling

Centralized error handling middleware ensures that errors are properly logged and a meaningful response is sent to the client. All errors are caught and handled gracefully, providing a consistent error response format.

Code Structure

  • index.js: Main application file containing the server setup and API implementation.
  • package.json: Project metadata and dependencies.

Example Request

Here is an example of how to make a request to the API using curl:

curl -X POST http://localhost:3000/api/upload -H "Content-Type: application/json" -d '{"imageUrl": "https://example.com/image.jpg", "no_cache": false}'

Install as an npm Package

To install the package from npm:

npm install google-lens-image-sources-api

Usage

Using the Installed Package

After installing the package, you can use it in your Node.js project:

Require the Package:

const GoogleLensImageSourcesAPI = require('google-lens-image-sources-api');

Create an Instance:

const api = new GoogleLensImageSourcesAPI({
  // Optional configuration parameters
  rateLimit: 10,  // Limits requests to 10 per minute
  cacheDuration: 3600  // Cache results for 1 hour
});

Make a Request:

const imageUrl = 'https://example.com/image.jpg';

api.uploadImage(imageUrl, { no_cache: false })
  .then(response => {
    console.log('Image Sources:', response.image_sources);
  })
  .catch(error => {
    console.error('Error:', error);
  });

Running the Local Server

Start the server:

npm start
The server will run on http://localhost:3000.

Contributing

Contributions are welcome! Please follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Make your changes and commit them with descriptive commit messages.
  4. Push your changes to your fork.
  5. Open a pull request to the main repository.

Please make sure to update tests as appropriate.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contact

For any inquiries, please contact [email protected].