From 43281fd5b34cf3ff32aa6b4f39c90ee3e3947f0f Mon Sep 17 00:00:00 2001 From: Peetz0r Date: Sun, 29 Mar 2020 16:57:30 +0200 Subject: [PATCH 1/3] Configurable email address for user agent --- config.example.json | 1 + get-barcode.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) 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..342c4b3 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) { @@ -157,7 +157,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 +200,7 @@ function getBarcode_buycott(barcode) { } else { productName = product; } - + return { name: productName, image: $(".header_image img").attr("src") -- 2.33.3 From 818132e8f13f0dee6513caa2035acbf1b0624d61 Mon Sep 17 00:00:00 2001 From: Peetz0r Date: Sun, 29 Mar 2020 17:57:10 +0200 Subject: [PATCH 2/3] Calorielijst added as source --- get-barcode.js | 25 +++++++++++++++++++++++++ test.js | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/get-barcode.js b/get-barcode.js index 342c4b3..43a1f1e 100644 --- a/get-barcode.js +++ b/get-barcode.js @@ -37,6 +37,7 @@ let barcodeGetters = [ getBarcode_foodBook, getBarcode_eanData, getBarcode_openFoodFacts, + getBarcode_calorielijst, ]; module.exports = function getBarcode(barcode) { @@ -230,3 +231,27 @@ function getBarcode_bolCom(barcode) { } }); } + +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: (image != "") ? `http://img.calorielijst.nl/product/${image}` : null, + //~ price: "N/A", + }; + } + } + }); +} diff --git a/test.js b/test.js index 70cfc63..7f6ceba 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("8718265015388"); }).then((result) => { console.log(result); }) -- 2.33.3 From 321c7e5f3213b73ba54021694ce647ae4a94c073 Mon Sep 17 00:00:00 2001 From: Peetz0r Date: Sun, 29 Mar 2020 18:17:21 +0200 Subject: [PATCH 3/3] bol.com fixes --- get-barcode.js | 7 +++---- test.js | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/get-barcode.js b/get-barcode.js index 43a1f1e..6c334d6 100644 --- a/get-barcode.js +++ b/get-barcode.js @@ -219,13 +219,13 @@ 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: $(".js_product_img").attr("src"), - price: `€ ${cleanValue($(".promo-price").text())}` + image: $(".product-image img").attr("src").replace("/regular/", "/extralarge/"), + price: `€ ${cleanValue($("meta[itemprop=price]").attr('content')).replace(".", ",")}`, }; } } @@ -249,7 +249,6 @@ function getBarcode_calorielijst(barcode) { return { name: productName, image: (image != "") ? `http://img.calorielijst.nl/product/${image}` : null, - //~ price: "N/A", }; } } diff --git a/test.js b/test.js index 7f6ceba..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("8718265015388"); + return getBarcode("8716309087797"); }).then((result) => { console.log(result); }) -- 2.33.3