Skip to content
Snippets Groups Projects
index.js 9.32 KiB
Newer Older
  • Learn to ignore specific revisions
  • i-vall's avatar
    i-vall committed
    const {
      BaseKonnector,
      log,
    
    i-vall's avatar
    i-vall committed
      errors,
      addData,
    
      hydrateAndFilter
    
    i-vall's avatar
    i-vall committed
    } = require('cozy-konnector-libs')
    
    i-vall's avatar
    i-vall committed
    const rp = require('request-promise')
    const cookiejar = rp.jar()
    const cheerio = require('cheerio')
    const moment = require('moment')
    
    
    i-vall's avatar
    i-vall committed
    module.exports = new BaseKonnector(start)
    
    // The start function is run by the BaseKonnector instance only when it got all the account
    // information (fields). When you run this connector yourself in "standalone" mode or "dev" mode,
    // the account information come from ./konnector-dev-config.json file
    async function start(fields) {
      log('info', 'Authenticating ...')
    
    i-vall's avatar
    i-vall committed
    
    
    i-vall's avatar
    i-vall committed
      //get the javax state from the auth page
    
      const javaxFacesAuth = await getJavaxFacesAuth()
      await authenticate(fields.login, fields.password, javaxFacesAuth)
    
    i-vall's avatar
    i-vall committed
    
    
    i-vall's avatar
    i-vall committed
      log('info', 'Successfully logged in')
    
    i-vall's avatar
    i-vall committed
      log('info', 'Fetching the data')
    
    i-vall's avatar
    i-vall committed
      // get the javax state from the consommations page
    
      const javaxFacesData = await getJavaxFacesData()
    
      await preGetData(javaxFacesData)
    
      const data = await getData(javaxFacesData)
    
    i-vall's avatar
    i-vall committed
      log('info', 'Saving data to Cozy')
    
      await storeData(data)
    
    i-vall's avatar
    i-vall committed
    }
    
    
    i-vall's avatar
    i-vall committed
    const startDate = moment()
    
    i-vall's avatar
    i-vall committed
      .subtract(10, 'day')
    
    i-vall's avatar
    i-vall committed
      .format('DD/MM/YYYY')
    const endDate = moment().format('DD/MM/YYYY')
    
    
    function getJavaxFacesAuth() {
    
    i-vall's avatar
    i-vall committed
      const javaxFacesRequest = {
    
    i-vall's avatar
    i-vall committed
        method: 'GET',
    
        uri: 'https://monespace.grdf.fr/monespace/connexion',
    
    i-vall's avatar
    i-vall committed
        jar: cookiejar,
        headers: {
    
    i-vall's avatar
    i-vall committed
          Host: 'monespace.grdf.fr'
        },
        followAllRedirects: true,
        transform: function(body) {
          const $ = cheerio.load(body)
    
          return $('[name = "javax.faces.ViewState"]')
    
    i-vall's avatar
    i-vall committed
            .eq(5) //!!!\\ il y en a 10, c'est le 5e
            .val()
    
    i-vall's avatar
    i-vall committed
        }
    
    i-vall's avatar
    i-vall committed
      }
    
    i-vall's avatar
    i-vall committed
    
      return rp(javaxFacesRequest).catch(() => {
    
    i-vall's avatar
    i-vall committed
        throw new Error(errors.VENDOR_DOWN)
    
    i-vall's avatar
    i-vall committed
      })
    }
    
    
    function getJavaxFacesData() {
    
    i-vall's avatar
    i-vall committed
      const javaxFacesRequest = {
        method: 'GET',
    
        uri:
          'https://monespace.grdf.fr/monespace/particulier/consommation/consommations',
    
    i-vall's avatar
    i-vall committed
        jar: cookiejar,
        headers: {
          Host: 'monespace.grdf.fr'
    
    i-vall's avatar
    i-vall committed
        },
    
    i-vall's avatar
    i-vall committed
        transform: function(body) {
          const $ = cheerio.load(body)
    
          return $('[name = "javax.faces.ViewState"]')
    
    i-vall's avatar
    i-vall committed
            .eq(7) //!!!\\ c'est le 7e
            .val()
    
    i-vall's avatar
    i-vall committed
        }
    
    i-vall's avatar
    i-vall committed
      }
    
    
      return rp(javaxFacesRequest).catch(() => {
    
    i-vall's avatar
    i-vall committed
        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: {
    
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    
    i-vall's avatar
    i-vall committed
        },
    
        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
    
    i-vall's avatar
    i-vall committed
      }
    
      try {
    
        const res = await rp(authRequest)
        log('debug', res)
    
    i-vall's avatar
    i-vall committed
      } catch (error) {
        throw new Error(errors.LOGIN_FAILED)
      }
    
    i-vall's avatar
    i-vall committed
    }
    
    
    async function getData(javaxFaces) {
    
    i-vall's avatar
    i-vall committed
      const dataRequest = {
        method: 'POST',
    
    i-vall's avatar
    i-vall committed
          '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=5&p_p_col_pos=3&_eConsoconsoDetaille_WAR_eConsoportlet__jsfBridgeAjax=true&_eConsoconsoDetaille_WAR_eConsoportlet__facesViewIdResource=%2Fviews%2Fconso%2Fdetaille%2FconsoDetailleViewMode.xhtml',
    
    i-vall's avatar
    i-vall committed
        jar: cookiejar,
        headers: {
    
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    
    i-vall's avatar
    i-vall committed
        },
    
    i-vall's avatar
    i-vall committed
          'javax.faces.partial.ajax=true&javax.faces.source=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ApanelTypeGranularite1%3A2&javax.faces.partial.execute=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ApanelTypeGranularite1&javax.faces.partial.render=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ArefreshHighchart+_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AupdateDatesBean+_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AboutonTelechargerDonnees+_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ApanelTypeGranularite+_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AidBlocSeuilParametrage&javax.faces.behavior.event=valueChange&javax.faces.partial.event=change&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille&javax.faces.encodedURL=https%3A%2F%2Fmonespace.grdf.fr%2Fweb%2Fguest%2Fmonespace%2Fparticulier%2Fconsommation%2Fconsommations%3Fp_p_id%3DeConsoconsoDetaille_WAR_eConsoportlet%26p_p_lifecycle%3D2%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_cacheability%3DcacheLevelPage%26p_p_col_id%3Dcolumn-3%26p_p_col_count%3D5%26p_p_col_pos%3D3%26_eConsoconsoDetaille_WAR_eConsoportlet__jsfBridgeAjax%3Dtrue%26_eConsoconsoDetaille_WAR_eConsoportlet__facesViewIdResource%3D%252Fviews%252Fconso%252Fdetaille%252FconsoDetailleViewMode.xhtml&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AidDateDebutConsoDetaille=' +
    
          startDate +
          '&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AidDateFinConsoDetaille=' +
          endDate +
          '&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ApanelTypeGranularite1=jour&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3ApanelTypeGranularite3=mois&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AselecteurVolumeType2=kwh&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3AselecteurVolumeType4=kwh&javax.faces.ViewState=' +
          javaxFaces
    
    i-vall's avatar
    i-vall committed
      }
    
      try {
        const response = await rp(dataRequest)
    
    i-vall's avatar
    i-vall committed
        //log('debug', 'reponse getData: ' + response)
    
        var data = /donneesCourante = ".+"/.exec(response)
        var dates = /tooltipDatesInfo = ".+"/.exec(response)
        return formating(data[0], dates[0])
      } catch (error) {
        throw new Error(errors.VENDOR_DOWN)
    
    async function preGetData(javaxFaces) {
    
    i-vall's avatar
    i-vall committed
      const dataRequest = {
        method: 'POST',
    
    i-vall's avatar
    i-vall committed
          '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=5&p_p_col_pos=3&_eConsoconsoDetaille_WAR_eConsoportlet__jsfBridgeAjax=true&_eConsoconsoDetaille_WAR_eConsoportlet__facesViewIdResource=%2Fviews%2Fconso%2Fdetaille%2FconsoDetailleViewMode.xhtml',
    
    i-vall's avatar
    i-vall committed
        jar: cookiejar,
        headers: {
    
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    
    i-vall's avatar
    i-vall committed
        },
    
    i-vall's avatar
    i-vall committed
          'javax.faces.partial.ajax=true&javax.faces.source=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3Aj_idt139&javax.faces.partial.execute=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3Aj_idt139&javax.faces.partial.render=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille&javax.faces.behavior.event=click&javax.faces.partial.event=click&_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille&javax.faces.encodedURL=https%3A%2F%2Fmonespace.grdf.fr%2Fweb%2Fguest%2Fmonespace%2Fparticulier%2Fconsommation%2Fconsommations%3Fp_p_id%3DeConsoconsoDetaille_WAR_eConsoportlet%26p_p_lifecycle%3D2%26p_p_state%3Dnormal%26p_p_mode%3Dview%26p_p_cacheability%3DcacheLevelPage%26p_p_col_id%3Dcolumn-3%26p_p_col_count%3D5%26p_p_col_pos%3D3%26_eConsoconsoDetaille_WAR_eConsoportlet__jsfBridgeAjax%3Dtrue%26_eConsoconsoDetaille_WAR_eConsoportlet__facesViewIdResource%3D%252Fviews%252Fconso%252Fdetaille%252FconsoDetailleViewMode.xhtml&javax.faces.ViewState=' +
    
    i-vall's avatar
    i-vall committed
        const response = await rp(dataRequest)
        return response
    
      } catch (error) {
    
    i-vall's avatar
    i-vall committed
        throw new Error(errors.VENDOR_DOWN)
      }
    
    i-vall's avatar
    i-vall committed
    }
    
    function formating(data, dates) {
      const dataArray = data.slice(19, -1).split(',')
      const dateArray = dates.slice(23, -1).split(',Le ')
      var n = -1
    
      return dataArray.map(value => {
    
          load: parseInt(value, 10),
          time: moment.utc(dateArray[n], 'DD/MM/YYYY').format('YYYY-MM-DD')
    
    async function storeData(data) {
    
      return hydrateAndFilter(data, 'grdf.data', {
        keys: ['time']
      }).then(filteredDocuments => {
        addData(filteredDocuments, 'grdf.data')
      })
    
    i-vall's avatar
    i-vall committed
    }