diff --git a/source/package.json b/source/package.json index f1dd9d48b2f1d64554683c2d3b7ec2ac6bab9556..6f02019320eb286d106d98dfb2e547d1da633dac 100644 --- a/source/package.json +++ b/source/package.json @@ -38,7 +38,8 @@ "travisDeployKey": "./bin/generate_travis_deploy_key" }, "dependencies": { - "cozy-konnector-libs": "4.15.3" + "cozy-konnector-libs": "4.15.3", + "moment": "^2.24.0" }, "devDependencies": { "copy-webpack-plugin": "5.0.2", diff --git a/source/src/index.js b/source/src/index.js index 08d5d81de125a6064a8deef590116f1367cd52b9..c006bc8b4891c6dcd57056fe170b0bc406c36c00 100644 --- a/source/src/index.js +++ b/source/src/index.js @@ -1,27 +1,18 @@ const { BaseKonnector, - requestFactory, - signin, - scrape, - saveBills, log, - utils + errors, + addData, + hydrateAndFilter, + cozyClient } = require('cozy-konnector-libs') -const request = requestFactory({ - // The debug mode shows all the details about HTTP requests and responses. Very useful for - // debugging but very verbose. This is why it is commented out by default - // debug: true, - // Activates [cheerio](https://cheerio.js.org/) parsing on each page - cheerio: true, - // If cheerio is activated do not forget to deactivate json parsing (which is activated by - // default in cozy-konnector-libs - json: false, - // This allows request-promise to keep cookies between requests - jar: true -}) - -const VENDOR = 'template' -const baseUrl = 'http://books.toscrape.com' +const rp = require('request-promise') +const cookiejar = rp.jar() +const cheerio = require('cheerio') +const qs = require('querystring') +const moment = require('moment') + +const fs = require('fs') module.exports = new BaseKonnector(start) @@ -30,101 +21,153 @@ module.exports = new BaseKonnector(start) // the account information come from ./konnector-dev-config.json file async function start(fields) { log('info', 'Authenticating ...') - await authenticate(fields.login, fields.password) + //await preAuth() + + const javaxFaces = await getJavaxFaces() + + // console.log('first cookie jar:\n', cookiejar) + console.log(javaxFaces) + + await authenticate(fields.login, fields.password, javaxFaces) + + // console.log('\n\n\nsecond cookie jar:\n', cookiejar) + log('info', 'Successfully logged in') // The BaseKonnector instance expects a Promise as return of the function - log('info', 'Fetching the list of documents') - const $ = await request(`${baseUrl}/index.html`) - // cheerio (https://cheerio.js.org/) uses the same api as jQuery (http://jquery.com/) - log('info', 'Parsing list of documents') - const documents = await parseDocuments($) - - // Here we use the saveBills function even if what we fetch are not bills, - // but this is the most common case in connectors + log('info', 'Fetching the data') + //await getData() log('info', 'Saving data to Cozy') - await saveBills(documents, fields, { - // This is a bank identifier which will be used to link bills to bank operations. These - // identifiers should be at least a word found in the title of a bank operation related to this - // bill. It is not case sensitive. - identifiers: ['books'] - }) } -// This shows authentication using the [signin function](https://github.com/konnectors/libs/blob/master/packages/cozy-konnector-libs/docs/api.md#module_signin) -// even if this in another domain here, but it works as an example -function authenticate(username, password) { - return signin({ - url: `http://quotes.toscrape.com/login`, - formSelector: 'form', - formData: { username, password }, - // The validate function will check if the login request was a success. Every website has a - // different way to respond: HTTP status code, error message in HTML ($), HTTP redirection - // (fullResponse.request.uri.href)... - validate: (statusCode, $, fullResponse) => { - log( - 'debug', - fullResponse.request.uri.href, - 'not used here but should be useful for other connectors' - ) - // The login in toscrape.com always works except when no password is set - if ($(`a[href='/logout']`).length === 1) { - return true - } else { - // cozy-konnector-libs has its own logging function which format these logs with colors in - // standalone and dev mode and as JSON in production mode - log('error', $('.error').text()) - return false - } +const startDate = moment() + .subtract(3, 'day') + .format('DD/MM/YYYY') +const endDate = moment().format('DD/MM/YYYY') + +function preAuth() { + const preAuthRequest = { + method: 'GET', + uri: 'https://monespace.grdf.fr/monespace/connexion', + jar: cookiejar, + headers: { + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Cache-Control': 'max-age=0', + Dnt: '1', + 'User-Agent': + 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0', + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + 'Accept-Language': 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3', + Referer: 'https://www.grdf.fr/', + Connection: 'keep-alive' } + } + return rp(preAuthRequest).catch(() => { + throw new Error(errors.VENDOR_DOWN) }) } -// The goal of this function is to parse a HTML page wrapped by a cheerio instance -// and return an array of JS objects which will be saved to the cozy by saveBills -// (https://github.com/konnectors/libs/blob/master/packages/cozy-konnector-libs/docs/api.md#savebills) -function parseDocuments($) { - // You can find documentation about the scrape function here: - // https://github.com/konnectors/libs/blob/master/packages/cozy-konnector-libs/docs/api.md#scrape - const docs = scrape( - $, - { - title: { - sel: 'h3 a', - attr: 'title' - }, - amount: { - sel: '.price_color', - parse: normalizePrice - }, - fileurl: { - sel: 'img', - attr: 'src', - parse: src => `${baseUrl}/${src}` - } +function getJavaxFaces() { + const javaxFacesRequest = { + method: 'GET', + uri: 'https://monespace.grdf.fr/monespace/connexion', + jar: cookiejar, + headers: { + Host: 'monespace.grdf.fr' }, - 'article' - ) - return docs.map(doc => ({ - ...doc, - // The saveBills function needs a date field - // even if it is a little artificial here (these are not real bills) - date: new Date(), - currency: 'EUR', - filename: `${utils.formatDate(new Date())}_${VENDOR}_${doc.amount.toFixed( - 2 - )}EUR${doc.vendorRef ? '_' + doc.vendorRef : ''}.jpg`, - vendor: VENDOR, - metadata: { - // It can be interesting to add the date of import. This is not mandatory but may be - // useful for debugging or data migration - importDate: new Date(), - // Document version, useful for migration after change of document structure - version: 1 + followAllRedirects: true, + transform: function(body) { + const $ = cheerio.load(body) + return $('.mdpOublie') + .next() + .val() //!!!\\ il y en a 10, c'est le 5e } - })) + } + + return rp(javaxFacesRequest).catch(() => { + throw new Error(errors.VENDOR_DOWN) + }) +} + +async function authenticate(login, password, javaxFaces) { + const authRequest = { + method: 'POST', + uri: + 'https://monespace.grdf.fr/web/guest/monespace?p_p_id=EspacePerso_WAR_EPportlet&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_cacheability=cacheLevelPage&p_p_col_id=column-2&p_p_col_count=1&_EspacePerso_WAR_EPportlet__jsfBridgeAjax=true&_EspacePerso_WAR_EPportlet__facesViewIdResource=%2Fviews%2FespacePerso%2FseconnecterEspaceViewMode.xhtml', + jar: cookiejar, + headers: { + 'User-Agent': + 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:66.0) Gecko/20100101 Firefox/66.0', + Accept: 'application/xml, text/xml, */*; q=0.01', + 'Accept-Language': 'fr-FR,en-US;q=0.7,en;q=0.3', + Referer: 'https://monespace.grdf.fr/monespace/connexion', + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'Faces-Request': 'partial/ajax', + 'X-Requested-With': 'XMLHttpRequest', + Connection: 'keep-alive', + //Cookie: 'dtCookie=DE9B4CA6869881C055D6E699F3CFDFB8|1|2||1; JSESSIONID_EP=25559043075796265A8F2087CF27E404; ROUTEID_EP=.2; COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=fr_FR', + Pragma: 'no-cache', + 'Cache-Control': 'no-cache', + DNT: '1' + }, + /*form: { + 'javax.faces.partial.ajax': 'true', + 'javax.faces.source': '_EspacePerso_WAR_EPportlet_:seConnecterForm%3AmeConnecter', + 'javax.faces.partial.execute':'_EspacePerso_WAR_EPportlet_:seConnecterForm', + 'javax.faces.partial.render': '_EspacePerso_WAR_EPportlet_:global+_EspacePerso_WAR_EPportlet_:groupTitre', + 'javax.faces.behavior.event': 'click', + 'javax.faces.partial.event': 'click', + '_EspacePerso_WAR_EPportlet_:seConnecterForm': '_EspacePerso_WAR_EPportlet_:seConnecterForm', + 'javax.faces.encodedURL': 'https://monespace.grdf.fr/web/guest/monespace?p_p_id=EspacePerso_WAR_EPportlet&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_cacheability=cacheLevelPage&p_p_col_id=column-2&p_p_col_count=1&_EspacePerso_WAR_EPportlet__jsfBridgeAjax=true&_EspacePerso_WAR_EPportlet__facesViewIdResource=%2Fviews%2FespacePerso%2FseconnecterEspaceViewMode.xhtml', + '_EspacePerso_WAR_EPportlet_:seConnecterForm:email': 'raphael.youssoufian@club-internet.fr', + '_EspacePerso_WAR_EPportlet_:seConnecterForm:passwordSecretSeConnecter': 'MDEmde77', + 'javax.faces.ViewState': '880647373313844401:-7107293260060007058' + }*/ + body: + 'javax.faces.partial.ajax=true&javax.faces.source=_EspacePerso_WAR_EPportlet_%3AseConnecterForm%3AmeConnecter&javax.faces.partial.execute=_EspacePerso_WAR_EPportlet_%3AseConnecterForm&javax.faces.partial.render=_EspacePerso_WAR_EPportlet_%3Aglobal+_EspacePerso_WAR_EPportlet_%3AgroupTitre&javax.faces.behavior.event=click&javax.faces.partial.event=click&_EspacePerso_WAR_EPportlet_%3AseConnecterForm=_EspacePerso_WAR_EPportlet_%3AseConnecterForm&javax.faces.encodedURL=https%3A%2F%2Fmonespace.grdf.fr%2Fweb%2Fguest%2Fmonespace%3Fp_p_id%3DEspacePerso_WAR_EPportlet%26p_p_lifecycle%3D2%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_cacheability%3DcacheLevelPage%26p_p_col_id%3Dcolumn-2%26p_p_col_count%3D1%26_EspacePerso_WAR_EPportlet__jsfBridgeAjax%3Dtrue%26_EspacePerso_WAR_EPportlet__facesViewIdResource%3D%252Fviews%252FespacePerso%252FseconnecterEspaceViewMode.xhtml&_EspacePerso_WAR_EPportlet_%3AseConnecterForm%3Aemail=' + + login + + '&_EspacePerso_WAR_EPportlet_%3AseConnecterForm%3ApasswordSecretSeConnecter=' + + password + + '&javax.faces.ViewState=' + + javaxFaces + } + + try { + const response = await rp(authRequest) + console.log(response) + fs.writeFile('response.html', response, err => { + if (err) throw err + console.log('The file has been saved!') + }) + //console.log(cookiejar) + } catch (error) { + fs.writeFile('error.html', error, err => { + if (err) throw err + console.log('The file has been saved!') + }) + console.log(error) + //console.log(cookiejar) + throw new Error(errors.LOGIN_FAILED) + } } -// Convert a price string to a float -function normalizePrice(price) { - return parseFloat(price.replace('£', '').trim()) +async function getData() { + const dataRequest = { + method: 'POST', + uri: + 'https://monespace.grdf.fr/web/guest/monespace/particulier/consommation/consommations?p_p_id=eConsoconsoDetaille_WAR_eConsoportlet&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_cacheability=cacheLevelPage&p_p_col_id=column-3&p_p_col_count=6&p_p_col_pos=3&_eConsoconsoDetaille_WAR_eConsoportlet__jsfBridgeAjax=true&_eConsoconsoDetaille_WAR_eConsoportlet__facesViewIdResource=/views/conso/detaille/consoDetailleViewMode.xhtml', + jar: cookiejar, + headers: { + Host: 'monespace.grdf.fr', + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: '' + } + + try { + const response = await rp(dataRequest) + var data = /donneesCourante = ".+"/.exec(response) + console.log(response) + } catch (error) { + throw new Error(errors.VENDOR_DOWN) + } } diff --git a/source/yarn.lock b/source/yarn.lock index e119f9ab28d19193f999c6ead99aea7d58ce57a8..e5609b70fbaf6e0ec416a41d6cda20abe6624b24 100644 --- a/source/yarn.lock +++ b/source/yarn.lock @@ -3547,6 +3547,11 @@ mkdirp@0.5.1, mkdirp@0.x.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@ dependencies: minimist "0.0.8" +moment@^2.24.0: + version "2.24.0" + resolved "https://registry.yarnpkg.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" + integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg== + move-concurrently@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"