Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const {
BaseKonnector,
requestFactory,
signin,
errors,
scrape,
saveBills,
log,
utils,
hydrateAndFilter,
addData
} = require('cozy-konnector-libs')
const moment = require('moment')
const rp = require('request-promise')
const startDate = moment()
.subtract(31, 'day')
.format('YYYY-MM-DD')
const endDate = moment().format('YYYY-MM-DD')
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 = 'https://gw.hml.api.enedis.fr'
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
// cozyParameters are static parameters, independents from the account. Most often, it can be a
// secret api key.
async function start(fields, cozyParameters) {
log('info', 'Authenticating ...')
console.log(fields.access_token)
// if (cozyParameters) log('debug', 'Found COZY_PARAMETERS')
// await authenticate(fields.login, fields.password)
// 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 getData(fields.access_token)
// 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', 'Saving data to Cozy')
storeData(documents)
// 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'],
// sourceAccount: this.accountId,
// sourceAccountIdentifier: fields.login
// })
}
// 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
}
}
})
}
async function getData(token) {
var usagePointID = 32320647321714
const dataRequest = {
method: 'GET',
uri:
baseUrl +
'/v3/metering_data/daily_consumption?start=' +
startDate +
'&end=' +
endDate +
'&usage_point_id=' +
usagePointID,
headers: {
Accept: 'application/json',
Authorization: 'Bearer ' + token
}
}
try {
const response = await rp(dataRequest)
return response
} catch (error) {
throw error
}
}
function storeData(data) {
console.log(data)
return hydrateAndFilter(data, 'io.enedis.day', {
keys: ['rank']
}).then(filteredDocuments => {
addData(filteredDocuments, 'io.enedis.day')
})
}
// 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
log($)
return $
}