Skip to content

Commit

Permalink
Support for moreselli
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomatiasgomez committed Sep 20, 2024
1 parent 92da219 commit 4b9c0dc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/connector/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -78,6 +79,7 @@ const SITE_BROWSERS = [
new GrupoMegaBrowser(),
new GrupoMegaListingsBrowser(),
new MudafyListingsBrowser(),
new MorselliBrowser(),
];

const BROWSER_KINDS = {
Expand Down
5 changes: 5 additions & 0 deletions src/connector/browsers/maluma-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
24 changes: 24 additions & 0 deletions src/connector/browsers/morselli-browser.js
Original file line number Diff line number Diff line change
@@ -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;
60 changes: 24 additions & 36 deletions src/connector/browsers/xintel-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 4b9c0dc

Please sign in to comment.