diff --git a/config.example.json b/config.example.json index 3225284..b6e93a6 100644 --- a/config.example.json +++ b/config.example.json @@ -2,6 +2,7 @@ "apiKeys": { "eanData": "YOURKEYGOESHERE" }, + "email": "you@example.com", "irc": { "host": "irc.freenode.net", "port": 6667, diff --git a/get-barcode.js b/get-barcode.js index 4e18a4e..6c334d6 100644 --- a/get-barcode.js +++ b/get-barcode.js @@ -1,13 +1,13 @@ "use strict"; const Promise = require("bluebird"); -const bhttp = require("bhttp").session({ headers: { "user-agent": "barcodebot (problems: admin@cryto.net)" } }); const dotty = require("dotty"); const cheerio = require("cheerio"); const url = require("url"); const chalk = require("chalk"); const config = require("./config.json"); +const bhttp = require("bhttp").session({ headers: { "user-agent": `barcodebot (problems: ${config.email})` } }); const parseFoodbook = require("./parse-foodbook"); function log(barcode, message) { @@ -37,6 +37,7 @@ let barcodeGetters = [ getBarcode_foodBook, getBarcode_eanData, getBarcode_openFoodFacts, + getBarcode_calorielijst, ]; module.exports = function getBarcode(barcode) { @@ -157,7 +158,7 @@ function getBarcode_foodBook(barcode) { function getBarcode_openFoodFacts(barcode) { return Promise.try(() => { log(barcode, "Trying OpenFoodFacts..."); - + return bhttp.get(`https://world.openfoodfacts.org/api/v0/product/${encodeURIComponent(barcode)}.json`); }).then((response) => { if (response.statusCode === 200 && response.body.status === 1 && response.body.product.product_name != null) { @@ -200,7 +201,7 @@ function getBarcode_buycott(barcode) { } else { productName = product; } - + return { name: productName, image: $(".header_image img").attr("src") @@ -218,13 +219,36 @@ function getBarcode_bolCom(barcode) { if (response.statusCode === 200) { let $ = cheerio.load(response.body); - let productName = cleanValue($(".pdp-header__title").text()); + let productName = cleanValue($(".product-title").text()); + + if (productName.length > 0) { + return { + name: productName, + image: $(".product-image img").attr("src").replace("/regular/", "/extralarge/"), + price: `€ ${cleanValue($("meta[itemprop=price]").attr('content')).replace(".", ",")}`, + }; + } + } + }); +} + +function getBarcode_calorielijst(barcode) { + return Promise.try(() => { + log(barcode, "Trying calorielijst.nl..."); + + return bhttp.get(`http://www.calorielijst.nl/?search=${encodeURIComponent(barcode)}`); + }).then((response) => { + if (response.statusCode === 200) { + let $ = cheerio.load(response.body); + + let a = $(".calorienamediv a").first(); + let productName = cleanValue(a.text()); if (productName.length > 0) { + let image = a.data().id; return { name: productName, - image: $(".js_product_img").attr("src"), - price: `€ ${cleanValue($(".promo-price").text())}` + image: (image != "") ? `http://img.calorielijst.nl/product/${image}` : null, }; } } diff --git a/test.js b/test.js index 70cfc63..f95c37c 100644 --- a/test.js +++ b/test.js @@ -5,7 +5,7 @@ const Promise = require("bluebird"); const getBarcode = require("./get-barcode"); Promise.try(() => { - return getBarcode("4002846034450"); + return getBarcode("8716309087797"); }).then((result) => { console.log(result); })