-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsolarweb.js
More file actions
50 lines (47 loc) · 1.95 KB
/
Copy pathsolarweb.js
File metadata and controls
50 lines (47 loc) · 1.95 KB
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
var request = require('request').defaults({ jar: true });
var cheerio = require('cheerio');
var fs = require('fs');
var isLoggedIn = false;
module.exports = {
login: (name, pw, rememberme, callback) => {
if (!name || !pw) { callback(false); return; }
request.get('https://www.solarweb.com/', (err, res, body) => {
if(err) { callback(false); return; }
var $ = cheerio.load(body);
var token = $('input[type="hidden"]:nth-child(1)').val();
console.log($('input[type="hidden"]:nth-child(1)').val()); // login form token
var postData = {
form: {
__RequestVerificationToken: token,
UserName: name,
Password: pw,
ReturnUrl: "",
RememberMe: rememberme,
'X-Requested-With': 'XMLHttpRequest'
}
};
request.post('https://www.solarweb.com/', postData, (err, res, body) => {
var jsonbody = JSON.parse(res.body);
if (!err) {
if (jsonbody.data && jsonbody.data.url) {
isLoggedIn = true;
callback(true);
}
else callback(false);
}
});
})
},
getEnergyChartByDay: (pvSysId, date, callback) => {
if (!isLoggedIn || !pvSysId || !date || !date.day || !date.month || !date.year) { callback(false); return; }
var dateUrl = date.year + '/' + date.month + '/' + date.day;
var url = 'https://www.solarweb.com/Chart/GetChart/' + pvSysId + '/' + dateUrl + '/Day/production';
//console.log(request.cookie());
request.get({ 'url':url, accept:'application/json'}, (err, response, body) => {
if (!err) {
console.log(body);
callback(JSON.parse(body));
} else callback(false);
});
}
};