Connecteur COZY permettant de récupérer et stocker ses factures Enercoop.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

102 rindas
2.7 KiB

  1. const moment = require('moment')
  2. const {log, BaseKonnector, saveBills, requestFactory} = require('cozy-konnector-libs')
  3. const baseUrl = 'https://espace-client.enercoop.fr'
  4. const loginUrl = baseUrl + '/login'
  5. const billUrl = baseUrl + '/mon-espace/factures/'
  6. moment.locale('fr')
  7. let rq = requestFactory({
  8. cheerio: true,
  9. json: false,
  10. debug: false,
  11. jar: true
  12. })
  13. module.exports = new BaseKonnector(function fetch (fields) {
  14. return logIn(fields)
  15. .then(parsePage)
  16. .then(entries => saveBills(entries, fields.folderPath, {
  17. timeout: Date.now() + 60 * 1000,
  18. identifiers: ['Enercoop']
  19. }))
  20. })
  21. // Procedure to login to Enercoop website.
  22. function logIn (fields) {
  23. const form = {
  24. email: fields.login,
  25. password: fields.password,
  26. }
  27. const options = {
  28. url: loginUrl,
  29. method: 'POST',
  30. form: form,
  31. resolveWithFullResponse: true,
  32. followAllRedirects: true,
  33. simple: false
  34. }
  35. return rq(options)
  36. .then(res => {
  37. //const isNoLocation = !res.headers.location
  38. const isNot200 = res.statusCode !== 200
  39. //const isError = res.headers.location && res.headers.location.indexOf('error') !== -1
  40. if (isNot200) {
  41. log('info', 'Authentification error')
  42. throw new Error('LOGIN_FAILED')
  43. }
  44. const url = `${billUrl}`
  45. return rq(url)
  46. .catch(err => {
  47. console.log(err, 'authentication error details')
  48. throw new Error('LOGIN_FAILED')
  49. })
  50. })
  51. }
  52. // Parse the fetched page to extract bill data.
  53. function parsePage ($) {
  54. const bills = []
  55. $('.invoice-line').each(function () {
  56. //console.log($(this).html())
  57. let billId = $(this).data('invoice-id')
  58. let amount = $(this).find('.amount').text()
  59. amount = amount.replace('€','')
  60. amount = amount.replace(',', '.').trim()
  61. amount = parseFloat(amount)
  62. let pdfUrl = $(this).find('a > i').data('url')
  63. pdfUrl = baseUrl + pdfUrl
  64. //pdfUrl = `https://adsl.free.fr/${pdfUrl}`
  65. let billDate = $(this).find('.invoiceDate').text().trim()
  66. let monthAndYear = billDate.split('-')
  67. let billYear = monthAndYear[0].trim()
  68. let billMonth = monthAndYear[1].trim()
  69. //console.log(billMonth.toLowerCase())
  70. billMonth = moment.months().indexOf(billMonth.toLowerCase()) + 1
  71. billMonth = billMonth < 10 ? '0' + billMonth : billMonth
  72. //console.log(billMonth+"*"+billYear + "-----|" + billId + '->' + amount + "|||"+pdfUrl)
  73. //let month = "12"//pdfUrl.split('&')[2].split('=')[1]
  74. let date = moment(billYear + billMonth, 'YYYYMM')
  75. let bill = {
  76. amount,
  77. date: date.toDate(),
  78. vendor: 'Enercoop',
  79. filename: `${date.format('YYYYMM')}_enercoop.pdf`,
  80. fileurl: pdfUrl
  81. }
  82. bills.push(bill)
  83. })
  84. return bills
  85. }