From 4b9c0dc94717b302274fbef494017ba5104eff4d Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Fri, 20 Sep 2024 16:54:58 -0300 Subject: [PATCH] Support for moreselli --- README.md | 2 + src/config.js | 6 +-- src/connector/browser.js | 2 + src/connector/browsers/maluma-browser.js | 5 ++ src/connector/browsers/morselli-browser.js | 24 +++++++++ src/connector/browsers/xintel-browser.js | 60 +++++++++------------- 6 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 src/connector/browsers/morselli-browser.js diff --git a/README.md b/README.md index 0d5ad4d..2dffd42 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ The currently supported websites are the following: | [MenendezProp](http://www.menendezprop.com.ar/) | ✅ | ✅ | | [GrupoMega](https://www.grupomega.com.ar/index.php) | ✅ | ✅ | | [Mudafy](https://mudafy.com.ar/) | | ✅ | +| [Morselli](https://morselli.com.ar/) | | ✅ | ## Prerequisites @@ -107,6 +108,7 @@ Where: * `files` - Reads each file in `files` (path relative to the project folder), line by line and ignores lines that start with "//" or are empty. * `broser` - configures how the browser will run and fetch the pages + * `xintnelApiKey` - apiKey to use when fetching xintel pages * `timeBetweenPageFetchesMs` - Time to wait between each page fetch * `telegram` - Telegram configuration used to notify the changes: * `token` - bot token, provided when you create the bot diff --git a/src/config.js b/src/config.js index bd789ce..632dced 100755 --- a/src/config.js +++ b/src/config.js @@ -16,12 +16,12 @@ let Config = { } }, "browser": { + "xintelApiKey": "", "timeBetweenPageFetchesMs": 16000 }, "telegram": { - "token": null, - // Can be retrieved using https://api.telegram.org/bot{TOKEN}/getUpdates - "chatId": null + "token": "", + "chatId": "" // Can be retrieved using https://api.telegram.org/bot{TOKEN}/getUpdates } }; diff --git a/src/connector/browser.js b/src/connector/browser.js index 43ce48b..5583d36 100644 --- a/src/connector/browser.js +++ b/src/connector/browser.js @@ -37,6 +37,7 @@ const LiderPropListingsBrowser = require('./browsers/liderprop-listings-browser. const GrupoMegaBrowser = require('./browsers/grupomega-browser.js'); const GrupoMegaListingsBrowser = require('./browsers/grupomega-listings-browser.js'); const MudafyListingsBrowser = require("./browsers/mudafy-listings-browser"); +const MorselliBrowser = require("./browsers/morselli-browser"); const Utils = require('../utils/utils.js'); @@ -78,6 +79,7 @@ const SITE_BROWSERS = [ new GrupoMegaBrowser(), new GrupoMegaListingsBrowser(), new MudafyListingsBrowser(), + new MorselliBrowser(), ]; const BROWSER_KINDS = { diff --git a/src/connector/browsers/maluma-browser.js b/src/connector/browsers/maluma-browser.js index 156c065..3cfaa90 100644 --- a/src/connector/browsers/maluma-browser.js +++ b/src/connector/browsers/maluma-browser.js @@ -8,6 +8,11 @@ const URL_REGEX = /^https?:\/\/(?:www\.)?maluma\.com\.ar\/.*MLM(\d+).*$/i; class MalumaBrowser extends XintelBrowser { + getXintelId(url) { + let splits = url.split("-"); + return splits[splits.length - 1]; + } + constructor() { super(URL_REGEX); } diff --git a/src/connector/browsers/morselli-browser.js b/src/connector/browsers/morselli-browser.js new file mode 100644 index 0000000..31fea54 --- /dev/null +++ b/src/connector/browsers/morselli-browser.js @@ -0,0 +1,24 @@ +'use strict'; + +const XintelBrowser = require('./xintel-browser.js'); + +//--------------- + +const URL_REGEX = /^https?:\/\/(?:www\.)?morselli\.com\.ar\/propiedad\.php\?reference_code=MII(\d+).*$/i; + +class MorselliBrowser extends XintelBrowser { + + getXintelId(url) { + let splits = url.split("="); + return splits[splits.length - 1]; + } + + constructor() { + super(URL_REGEX); + } +} + + +// --------- + +module.exports = MorselliBrowser; diff --git a/src/connector/browsers/xintel-browser.js b/src/connector/browsers/xintel-browser.js index 921d5a0..8e9359e 100644 --- a/src/connector/browsers/xintel-browser.js +++ b/src/connector/browsers/xintel-browser.js @@ -15,49 +15,37 @@ class XintelBrowser extends SiteBrowser { super(urlRegex); } + /** + * This method must be implemented and should extract the ID from the url + * @param url current url + */ + getXintelId(url) { + throw new Error("Method must be implemented!"); + } + extractData(browserPage) { logger.info(`Extracting data...`); - return browserPage.evaluate(() => { - return new Promise((resolve, reject) => { - let response = { - EXPORT_VERSION: "1" - }; - - let script = [...document.getElementsByTagName("script")] - .filter(script => script.innerText.indexOf("fichas.propiedades") !== -1) - [0] - .innerText; - - let successFnStr = "success:function(response){"; - let startingFunctionIndex = script.indexOf(successFnStr); - if (startingFunctionIndex === -1) throw new Error("Couldn't find success fn!"); - script = script.substring(0, startingFunctionIndex + successFnStr.length); + let xintelId = this.getXintelId(browserPage.url()); + logger.info(`Using xintelId ${xintelId}...`); - /** - * Create global fns where we will be called. - * @param {{ resultado: Object }} r - ajax response - */ - window.customOnSuccessFn = function (r) { - Object.assign(response, r.resultado); - resolve(response); - }; - window.customOnError = function (err) { - reject(err); - }; + return browserPage.evaluate((xintelApiKey, xintelId) => { + let response = { + EXPORT_VERSION: "1" + }; - // success:function(response){ - script += ` - window.customOnSuccessFn(response); - }, - error: function(err) { - window.customOnError(err) - } - });`; + let code = xintelId.substring(0, 3); + let id = xintelId.substring(3); + let apiURL = `https://xintelapi.com.ar/?cache=20092024&json=fichas.propiedades&amaira=false&suc=${code}&global=LU3AIKPR4F6ZSUY8GQODKWRO8&emprendimiento=True&oppel=&esweb=&apiK=${xintelApiKey}&id=${id}&_=${Date.now()}`; - eval(script); // jshint ignore:line + return fetch(apiURL).then(response => { + if (!response.ok) throw new Error("Failed to fetch data" + response); + return response.json(); + }).then(responseJson => { + Object.assign(response, responseJson.resultado); + return response; }); - }); + }, config.browser.xintelApiKey, xintelId); } }