Skip to content
Snippets Groups Projects
index.js 11.5 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 javaxFaces = await getJavaxFaces()
      await authenticate(fields.login, fields.password, javaxFaces)
    
    
    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
    
      const javaxFaces2 = await getJavaxFaces2()
      console.log('\njavaxfaces2:\n',javaxFaces2)
    
      await preGetData(javaxFaces2)
    
    i-vall's avatar
    i-vall committed
      const data = await getData(javaxFaces2)
      await toJSON(data)
    
    i-vall's avatar
    i-vall committed
      log('info', 'Saving data to Cozy')
    
    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')
    
    
    i-vall's avatar
    i-vall committed
    function getJavaxFaces() {
      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
      })
    }
    
    
    i-vall's avatar
    i-vall committed
    function getJavaxFaces2() {
    
    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: {
          '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',
          Pragma: 'no-cache',
          'Cache-Control': 'no-cache',
    
    i-vall's avatar
    i-vall committed
          'DNT': '1'
    
    i-vall's avatar
    i-vall committed
        },
        /*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'
        }*/
    
    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: {
    
    i-vall's avatar
    i-vall committed
          '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-FR;q=0.8,en-US;q=0.5,en;q=0.3',
          'Referer': 'https://monespace.grdf.fr/monespace/particulier/consommation/consommations',
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
          'Faces-Request': 'partial/ajax',
          'X-Requested-With': 'XMLHttpRequest',
          'DNT': '1',
          'Connection': 'keep-alive'
    
    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);
        console.log(data[0])
    
    i-vall's avatar
    i-vall committed
        return data[0]
    
    i-vall's avatar
    i-vall committed
    
      } catch(error) {
          console.log(error)
          throw new Error(errors.VENDOR_DOWN)
      }
    }
    
    
    async function preGetData(javaxFaces){ //POURQUOI? ... à voir
      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: {
          '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': 'en-US,en;q=0.5',
          'Referer': 'https://monespace.grdf.fr/monespace/particulier/consommation/consommations',
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
          'Faces-Request': 'partial/ajax',
          'X-Requested-With': 'XMLHttpRequest',
          'DNT': '1',
          'Connection': 'keep-alive'
        },
        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
    }
    
    i-vall's avatar
    i-vall committed
    
    async function toJSON(data){
      const dataArray = data.slice(19,-1).split(',')
      console.log(dataArray)
      var n = dataArray.length
      const res = dataArray.reduce((accumulator, currentValue) => {
        n --
        return accumulator + '{ "value": ' + currentValue + ', "time": ' + moment().subtract(n, 'day').format('DD/MM/YYYY') + '},'
      }, '')
      return res
    
    }