From 47e4230b3919ef9eb812cf0119fdc547807050a4 Mon Sep 17 00:00:00 2001 From: Quoc Duong Bui <35042166+vanHekthor@users.noreply.github.com> Date: Mon, 19 Dec 2022 19:29:13 +0100 Subject: [PATCH] Provider for immobilien.de (#65) * Add provider for immobilien.de --- lib/provider/immobilienDe.js | 59 ++++++++++++++++++++++++++++++ test/provider/immobilienDe.test.js | 51 ++++++++++++++++++++++++++ test/provider/testProvider.json | 6 ++- 3 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 lib/provider/immobilienDe.js create mode 100644 test/provider/immobilienDe.test.js diff --git a/lib/provider/immobilienDe.js b/lib/provider/immobilienDe.js new file mode 100644 index 0000000..7e5e65b --- /dev/null +++ b/lib/provider/immobilienDe.js @@ -0,0 +1,59 @@ +const utils = require('../utils'); + +let appliedBlackList = []; + +function shortenLink(link) { + return link.substring(0, link.indexOf('?')); +} + +function parseId(shortenedLink) { + return shortenedLink.substring(shortenedLink.lastIndexOf('/') + 1); +} + +function normalize(o) { + const id = parseId(shortenLink(o.link)); + const size = o.size || 'N/A m²'; + const price = o.price || 'N/A €'; + const title = o.title || 'No title available'; + const address = o.address || 'No address available'; + const link = shortenLink(o.link); + return Object.assign(o, { id, price, size, title, address, link }); +} + +function applyBlacklist(o) { + const titleNotBlacklisted = !utils.isOneOf(o.title, appliedBlackList); + const descNotBlacklisted = !utils.isOneOf(o.description, appliedBlackList); + + return titleNotBlacklisted && descNotBlacklisted; +} + +const config = { + url: null, + crawlContainer: '.estates_list .list_immo a._ref', + sortByDateParam: 'sort_col=*created_ts&sort_dir=desc', + crawlFields: { + price: '.list_entry .immo_preis .label_info', + size: '.list_entry .flaeche .label_info | removeNewline | trim', + title: '.list_entry .part_text h3 span', + description: '.list_entry .description | trim', + link: '@href', + address: '.list_entry .place', + }, + paginate: '.list_immo .blocknav .blocknav_list li.next a@href', + normalize: normalize, + filter: applyBlacklist, +}; + +exports.init = (sourceConfig, blacklist) => { + config.enabled = sourceConfig.enabled; + config.url = sourceConfig.url; + appliedBlackList = blacklist || []; +}; + +exports.metaInformation = { + name: 'Immobilien.de', + baseUrl: 'https://www.immobilien.de/', + id: __filename.slice(__dirname.length + 1, -3), +}; + +exports.config = config; diff --git a/test/provider/immobilienDe.test.js b/test/provider/immobilienDe.test.js new file mode 100644 index 0000000..8582bc8 --- /dev/null +++ b/test/provider/immobilienDe.test.js @@ -0,0 +1,51 @@ +const similarityCache = require('../../lib/services/similarity-check/similarityCache'); +const mockNotification = require('../mocks/mockNotification'); +const providerConfig = require('./testProvider.json'); +const mockStore = require('../mocks/mockStore'); +const proxyquire = require('proxyquire').noCallThru(); +const expect = require('chai').expect; +const provider = require('../../lib/provider/immobilienDe'); + +describe('#immobilien.de testsuite()', () => { + after(() => { + similarityCache.stopCacheCleanup(); + }); + + provider.init(providerConfig.immobilienDe, [], []); + const Fredy = proxyquire('../../lib/FredyRuntime', { + './services/storage/listingsStorage': { + ...mockStore, + }, + './notification/notify': mockNotification, + }); + + it('should test immobilien.de provider', async () => { + return await new Promise((resolve) => { + const fredy = new Fredy(provider.config, null, provider.metaInformation.id, 'test1', similarityCache); + fredy.execute().then((listing) => { + expect(listing).to.be.a('array'); + + const notificationObj = mockNotification.get(); + expect(notificationObj).to.be.a('object'); + expect(notificationObj.serviceName).to.equal('immobilienDe'); + notificationObj.payload.forEach((notify) => { + /** check the actual structure **/ + expect(notify.id).to.be.a('string'); + expect(notify.price).to.be.a('string'); + expect(notify.size).to.be.a('string'); + expect(notify.title).to.be.a('string'); + expect(notify.link).to.be.a('string'); + expect(notify.address).to.be.a('string'); + + /** check the values if possible **/ + expect(notify.price).that.does.include('€'); + expect(notify.size).that.does.include('m²'); + expect(notify.title).to.be.not.empty; + expect(notify.link).that.does.include('https://www.immobilien.de'); + expect(notify.address).to.be.not.empty; + }); + resolve(); + }); + }); + }); +}); diff --git a/test/provider/testProvider.json b/test/provider/testProvider.json index 202830d..14dd2ea 100644 --- a/test/provider/testProvider.json +++ b/test/provider/testProvider.json @@ -4,6 +4,10 @@ "enabled": true, "id": "einsAImmobilien" }, + "immobilienDe": { + "url": "https://www.immobilien.de/Wohnen/Suchergebnisse-51797.html?search._digest=true&search._filter=wohnen&search.flaeche_von=50&search.objektart=wohnung&search.preis_bis=1200&search.typ=mieten&search.umkreis=15&search.wo=district%3A2434%2C2695%2C2621%2C2700%2C2967%2C2734%2C2909%2C2955%2C2392%2C2746%2C2767%2C2982%2C2904%2C2612%2C2892%2C2587%2C2871%2C2975%2C2591%2C2887%2C2569%2C2640%2C2735&sort_col=*created_ts&sort_dir=desc", + "enabled": true + }, "immonet": { "url": "https://www.immonet.de/immobiliensuche/sel.do?pageoffset=1&listsize=100&objecttype=1&locationname=Düsseldorf&acid=&actype=&district=8717&district=8718&district=8719&district=8720&district=8721&district=8723&district=8724&district=8725&district=8727&district=8728&district=8729&district=8730&district=8731&district=8732&district=8733&district=8737&district=8738&district=8741&district=8745&district=8747&district=8750&district=8752&district=8754&district=8755&district=8756&district=8759&district=8760&district=8761&district=8763&district=8764&district=8765&ajaxIsRadiusActive=false&sortby=19&suchart=1&radius=0&pcatmtypes=1_1&pCatMTypeStoragefield=&parentcat=1&marketingtype=1&fromprice=&toprice=420000&fromarea=90&toarea=&fromplotarea=&toplotarea=&fromrooms=3&torooms=&objectcat=225&objectcat=18&objectcat=17&objectcat=12&objectcat=16&objectcat=181&objectcat=14&objectcat=15&objectcat=226&objectcat=13&wbs=-1&fromyear=&toyear=", "enabled": true @@ -36,4 +40,4 @@ "url": "https://www.wg-gesucht.de/wg-zimmer-in-Duesseldorf.30.0.1.0.html?offer_filter=1&noDeact=1&city_id=30&category=0&rent_type=0&rMax=5000", "enabled": true } -} \ No newline at end of file +}