Skip to content
Snippets Groups Projects
index.js 9.08 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,
      cozyClient
    
    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 qs = require('querystring')
    const moment = require('moment')
    
    const fs = require('fs')
    
    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
    
    
      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')
    
      const javaxFacesData = await getJavaxFacesData()
    
      await preGetData(javaxFacesData)
      const data = await getData(javaxFacesData)
      console.log(data)
    
    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()
      .subtract(3, 'day')
      .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',
    
    i-vall's avatar
    i-vall committed
        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"]').eq(5).val() //!!!\\ il y en a 10, c'est le 5e
    
    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',
    
    i-vall's avatar
    i-vall committed
        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)
    
    i-vall's avatar
    i-vall committed
          return $('[name = "javax.faces.ViewState"]').eq(7).val() //!!!\\ c'est le 7e
    
    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((error) => {
        console.log(error)
    
    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
        },
    
    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 response = await rp(authRequest)
        console.log(response)
      } catch (error) {
        console.log(error)
        throw new Error(errors.LOGIN_FAILED)
      }
    
    i-vall's avatar
    i-vall committed
    }
    
    
    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
        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=%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
        body:'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%3D6%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
        var data = /donneesCourante = ".+"/.exec(response);
    
        return formating(data[0])
    
    i-vall's avatar
    i-vall committed
    
      } catch(error) {
          console.log(error)
          throw new Error(errors.VENDOR_DOWN)
      }
    }
    
    
    
    async function preGetData(javaxFaces){
    
    i-vall's avatar
    i-vall committed
      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=%2Fviews%2Fconso%2Fdetaille%2FconsoDetailleViewMode.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=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3Aj_idt113&javax.faces.partial.execute=_eConsoconsoDetaille_WAR_eConsoportlet_%3AidFormConsoDetaille%3Aj_idt113&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%3D6%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='+javaxFaces
      }
    
      try {
        const response = await rp(dataRequest)
      } 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){
    
    i-vall's avatar
    i-vall committed
      const dataArray = data.slice(19,-1).split(',')
      console.log(dataArray)
      var n = dataArray.length
    
      return dataArray.map(value => {
    
    i-vall's avatar
    i-vall committed
        n --
    
        return {
          value: parseInt(value, 10),
          time: moment().subtract(n, 'day').format('DD/MM/YYYY')
        }
      })
    }
    
    async function storeData(data){
      return hydrateAndFilter(data, 'grdf.data', {
        keys: ['time']
      }).then(filteredDocuments => {
        addData(filteredDocuments, 'grdf.data')
      })