Giter Club home page Giter Club logo

price-monitoring's Introduction

price-monitoring NPM Version JavaScript Style Guide

Node.js price monitoring library, leveraging the power of x-ray and nightmare.

Features

  • Leverage the power of x-ray, nightmare and cheerio.
  • Electron based headless-engine that accepts JS and follows redirects.
  • Parse Products starting from a CSV file.
  • Modular structure for Website parser (MyEcommerce that extends SiteParser).
  • Bind every URL to an instance of the parsers. (A parser can handle multiple urls).
    • Check wether an url got his parser.

Install

$ npm install --save price-monitoring

Usage

Product structure

{
  "name": "Samsung Evo microSD 32GB",
  "link": "https://www.amazon.it/Samsung-MB-MP32DA-EU-Memoria-Adattatore/dp/B00J29BR3Y/",
  "price": 14.00,
  "variation": 10
}
  • name: product name or identifier.
  • link: absolute link to the product.
  • price: base price.
  • variation: price variation in % that triggers the priceAlert event.
  • customSelector: [optional] provide a custom selector for a given product instead of using the one of your custom parser.

Get it from a CSV

You can easily provide a CSV list of products.

name, link, price, variation
Samsung Evo microSD 32GB,https://www.amazon.it/Samsung-MB-MP32DA-EU-Memoria-Adattatore/dp/B00J29BR3Y/,14.00,10
...

Main flow

var PriceMonitoring = require('price-monitoring')

var pm = new PriceMonitoring({
  parsersDir: path.join(__dirname, 'myParsers/'), // add parsers
  interval: 60 * 1000 // check prices every min
})

pm.on('priceAlert', function(product, newPrice){
  // do whatever you want
  // ...
})

/* Add the products from CSV */
pm.parseCSV(fileInput) // fileInput can be a csv file path or a Buffer
// or manually
pm.parseAndSubmit({
  name: 'Special price product',
  link: 'https://www.amazon.it/dp/B01MY76N2U?psc=1',
  price: 249.99,
  variation: 18
})

pm.watchPrices()

Every parser works in Parallel, and products prices are fetched sequentially.

The parsersDir property is mandatory. Check out a fully-working sample in the example/ dir.

API

parseAndSubmit(<Object>, <fn>)

Parse and submit a product to his own parser instance. The product object should follow the structure explained above. The callback will return true or false depending on the submission outcome.

removeProduct(<Object>/<link>, <fn>)

Remove the product from his own parser instance matching the given product obj or link. The callback will return true or false if the product link match a registered parser.

parseCSV(<fileInput>/<Buffer>)

Fetch and parse all the products from the provided CSV file or Buffer. It returns a Promise.

fetchAllPrices(cb)

The cb is called once all the prices for all the Parsers have been retrieved.

watchPrices(cb)

Start watching for Prices, the callback is called once all the parsers retrieved all the products prices. The callback returns the current round number. If called multiple times it will not take effect.

isWatchingPrices()

Returns true if the price watch operation is running, false otherwise.

stop()

Stop the prices' watch.

close()

Close all the nightmareDrivers from the parsers' instances. (This avoids zombie processes).

getParsersList()

Returns an Array of the all the Parsers loaded in the current instance of PriceMonitoring with all the submitted products.

getParsersCount()

Returns the number of site-parsers loaded.

getProductsCount()

Returns the number of products added.

getProducts()

Returns an array of all the inserted products.

isWebsiteCovered(<Url>)

Check wether a given website url has his own parser loaded or not.

Events

.on('error', cb)

Triggered in case of error. cb accepts one param, error.

.on('submit', cb)

Triggered when a new product is submitted in a parser' instance. The cb accepts one param, product. (The product being submitted).

.on('skip', cb)

Triggered when a product is skipped (no suitable parser found). The cb accepts one param, product. (The product being skipped).

.on('priceFetched', cb)

Triggered when a new price has been fetched for a given product. The cb accepts 2 params: product, price.

.on('priceAlert', cb)

Triggered when a product's price is outside the variation% threshold. The cb accepts 2 params: product, newPrice.

.on('parserEnd', cb)

Called when a parser ends his price-fetch process for all his products. The cb accepts 2 params: site, nProducts. Respectively the website and the number of products involved.

Build a custom website parser

It's quick:

const SiteParser = require('price-monitoring').SiteParser

/* Class */
class MyEcommerce extends SiteParser {
  constructor () {
    super(
      'https://www.my-ecommerce-website.com', // base url
      '.pricing .price@html | trim | slice:-5 | replaceComma | float' // price selector
    )
  }
}

module.exports = MyEcommerce

That's it. Then don't forget to put your parsers in a directory and give it as parsersDir property to your currente PriceMonitoring instance.

Available filters

  • trim: trim the given string.

  • reverse: reverse the given string.

  • join:<sep>: join the string using the given sep separator.

  • slice:<start>,<end>: split the string from start to end indexes.

  • split:<sep>,<index>: split for the given sep separator and return the elem. at the index.

  • replaceComma: Replace the , char with ., useful to make the price string parsable.

  • float: Cast the given string to float.

Test our website parser in an isolate-manner

var p = new AmazonIT()

p.on('error', console.log)
p.on('submit', console.log)
p.on('priceFetched', function(product, currentPrice){
  console.log(product.name, 'got', currentPrice)
})
p.on('priceAlert', function(product, newPrice){
  console.log('Alert!', product.name, 'got', newPrice)
})
p.on('end', function(){
  console.log('End!')
})

p.addProduct({
    name: 'Special price product',
    link: 'https://www.amazon.it/dp/B01MY76N2U?psc=1',
    price: 20.5,
    variation: 15
  })
  .addProduct({
    name: 'Normal price product',
    link: 'https://www.amazon.it/Samsung-MB-MP32DA-EU-Memoria-Adattatore/dp/B00J29BR3Y/',
    price: 14.00,
    variation: 17
  })
  .fetchPrices()

Notes

Still in Beta

Author

Rocco Musolino

License

MIT

price-monitoring's People

Contributors

roccomuso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar

price-monitoring's Issues

Error: Cannot find module '../../lib/PriceMonitoring'

When I run my code with little changes on example are returning:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '../../lib/PriceMonitoring'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\RMS\price-robot\competitors\RoofingSuperstore.js:2:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at C:\RMS\price-robot\node_modules\price-monitoring\lib\util.js:25:28
    at Array.forEach (<anonymous>)
    at autoload (C:\RMS\price-robot\node_modules\price-monitoring\lib\util.js:23:11)
    at new PriceMonitoring (C:\RMS\price-robot\node_modules\price-monitoring\lib\PriceMonitoring.js:19:23)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node robot.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\RafaelP\AppData\Roaming\npm-cache\_logs\2019-09-11T15_28_33_465Z-debug.log

Doubts about how it works

  • If I set the variation they will consider under or over the number? (like variation 10 the robot will consider just if the change is bigger then 10% or until 10%)

  • My products are being skipped. How the robot triggers the product? How do they compare? How does the robot know which product should be compared? Or only compares with his own price?

How Does It Works

While Importing the Command that you have Mentioned Need To Install npm install --save price-monitoring while hitting the enter it showing that :
npm ERR! code ENOSELF
npm ERR! Refusing to install package with name "price-monitoring" under a package
npm ERR! also called "price-monitoring". Did you name your project the same
npm ERR! as the dependency you're installing?
npm ERR!
npm ERR! For more information, see:
npm ERR! https://docs.npmjs.com/cli/install#limitations-of-npms-install-algorithm

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\RAHUL\AppData\Roaming\npm-cache_logs\2022-04-15T07_30_22_193Z-debug.log

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.